#include #include #include // ── Config ───────────────────────────────────────────────────────── config mach() { return (config) { .databases = {{ .engine = sqlite_db, .name = "todos_db", .connect = "file:todos.db?mode=rwc", .migrations = { "CREATE TABLE todos (" "id INTEGER PRIMARY KEY AUTOINCREMENT," "title TEXT NOT NULL," "done INTEGER NOT NULL DEFAULT 0," "created_at TEXT NOT NULL DEFAULT (datetime('now'))" ");", "CREATE TABLE stats (" "id INTEGER PRIMARY KEY CHECK (id = 1)," "total INTEGER NOT NULL DEFAULT 0," "done INTEGER NOT NULL DEFAULT 0," "updated_at TEXT NOT NULL DEFAULT (datetime('now'))" ");", "CREATE TABLE activity_log (" "id INTEGER PRIMARY KEY AUTOINCREMENT," "action TEXT NOT NULL," "detail TEXT," "ran_at TEXT NOT NULL DEFAULT (datetime('now'))" ");" }, .seeds = { "INSERT INTO todos(title) VALUES" "('Learn the MACH tasks API')," "('Build something cool')," "('Ship it');", "INSERT INTO stats(total, done, updated_at) VALUES" "(3, 0, datetime('now'));" } }}, // ── Tasks ──────────────────────────────────────────────────── // // All three flavors, each on the worker reactor. .tasks = { // task() — fire-and-forget. // Logs mutations without making the user wait. {"log_created", { query((d){ .db = "todos_db", "INSERT INTO activity_log(action, detail) " "VALUES('created', 'added: {{title}}');" }) }, .accepts = {"title"} }, {"log_toggled", { query((d){ .db = "todos_db", "INSERT INTO activity_log(action, detail) " "VALUES('toggled', 'toggled todo #{{id}}');" }) }, .accepts = {"id"} }, {"log_deleted", { query((d){ .db = "todos_db", "INSERT INTO activity_log(action, detail) " "VALUES('deleted', 'removed todo #{{id}}');" }) }, .accepts = {"id"} }, // .cron — scheduled background job. // Every minute, auto-complete todos older than 1 hour. {"cleanup_stale", { query((d){ .db = "todos_db", "UPDATE todos SET done = 1 " "WHERE done = 0 " "AND created_at < datetime('now', '-1 hour');" }), query((d){ .db = "todos_db", "INSERT INTO activity_log(action, detail) " "VALUES('cron', 'auto-completed stale todos');" }) }, .cron = "* * * * *" } }, // ── Resources ──────────────────────────────────────────────── .resources = {{ {"home", "/", .get = {{ reroute((u){"todos"}) }} }, // ── List ───────────────────────────────────────────────── {"todos", "/todos", .get = {{ queries((da){{ { .set = "todos", .db = "todos_db", "SELECT id, title, done, created_at " "FROM todos ORDER BY created_at DESC;" }, { .set = "stats", .db = "todos_db", "SELECT total, done FROM stats WHERE id = 1;" } }}), render((r){ "" "" "MACH Tasks Demo" "{{> tailwind_script}}" "" "" "
" "

Todos

" "{{#stats}}" "

" "{{done}} of {{total}} done" "

" "{{/stats}}" "
" "" "" "
" "{{#todos}}" "
" "
" "" "{{#done}}" "" "{{/done}}" "{{^done}}" "" "{{/done}}" "
" "{{#done}}" "{{title}}" "{{/done}}" "{{^done}}" "{{title}}" "{{/done}}" "
" "" "" "
" "
" "{{/todos}}" "{{^todos}}" "

No todos yet.

" "{{/todos}}" "" "View worker activity log →" "" "
" "" "" }) }}, .post = {{ validate((v){ .name = "title", .validation = validate_not_empty, .message = "Title cannot be empty" }), query((d){ .db = "todos_db", "INSERT INTO todos(title) VALUES({{title}});" }), query((d){ .db = "todos_db", .asset = "recount" }), // fire-and-forget: log the create asynchronously task((t){"log_created"}), redirect((u){"todos"}) }}, .errors = { {http_bad_request, { render((r){ "" "" "Error" "{{> tailwind_script}}" "" "" "
" "

{{error_message:title}}

" "← Back" "
" "" "" }) }} } }, // ── Single Todo ────────────────────────────────────────── {"todo", "/todos/:id", .before = { validate((v){ .name = "id", .validation = validate_positive, .message = "Invalid id" }) }, // Toggle done .put = {{ query((d){ .db = "todos_db", "UPDATE todos " "SET done = CASE WHEN done = 0 THEN 1 ELSE 0 END " "WHERE id = {{id}};" }), query((d){ .db = "todos_db", .asset = "recount" }), task((t){"log_toggled"}), redirect((u){"todos"}) }}, // Delete .delete = {{ // prepare log detail before the row is gone query((d){ .db = "todos_db", "DELETE FROM todos WHERE id = {{id}};" }), query((d){ .db = "todos_db", .asset = "recount" }), task((t){"log_deleted"}), redirect((u){"todos"}) }} }, // ── Activity Log ───────────────────────────────────────── {"activity", "/activity", .get = {{ query((d){ .set = "logs", .db = "todos_db", "SELECT action, detail, ran_at " "FROM activity_log ORDER BY ran_at DESC LIMIT 50;" }), render((r){ "" "" "Worker Activity" "{{> tailwind_script}}" "" "" "
" "

Worker Activity

" "

" "Task executions from the worker reactor" "

" "" "← Back to todos" "" "{{#logs}}" "
" "{{action}}" "{{detail}}" "{{ran_at}}" "
" "{{/logs}}" "{{^logs}}" "

No activity yet.

" "{{/logs}}" "
" "" "" }) }} }}, .context = { {"recount", "UPDATE stats SET " "total = (SELECT count(*) FROM todos)," "done = (SELECT count(*) FROM todos WHERE done = 1)," "updated_at = datetime('now') " "WHERE id = 1;" } } }, .modules = {sqlite, tailwind} }; }