nerack repo
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
|
||||
module(hello){
|
||||
context("greeting", "hello world");
|
||||
http("home", "/", .mime = mime_text,
|
||||
.get = {
|
||||
http_response("greeting")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
|
||||
module(hello_world){
|
||||
context("greeting", "hello {{name}}");
|
||||
http("home", "/", .mime = mime_text,
|
||||
.get = {
|
||||
input({"name", not_empty_input, .default_value = "world"}),
|
||||
html("greeting", "hello"),
|
||||
http_response("hello")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
|
||||
module(hello_world){
|
||||
context("greeting",
|
||||
"<html>"
|
||||
"<head></head>"
|
||||
"<body>"
|
||||
"<p>Hello {{name}}</p>"
|
||||
"</body>"
|
||||
"</html>"
|
||||
);
|
||||
http("home", "/",
|
||||
.get = {
|
||||
input({"name", not_empty_input, .default_value = "world"}),
|
||||
html("greeting", "hello"),
|
||||
http_response("hello")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
CREATE TABLE IF NOT EXISTS greetings (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
@@ -0,0 +1,3 @@
|
||||
select name
|
||||
from greetings
|
||||
limit 1;
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <sqlite.h>
|
||||
|
||||
module(hello_world){
|
||||
sqlite(
|
||||
"hello_db",
|
||||
"file:hello.db?mode=rwc",
|
||||
{"create_hello_world_table"},
|
||||
{"seed_hello_world"}
|
||||
);
|
||||
|
||||
http("home", "/",
|
||||
.get = {
|
||||
sqlite_query({"hello_db", "get_hello_world", "hello_world_data"}),
|
||||
html("hello_world", "hello"),
|
||||
http_response("hello")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
{{#hello_world_data}}
|
||||
<p>Hello {{name}}</p>
|
||||
{{/hello_world_data}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,2 @@
|
||||
INSERT INTO greetings(name)
|
||||
VALUES('World');
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,2 @@
|
||||
insert into todos(title)
|
||||
values({{title}});
|
||||
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
finished INTEGER CHECK(finished IN (1))
|
||||
);
|
||||
@@ -0,0 +1,2 @@
|
||||
delete from todos
|
||||
where id = {{id}};
|
||||
@@ -0,0 +1,2 @@
|
||||
select id, title, finished
|
||||
from todos;
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,23 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='{{asset:favicon.png}}'>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='{{url:get:login}}'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='{{url:get:home}}'>home</a>
|
||||
<a href='{{url:get:about}}'>about us</a>
|
||||
<a href='{{url:get:contact}}'>contact us</a>
|
||||
<a href='{{url:get:todos}}'>todos</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,70 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <sqlite.h>
|
||||
#include <cookie_auth.h>
|
||||
|
||||
module(todo){
|
||||
sqlite(
|
||||
"todos_db",
|
||||
"file:{{user_id}}_todo.db?mode=rwc",
|
||||
{"create_todos_table"}
|
||||
);
|
||||
|
||||
http("home", "/",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("home", "home_s"),
|
||||
http_response("home_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("about", "/about",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("about", "about_s"),
|
||||
http_response("about_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("contact", "/contact",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("contact", "contact_s"),
|
||||
http_response("contact_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("todos", "/todos",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session()
|
||||
},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", "get_todos", "todos_data"}),
|
||||
html("todos", "todos_s"),
|
||||
http_response("todos_s")
|
||||
},
|
||||
.post = {
|
||||
input({"title", not_empty_input}),
|
||||
sqlite_query({"todos_db", "create_todo"}),
|
||||
http_redirect("todos")
|
||||
}
|
||||
);
|
||||
|
||||
http("todo", "/todos/:id",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session(),
|
||||
input({"id", positive_integer_input})
|
||||
},
|
||||
.patch = {
|
||||
input({"finished", "^1$", "must be 1", .optional = true}),
|
||||
sqlite_query({"todos_db", "update_todo"}),
|
||||
http_redirect("todos")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", "delete_todo"}),
|
||||
http_redirect("todos")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<form action="{{url:post:todos}}" method="post">
|
||||
<input type="text" name="title" placeholder="new todo" required>
|
||||
<button type="submit">add</button>
|
||||
</form>
|
||||
{{^todos_data}}
|
||||
<p>no todos</p>
|
||||
{{/todos_data}}
|
||||
{{#todos_data}}
|
||||
<div>
|
||||
<form action="{{url:patch:todo}}" method="post" style="display:inline">
|
||||
{{^finished}}
|
||||
<input type="checkbox" name="finished" value="1">
|
||||
{{/finished}}
|
||||
{{#finished}}
|
||||
<input type="checkbox" name="finished" value="1" checked>
|
||||
{{/finished}}
|
||||
{{title}}
|
||||
<button type="submit">save</button>
|
||||
</form>
|
||||
<form action="{{url:delete:todo}}" method="post" style="display:inline">
|
||||
<button type="submit">delete</button>
|
||||
</form>
|
||||
</div>
|
||||
{{/todos_data}}
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,3 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where id = {{id}};
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,3 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}})
|
||||
returning id, title, finished;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
finished INTEGER CHECK(finished IN (1))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_user_id ON todos(user_id);
|
||||
@@ -0,0 +1,4 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id;
|
||||
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,25 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='{{asset:favicon.png}}'>
|
||||
{{$head}}
|
||||
{{/head}}
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='{{url:get:login}}'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='{{url:get:home}}'>home</a>
|
||||
<a href='{{url:get:about}}'>about us</a>
|
||||
<a href='{{url:get:contact}}'>contact us</a>
|
||||
<a href='{{url:get:todos}}'>todos</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <sqlite.h>
|
||||
#include <datastar.h>
|
||||
#include <cookie_auth.h>
|
||||
|
||||
module(todo){
|
||||
sqlite(
|
||||
"todos_db",
|
||||
"file:todo.db?mode=rwc",
|
||||
{"create_todos_table"}
|
||||
);
|
||||
|
||||
http("home", "/",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("home", "home_s"),
|
||||
http_response("home_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("about", "/about",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("about", "about_s"),
|
||||
http_response("about_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("contact", "/contact",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("contact", "contact_s"),
|
||||
http_response("contact_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("todos", "/todos",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session()
|
||||
},
|
||||
.sse = {"todos:{{user_id}}"},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", "get_todos", "todos_data"}),
|
||||
html("todos", "todos_s"),
|
||||
http_response("todos_s")
|
||||
},
|
||||
.post = {
|
||||
input({"title", not_empty_input}),
|
||||
sqlite_query({"todos_db", "create_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todos", .mode = prepend_mode, .elements = "todo_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("todo", "/todos/:id",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session(),
|
||||
input({"id", positive_integer_input})
|
||||
},
|
||||
.patch = {
|
||||
input({"finished", "^1$", "must be 1", .optional = true}),
|
||||
sqlite_query({"todos_db", "update_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = replace_mode, .elements = "todo_s")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", "delete_todo", .error_on_empty = true}),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = remove_mode)
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#todo_data}}
|
||||
{{> todo_item}}
|
||||
{{/todo_data}}
|
||||
@@ -0,0 +1,10 @@
|
||||
<div id="todo_{{id}}">
|
||||
{{^finished}}
|
||||
<input type="checkbox" data-on:click__prevent="@patch('{{url:patch:todo:finished=1}}')">
|
||||
{{/finished}}
|
||||
{{#finished}}
|
||||
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:patch:todo}}')">
|
||||
{{/finished}}
|
||||
{{title}}
|
||||
<button data-on:click="@delete('{{url:delete:todo}}')">delete</button>
|
||||
</div>
|
||||
@@ -0,0 +1,16 @@
|
||||
{{< layout}}
|
||||
{{$head}}
|
||||
{{> datastar }}
|
||||
{{/head}}
|
||||
{{$body}}
|
||||
<input type="text" placeholder="new todo" data-bind:title
|
||||
data-on:keydown="evt.key === 'Enter' && @post('{{url:post:todos}}') && ($title = '');"
|
||||
>
|
||||
<button data-on:click="@post('{{url:post:todos}}') && ($title = '')">add</button>
|
||||
<div id="todos" data-init="@get('{{url:events:todos}}')">
|
||||
{{#todos_data}}
|
||||
{{> todo_item}}
|
||||
{{/todos_data}}
|
||||
</div>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id, title, finished;
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <sqlite.h>
|
||||
#include <pubsub.h>
|
||||
#include <cookie_auth.h>
|
||||
|
||||
module(activity){
|
||||
sqlite(
|
||||
"activity_db",
|
||||
"file:activity.db?mode=rwc",
|
||||
{"create_activity_table"}
|
||||
);
|
||||
|
||||
subscribe("todo_created", {
|
||||
sqlite_query({"activity_db", "insert_activity"})
|
||||
});
|
||||
|
||||
http("activity", "/activity",
|
||||
.get = {
|
||||
cookie_logged_in(),
|
||||
cookie_session(),
|
||||
sqlite_query({"activity_db", "get_activities", "activities"}),
|
||||
html("activity", "activity_s"),
|
||||
http_response("activity_s")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
{{^activities}}
|
||||
<p>no activity</p>
|
||||
{{/activities}}
|
||||
{{#activities}}
|
||||
<p>{{action}}: {{title}} ({{created_at}})</p>
|
||||
{{/activities}}
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE IF NOT EXISTS activity (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
action TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_user_id ON activity(user_id);
|
||||
@@ -0,0 +1,5 @@
|
||||
select action, title, created_at
|
||||
from activity
|
||||
where user_id = {{user_id}}
|
||||
order by created_at desc
|
||||
limit 50;
|
||||
@@ -0,0 +1,2 @@
|
||||
insert into activity(user_id, action, title)
|
||||
values({{user_id}}, 'created', {{title}});
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <cookie_auth.h>
|
||||
|
||||
module(home){
|
||||
http("home", "/",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("home", "home_s"),
|
||||
http_response("home_s")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,22 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='{{asset:favicon.png}}'>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='{{url:get:login}}'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='{{url:get:home}}'>home</a>
|
||||
<a href='{{url:get:todos}}'>todos</a>
|
||||
<a href='{{url:get:activity}}'>activity</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,2 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}});
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
finished INTEGER CHECK(finished IN (1))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_user_id ON todos(user_id);
|
||||
@@ -0,0 +1,3 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <sqlite.h>
|
||||
#include <pubsub.h>
|
||||
#include <cookie_auth.h>
|
||||
|
||||
module(todos){
|
||||
sqlite(
|
||||
"todos_db",
|
||||
"file:todo.db?mode=rwc",
|
||||
{"create_todos_table"}
|
||||
);
|
||||
|
||||
publish("todo_created",
|
||||
.with = {"user_id", "title"}
|
||||
);
|
||||
|
||||
http("todos", "/todos",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session()
|
||||
},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", "get_todos", "todos_data"}),
|
||||
html("todos", "todos_s"),
|
||||
http_response("todos_s")
|
||||
},
|
||||
.post = {
|
||||
input({"title", not_empty_input}),
|
||||
sqlite_query({"todos_db", "create_todo"}),
|
||||
emit("todo_created"),
|
||||
http_redirect("todos")
|
||||
}
|
||||
);
|
||||
|
||||
http("todo", "/todos/:id",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session(),
|
||||
input({"id", positive_integer_input})
|
||||
},
|
||||
.patch = {
|
||||
input({"finished", "^1$", "must be 1", .optional = true}),
|
||||
sqlite_query({"todos_db", "update_todo"}),
|
||||
http_redirect("todos")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", "delete_todo"}),
|
||||
http_redirect("todos")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<form action="{{url:post:todos}}" method="post">
|
||||
<input type="text" name="title" placeholder="new todo" required>
|
||||
<button type="submit">add</button>
|
||||
</form>
|
||||
{{^todos_data}}
|
||||
<p>no todos</p>
|
||||
{{/todos_data}}
|
||||
{{#todos_data}}
|
||||
<div>
|
||||
<form action="{{url:patch:todo}}" method="post" style="display:inline">
|
||||
{{^finished}}
|
||||
<input type="checkbox" name="finished" value="1">
|
||||
{{/finished}}
|
||||
{{#finished}}
|
||||
<input type="checkbox" name="finished" value="1" checked>
|
||||
{{/finished}}
|
||||
{{title}}
|
||||
<button type="submit">save</button>
|
||||
</form>
|
||||
<form action="{{url:delete:todo}}" method="post" style="display:inline">
|
||||
<button type="submit">delete</button>
|
||||
</form>
|
||||
</div>
|
||||
{{/todos_data}}
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,4 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
@@ -0,0 +1 @@
|
||||
compare to https://github.com/t3dotgg/1app5stacks
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS pokemons (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
sprite TEXT NOT NULL,
|
||||
wins INTEGER NOT NULL DEFAULT 0,
|
||||
losses INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
select id, name, sprite
|
||||
from pokemons
|
||||
order by random()
|
||||
limit 2;
|
||||
@@ -0,0 +1,9 @@
|
||||
select id, name, sprite, wins, losses,
|
||||
round(100.0 * wins / nullif(wins + losses, 0), 1) as win_pct,
|
||||
row_number() over (
|
||||
order by 1.0 * wins / nullif(wins + losses, 0) desc nulls last,
|
||||
wins + losses desc,
|
||||
name
|
||||
) as rank
|
||||
from pokemons
|
||||
order by rank;
|
||||
@@ -0,0 +1,53 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Roundest (Nerack Version)</title>
|
||||
{{> htmx }}
|
||||
{{> tailwind }}
|
||||
</head>
|
||||
<body hx-boost='true'>
|
||||
<div class='bg-gray-950 text-white flex flex-col justify-between font-geist min-h-screen border-t-2 border-red-300'>
|
||||
<nav class='flex flex-row justify-between items-center py-4 px-8'>
|
||||
<div class='flex items-baseline'>
|
||||
<a href='{{url:get:home}}' class='font-bold text-3xl'>
|
||||
Round<span class='text-red-300'>est</span>
|
||||
</a>
|
||||
<p class='text-gray-400 font-extralight pl-2 text-2xl'>
|
||||
(Nerack Version)
|
||||
</p>
|
||||
</div>
|
||||
<div class='flex flex-row items-center gap-8'>
|
||||
<a href='{{url:get:results}}' class='hover:underline text-lg'>Results</a>
|
||||
</div>
|
||||
</nav>
|
||||
<main>
|
||||
{{$body}}
|
||||
<div class='container mx-auto px-4'>
|
||||
<h1 class='text-3xl font-bold text-center mb-8'>Vote for which is roundest</h1>
|
||||
<div class='flex justify-center gap-16 items-center min-h-[80vh]'>
|
||||
{{#challengers}}
|
||||
<form action='{{url:post:home}}' method='post'>
|
||||
<div class='flex flex-col items-center gap-4'>
|
||||
<img src='{{sprite}}' alt='{{name}}' class='w-64 h-64 sprite'>
|
||||
<div class='text-center'>
|
||||
<span class='text-gray-500 text-lg'>#{{id}}</span>
|
||||
<h2 class='text-2xl font-bold capitalize'>{{name}}</h2>
|
||||
<input type='hidden' name='winner' value='{{id}}'>
|
||||
<input type='hidden' name='loser' value='{{opponent_id}}'>
|
||||
<button class='mt-4 px-8 py-3 bg-blue-500 text-white rounded-lg text-lg font-semibold hover:bg-blue-600 transition-colors'>
|
||||
Vote
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{{/challengers}}
|
||||
</div>
|
||||
</div>
|
||||
{{/body}}
|
||||
</main>
|
||||
<footer class='font-light text-center py-3 text-gray-500'>
|
||||
<a href='https://code.nightshadecoder.dev/nightshade/nerack' target='_blank'>Gitea</a>
|
||||
</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,28 @@
|
||||
{{< home}}
|
||||
{{$body}}
|
||||
<div class='container mx-auto px-4 py-8 text-white'>
|
||||
<div class='grid gap-4'>
|
||||
{{#results_data}}
|
||||
<div class='flex items-center gap-6 p-6 bg-gray-800/40 rounded-lg shadow hover:shadow-md transition-shadow'>
|
||||
<div class='text-2xl font-bold text-gray-400 w-8'>
|
||||
{{rank}}
|
||||
</div>
|
||||
<img src='{{sprite}}' alt='{{name}}' loading='lazy' class='w-20 h-20'>
|
||||
<div class='flex-grow'>
|
||||
<div class='text-gray-400 text-sm'>#{{id}}</div>
|
||||
<h2 class='text-xl font-semibold capitalize'>{{name}}</h2>
|
||||
</div>
|
||||
<div class='text-right'>
|
||||
<div class='text-2xl font-bold text-blue-400'>
|
||||
{{#win_pct}}{{win_pct}}%{{/win_pct}}{{^win_pct}}n/a{{/win_pct}}
|
||||
</div>
|
||||
<div class='text-sm text-gray-400'>
|
||||
{{wins}}W - {{losses}}L
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{/results_data}}
|
||||
</div>
|
||||
</div>
|
||||
{{/body}}
|
||||
{{/home}}
|
||||
@@ -0,0 +1,45 @@
|
||||
#include <nerack.h>
|
||||
#include <http.h>
|
||||
#include <htmx.h>
|
||||
#include <sqlite.h>
|
||||
#include <tailwind.h>
|
||||
|
||||
module(roundest){
|
||||
sqlite(
|
||||
"pokemon_db",
|
||||
"file::memory:?cache=shared",
|
||||
{"create_pokemons_table"},
|
||||
{"seed_pokemons"}
|
||||
);
|
||||
|
||||
http("home", "/",
|
||||
.get = {
|
||||
sqlite_query({"pokemon_db", "get_challengers", "challengers"}),
|
||||
run(^(){
|
||||
auto const t = get("challengers");
|
||||
auto const p0 = table_get(t, 0);
|
||||
auto const p1 = table_get(t, 1);
|
||||
record_set(p0, "opponent_id", record_get(p1, "id"));
|
||||
record_set(p1, "opponent_id", record_get(p0, "id"));
|
||||
}),
|
||||
html("home", "home_s"),
|
||||
http_response("home_s")
|
||||
},
|
||||
.post = {
|
||||
input(
|
||||
{"winner", positive_integer_input},
|
||||
{"loser", positive_integer_input}
|
||||
),
|
||||
sqlite_query({"pokemon_db", "vote"}),
|
||||
http_redirect("home")
|
||||
}
|
||||
);
|
||||
|
||||
http("results", "/results",
|
||||
.get = {
|
||||
sqlite_query({"pokemon_db", "get_results", "results_data"}),
|
||||
html("results", "results_s"),
|
||||
http_response("results_s")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
INSERT INTO pokemons(name, sprite) VALUES
|
||||
('bulbasaur', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png'),
|
||||
('charmander', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/4.png'),
|
||||
('squirtle', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/7.png'),
|
||||
('caterpie', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/10.png'),
|
||||
('pidgey', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/16.png'),
|
||||
('pikachu', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/25.png'),
|
||||
('jigglypuff', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/39.png'),
|
||||
('meowth', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/52.png'),
|
||||
('psyduck', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/54.png'),
|
||||
('machop', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/66.png'),
|
||||
('magnemite', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/81.png'),
|
||||
('gastly', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/92.png'),
|
||||
('onix', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/95.png'),
|
||||
('voltorb', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/100.png'),
|
||||
('eevee', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/133.png'),
|
||||
('snorlax', 'https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/143.png');
|
||||
@@ -0,0 +1,8 @@
|
||||
begin transaction;
|
||||
update pokemons
|
||||
set wins = wins + 1
|
||||
where id = {{winner}};
|
||||
update pokemons
|
||||
set losses = losses + 1
|
||||
where id = {{loser}};
|
||||
commit;
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
about us
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
contact us
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,3 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}})
|
||||
returning id, title, finished;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
finished INTEGER CHECK(finished IN (1))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_todos_user_id ON todos(user_id);
|
||||
@@ -0,0 +1,4 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id;
|
||||
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
home
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,25 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='{{asset:favicon.png}}'>
|
||||
{{$head}}
|
||||
{{/head}}
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='{{url:get:login}}'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='{{url:get:http_home}}'>home</a>
|
||||
<a href='{{url:get:http_about}}'>about us</a>
|
||||
<a href='{{url:get:http_contact}}'>contact us</a>
|
||||
<a href='{{url:get:http_todos}}'>todos</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
>Nerack Todos
|
||||
|
||||
{{#user}}welcome, {{short_name}}{{/user}}{{^user}}not identified{{/user}}
|
||||
|
||||
`[home`{{url:get:rns_home}}] `[about us`{{url:get:rns_about}}] `[contact us`{{url:get:rns_contact}}] `[todos`{{url:get:rns_todos}}]
|
||||
|
||||
-
|
||||
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
@@ -0,0 +1,134 @@
|
||||
#include <nerack.h>
|
||||
#include <sqlite.h>
|
||||
#include <http.h>
|
||||
#include <cookie_auth.h>
|
||||
#include <datastar.h>
|
||||
#include <rns.h>
|
||||
|
||||
module(todo){
|
||||
sqlite(
|
||||
"todos_db",
|
||||
"file:todo.db?mode=rwc",
|
||||
{"create_todos_table"}
|
||||
);
|
||||
|
||||
http("http_home", "/",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("home", "home_s"),
|
||||
http_response("home_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("http_about", "/about",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("about", "about_s"),
|
||||
http_response("about_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("http_contact", "/contact",
|
||||
.get = {
|
||||
cookie_session(),
|
||||
html("contact", "contact_s"),
|
||||
http_response("contact_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("http_todos", "/todos",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session()
|
||||
},
|
||||
.sse = {"todos:{{user_id}}"},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", "get_todos", "todos_data"}),
|
||||
html("todos", "todos_s"),
|
||||
http_response("todos_s")
|
||||
},
|
||||
.post = {
|
||||
input({"title", not_empty_input}),
|
||||
sqlite_query({"todos_db", "create_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todos", .mode = prepend_mode, .elements = "todo_s")
|
||||
}
|
||||
);
|
||||
|
||||
http("http_todo", "/todos/:id",
|
||||
.all = {
|
||||
cookie_logged_in(),
|
||||
cookie_session(),
|
||||
input({"id", positive_integer_input})
|
||||
},
|
||||
.patch = {
|
||||
input({"finished", "^1$", "must be 1", .optional = true}),
|
||||
sqlite_query({"todos_db", "update_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = replace_mode, .elements = "todo_s")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", "delete_todo", .error_on_empty = true}),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = remove_mode)
|
||||
}
|
||||
);
|
||||
|
||||
rns("rns_home", "/",
|
||||
.get = {
|
||||
rns_identity(),
|
||||
micron("home", "home_s"),
|
||||
rns_response("home_s")
|
||||
}
|
||||
);
|
||||
|
||||
rns("rns_about", "/about",
|
||||
.get = {
|
||||
rns_identity(),
|
||||
micron("about", "about_s"),
|
||||
rns_response("about_s")
|
||||
}
|
||||
);
|
||||
|
||||
rns("rns_contact", "/contact",
|
||||
.get = {
|
||||
rns_identity(),
|
||||
micron("contact", "contact_s"),
|
||||
rns_response("contact_s")
|
||||
}
|
||||
);
|
||||
|
||||
rns("rns_todos", "/todos",
|
||||
.all = {rns_identity()},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", "get_todos", "todos_data"}),
|
||||
micron("todos", "todos_s"),
|
||||
rns_response("todos_s")
|
||||
},
|
||||
.post = {
|
||||
input({"title", not_empty_input}),
|
||||
sqlite_query({"todos_db", "create_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todos", .mode = prepend_mode, .elements = "todo_s"),
|
||||
rns_reroute("rns_todos")
|
||||
}
|
||||
);
|
||||
|
||||
rns("rns_todo", "/todos/:id",
|
||||
.all = {
|
||||
rns_identity(),
|
||||
input({"id", positive_integer_input})
|
||||
},
|
||||
.patch = {
|
||||
input({"finished", "^1$", "must be 1", .optional = true}),
|
||||
sqlite_query({"todos_db", "update_todo", "todo_data", .error_on_empty = true}),
|
||||
html("todo", "todo_s"),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = replace_mode, .elements = "todo_s"),
|
||||
rns_reroute("rns_todos")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", "delete_todo", .error_on_empty = true}),
|
||||
datastar("todos:{{user_id}}", .target = "#todo_{{id}}", .mode = remove_mode),
|
||||
rns_reroute("rns_todos")
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{#todo_data}}
|
||||
{{> todo_item}}
|
||||
{{/todo_data}}
|
||||
@@ -0,0 +1,10 @@
|
||||
<div id="todo_{{id}}">
|
||||
{{^finished}}
|
||||
<input type="checkbox" data-on:click__prevent="@patch('{{url:patch:http_todo:finished=1}}')">
|
||||
{{/finished}}
|
||||
{{#finished}}
|
||||
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:patch:http_todo}}')">
|
||||
{{/finished}}
|
||||
{{title}}
|
||||
<button data-on:click="@delete('{{url:delete:http_todo}}')">delete</button>
|
||||
</div>
|
||||
@@ -0,0 +1 @@
|
||||
{{^finished}}`[mark done`{{url:patch:rns_todo:finished=1}}]{{/finished}}{{#finished}}`[mark undone`{{url:patch:rns_todo}}]{{/finished}} {{title}} `[delete`{{url:delete:rns_todo}}]
|
||||
@@ -0,0 +1,16 @@
|
||||
{{< layout}}
|
||||
{{$head}}
|
||||
{{> datastar }}
|
||||
{{/head}}
|
||||
{{$body}}
|
||||
<input type="text" placeholder="new todo" data-bind:title
|
||||
data-on:keydown="evt.key === 'Enter' && @post('{{url:post:http_todos}}') && ($title = '');"
|
||||
>
|
||||
<button data-on:click="@post('{{url:post:http_todos}}') && ($title = '')">add</button>
|
||||
<div id="todos" data-init="@get('{{url:events:http_todos}}')">
|
||||
{{#todos_data}}
|
||||
{{> todo_item}}
|
||||
{{/todos_data}}
|
||||
</div>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,16 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
>todos
|
||||
|
||||
`<title`new todo> `[add`{{url:post:rns_todos}}`title]
|
||||
|
||||
-
|
||||
|
||||
{{^todos_data}}
|
||||
no todos
|
||||
{{/todos_data}}
|
||||
{{#todos_data}}
|
||||
{{> todo_item}}
|
||||
{{/todos_data}}
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
@@ -0,0 +1,5 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id, title, finished;
|
||||
Reference in New Issue
Block a user