MaCH repo

This commit is contained in:
2025-07-24 12:46:01 -05:00
committed by Nick Ricketts
commit 5d6178cc3c
105 changed files with 5801 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>not found</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>error</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>about us</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>contact us</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,3 @@
insert into todos(user_id, title)
values({{user_id}}, {{title}})
returning id, title, finished;

View File

@@ -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);

View File

@@ -0,0 +1,4 @@
delete from todos
where user_id = {{user_id}}
and id = {{id}}
returning id;

View File

@@ -0,0 +1,3 @@
select id, title, finished
from todos
where user_id = {{user_id}};

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>home</p>
{{/body}}
{{/layout}}

View File

@@ -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>

157
06_todo_sse_datastar/main.c Normal file
View File

@@ -0,0 +1,157 @@
#include <mach.h>
#include <sqlite.h>
#include <datastar.h>
#include <session_auth.h>
config mach(){
return (config) {
.resources = {{
{"home", "/",
.get = {{
render((r){(asset){
#embed "home.mustache.html"
}})
}}
},
{"about", "/about",
.get = {{
render((r){(asset){
#embed "about.mustache.html"
}})
}}
},
{"contact", "/contact",
.get = {{
render((r){(asset){
#embed "contact.mustache.html"
}})
}}
},
{"todos", "/todos",
.sse = {.channel = "todos:{{user_id}}"},
.get = {{
query((d){
.set = "todos",
.db = "todos_db",
.query = (asset){
#embed "get_todos.sql"
}
}),
render((r){(asset){
#embed "todos.mustache.html"
}})
}},
.post = {{
validate((v){
.name = "title",
.validation = "^\\S{1,16}$",
.message = "must be 1-16 characters, no spaces"
}),
query((d){
.set = "todo",
.db = "todos_db",
.query = (asset){
#embed "create_todo.sql"
}
}),
ds_sse((ds){
.target = "todos",
.mode = mode_prepend,
.channel = "todos:{{user_id}}",
.elements = {.asset = "todo"}
})
}},
.before = {logged_in()}
},
{"todo", "/todos/:id",
.patch = {{
validate((v){
.name = "finished",
.optional = true,
.validation = "1",
.message = "must be 1"
}),
find((d){
.set = "todo",
.db = "todos_db",
.query = (asset){
#embed "update_todo.sql"
}
}),
ds_sse((ds){
.mode = mode_replace,
.target = "todo_{{id}}",
.channel = "todos:{{user_id}}",
.elements = {.asset = "todo"}
})
}},
.delete = {{
find((d){
.db = "todos_db",
.query = (asset){
#embed "delete_todo.sql"
}
}),
ds_sse((ds){
.mode = mode_remove,
.target = "todo_{{id}}",
.channel = "todos:{{user_id}}"
})
}},
.before = {
logged_in(),
validate((v){
.name = "id",
.validation = "^\\d{1,10}$",
.message = "must be between 1-9999999999"
})
}
}},
.before = {session()},
.context = {
{"layout", (asset){
#embed "layout.mustache.html"
}},
{"todo", (asset){
#embed "todo.mustache.html"
}}
},
.errors = {
{http_error, {
render((r){(asset){
#embed "5xx.mustache.html"
}})
}},
{http_not_found, {
render((r){(asset){
#embed "404.mustache.html"
}})
}}
}
},
.databases = {{
.engine = sqlite_db,
.name = "todos_db",
.connect = "file:todo.db?mode=rwc",
.migrations = {(asset){
#embed "create_todos_table.sql"
}}
}},
.modules = {sqlite, datastar, session_auth}
};
}

View File

View File

@@ -0,0 +1,10 @@
<div id="todo_{{id}}">
{{^finished}}
<input type="checkbox" data-on:click__prevent="@patch('{{url:todo:id}}?finished=1')">
{{/finished}}
{{#finished}}
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:todo:id}}')">
{{/finished}}
{{title}}
<button data-on:click="@delete('{{url:todo:id}}')">delete</button>
</div>

View File

@@ -0,0 +1,21 @@
{{< layout}}
{{$head}}
{{> datastar_script }}
{{/head}}
{{$body}}
<input type="text" placeholder="new todo" data-bind:title
data-on:keydown="evt.key === 'Enter' && @post('{{utl:todos}}') && ($title = '');"
>
<button data-on:click="@post('{{url:todos}}') && ($title = '')">add</button>
<div id="todos" data-init="@get('{{url:todos}}?http_method=sse')">
{{^todos}}
<p>no todos</p>
{{/todos}}
{{#todos}}
{{#.}}
{{> todo}}
{{/.}}
{{/todos}}
</div>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
update todos
set finished = {{finished}}
where user_id = {{user_id}}
and id = {{id}}
returning id, title, finished;