nerak repo

This commit is contained in:
2026-07-31 15:48:58 -05:00
committed by nightshade
commit 0155e6ddf0
84 changed files with 5448 additions and 0 deletions
@@ -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}}
+68
View File
@@ -0,0 +1,68 @@
#include <nerak.h>
#include <sqlite.h>
#include <datastar.h>
#include <session_auth.h>
config(app){
middleware(session());
sqlite_config(
"todos_db",
"file:todo.db?mode=rwc",
{"create_todos_table"}
);
resource("home", "/",
.get = {
mustache("home", "home_s"),
respond("home_s")
}
);
resource("about", "/about",
.get = {
mustache("about", "about_s"),
respond("about_s")
}
);
resource("contact", "/contact",
.get = {
mustache("contact", "contact_s"),
respond("contact_s")
}
);
resource("todos", "/todos",
.all = {logged_in()},
.sse = {"todos:{{user_id}}"},
.get = {
sqlite_query({"todos_db", "get_todos", "todos_data"}),
mustache("todos", "todos_s"),
respond("todos_s")
},
.post = {
input({"title", n_not_empty}),
sqlite_query({"todos_db", "create_todo", "todo_data", .err_on_empty = true}),
mustache("todo", "todo_s"),
datastar("todos:{{user_id}}", .target = "todos", .mode = ds_prepend, .elements = "todo_s")
}
);
resource("todo", "/todos/:id",
.all = {
logged_in(),
input({"id", n_positive})
},
.patch = {
input({"finished", "1", "must be 1", .opt = true}),
sqlite_query({"todos_db", "update_todo", "todo_data", .err_on_empty = true}),
mustache("todo", "todo_s"),
datastar("todos:{{user_id}}", .target = "todo_{{id}}", .mode = ds_replace, .elements = "todo_s")
},
.delete = {
sqlite_query({"todos_db", "delete_todo", .err_on_empty = true}),
datastar("todos:{{user_id}}", .target = "todo_{{id}}", .mode = ds_remove)
}
);
}
@@ -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:login}}'>sign in</a>
{{/user}}
{{#user}}
welcome, {{short_name}}
{{/user}}
</p>
<nav>
<a href='{{url:home}}'>home</a>
<a href='{{url:about}}'>about us</a>
<a href='{{url:contact}}'>contact us</a>
<a href='{{url:todos}}'>todos</a>
</nav>
{{$body}}
{{/body}}
</body>
</html>
@@ -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:todo}}?{{csrf:param}}&finished=1')">
{{/finished}}
{{#finished}}
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:todo}}?{{csrf:param}}')">
{{/finished}}
{{title}}
<button data-on:click="@delete('{{url:todo}}?{{csrf:param}}')">delete</button>
</div>
@@ -0,0 +1,19 @@
{{< layout}}
{{$head}}
{{> datastar }}
{{/head}}
{{$body}}
<input type="text" placeholder="new todo" data-bind:title
data-on:keydown="evt.key === 'Enter' && @post('{{url:todos}}?{{csrf:param}}') && ($title = '');"
>
<button data-on:click="@post('{{url:todos}}?{{csrf:param}}') && ($title = '')">add</button>
<div id="todos" data-init="@get('{{url:todos}}?{{http_sse:param}}')">
{{^todos_data}}
<p>no todos</p>
{{/todos_data}}
{{#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;