#include #include #include config mach() { return (config) { .resources = { {"home", "/", .get = {reroute("todos")} }, {"todos", "/todos", .get = { query( {.set_key = "todos", .db = "todos_db", .query = "SELECT id, title, done, created_at " "FROM todos ORDER BY created_at DESC;" }, {.set_key = "stats", .db = "todos_db", .query = "SELECT total, done FROM stats WHERE id = 1;" } ), render( "" "" "MACH Tasks Demo" "{{> tailwind }}" "" "" "
" "

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 = { input({"title", .matches = m_not_empty}), query({.db = "todos_db", .query = "INSERT INTO todos(title) VALUES({{title}});"}), query({"recount", .db = "todos_db"}), task("log_created"), redirect("todos") } }, {"todo", "/todos/:id", { input({"id", .matches = m_positive})}, .put = { query({.db = "todos_db", .query = "UPDATE todos " "SET done = CASE WHEN done = 0 THEN 1 ELSE 0 END " "WHERE id = {{id}};" }), query({"recount", .db = "todos_db"}), task("log_toggled"), redirect("todos") }, .delete = { query({.db = "todos_db", .query = "DELETE FROM todos WHERE id = {{id}};"}), query({"recount", .db = "todos_db"}), task("log_deleted"), redirect("todos") } }, {"activity", "/activity", .get = { query({.set_key = "logs", .db = "todos_db", .query = "SELECT action, detail, ran_at " "FROM activity_log ORDER BY ran_at DESC LIMIT 50;" }), render( "" "" "Worker Activity" "{{> tailwind }}" "" "" "
" "

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;" } }, .errors = { {m_bad_request, { render( "" "" "Error" "{{> tailwind }}" "" "" "
" "

{{error_message:title}}

" "← Back" "
" "" "" ) }} }, .tasks = { {"log_created", { query({.db = "todos_db", .query = "INSERT INTO activity_log(action, detail) " "VALUES('created', 'added: {{title}}');" })}, .accepts = {"title"} }, {"log_toggled", { query({.db = "todos_db", .query = "INSERT INTO activity_log(action, detail) " "VALUES('toggled', 'toggled todo #{{id}}');" })}, .accepts = {"id"} }, {"log_deleted", { query({.db = "todos_db", .query = "INSERT INTO activity_log(action, detail) " "VALUES('deleted', 'removed todo #{{id}}');" })}, .accepts = {"id"} }, {"cleanup_stale", { query( {.db = "todos_db", .query = "UPDATE todos SET done = 1 " "WHERE done = 0 " "AND created_at < datetime('now', '-1 hour');" }, {.db = "todos_db", .query = "INSERT INTO activity_log(action, detail) " "VALUES('cron', 'auto-completed stale todos');" } )}, .cron = "* * * * *" } }, .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'));" } }}, .modules = {sqlite, tailwind} }; }