nerak repo

This commit is contained in:
2026-06-26 12:22:17 -05:00
committed by nightshade
commit 1c1528428a
83 changed files with 5407 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
#include <nerak.h>
config(main){
context("greeting", "hello world");
resource("home", "/", .mime = m_txt,
.get = {
respond("greeting")
}
);
}
+12
View File
@@ -0,0 +1,12 @@
#include <nerak.h>
config(main){
context("greeting", "hello {{name}}");
resource("home", "/", .mime = m_txt,
.get = {
input({"name", .fallback = "world"}),
mustache("greeting", "hello"),
respond("hello")
}
);
}
+19
View File
@@ -0,0 +1,19 @@
#include <nerak.h>
config(main){
context("greeting",
"<html>"
"<head></head>"
"<body>"
"<p>Hello {{name}}</p>"
"</body>"
"</html>"
);
resource("home", "/",
.get = {
input({"name", .fallback = "world"}),
mustache("greeting", "hello"),
respond("hello")
}
);
}
@@ -0,0 +1,4 @@
CREATE TABLE greetings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
@@ -0,0 +1,3 @@
select name
from greetings
limit 1;
@@ -0,0 +1,7 @@
<html>
<body>
{{#hello_world_data}}
<p>Hello {{name}}</p>
{{/hello_world_data}}
</body>
</html>
+19
View File
@@ -0,0 +1,19 @@
#include <nerak.h>
#include <sqlite.h>
config(main){
sqlite_database(
.name = "hello_db",
.connect = "file:hello.db?mode=rwc",
.migrations = {"create_hello_world_table"},
.seeds = {"seed_hello_world"}
);
resource("home", "/",
.get = {
sqlite_query({"hello_db", "get_hello_world", "hello_world_data"}),
mustache("hello_world", "hello"),
respond("hello")
}
);
}
@@ -0,0 +1,2 @@
INSERT INTO greetings(name)
VALUES('World');
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>not found</p>
{{/body}}
{{/layout}}
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>error</p>
{{/body}}
{{/layout}}
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>about us</p>
{{/body}}
{{/layout}}
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>contact us</p>
{{/body}}
{{/layout}}
+2
View File
@@ -0,0 +1,2 @@
insert into todos(user_id, title)
values({{user_id}}, {{title}});
+7
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);
+3
View File
@@ -0,0 +1,3 @@
delete from todos
where user_id = {{user_id}}
and id = {{id}};
+3
View File
@@ -0,0 +1,3 @@
select id, title, finished
from todos
where user_id = {{user_id}};
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>home</p>
{{/body}}
{{/layout}}
+23
View File
@@ -0,0 +1,23 @@
<html>
<head>
<link rel='icon' href='{{asset:favicon.png}}'>
</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>
+64
View File
@@ -0,0 +1,64 @@
#include <nerak.h>
#include <sqlite.h>
#include <session_auth.h>
config(main){
middleware(session());
sqlite_database(
.name ="todos_db",
.connect = "file:{{user_id}}_todo.db?mode=rwc",
.migrations = {"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()},
.get = {
sqlite_query({"todos_db", "get_todos", "todos_data"}),
mustache("todos", "todos_s"),
respond("todos_s")
},
.post = {
input({"title", m_not_empty}),
sqlite_query({"todos_db", "create_todo"}),
redirect("todos")
}
);
resource("todo", "/todos/:id",
.all = {
logged_in(),
input({"id", m_positive})
},
.patch = {
input({"finished", "1", "must be 1", .optional = true,}),
sqlite_query({"todos_db", "update_todo"}),
redirect("todos")
},
.delete = {
sqlite_query({"todos_db", "delete_todo"}),
redirect("todos")
}
);
}
View File
+30
View File
@@ -0,0 +1,30 @@
{{< layout}}
{{$body}}
<form action="{{url: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:todo}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="patch">
{{^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:todo}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="delete">
<button type="submit">delete</button>
</form>
</div>
{{/todos_data}}
{{/body}}
{{/layout}}
+4
View File
@@ -0,0 +1,4 @@
update todos
set finished = {{finished}}
where user_id = {{user_id}}
and id = {{id}};
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>not found</p>
{{/body}}
{{/layout}}
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>error</p>
{{/body}}
{{/layout}}
+5
View File
@@ -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}};
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>home</p>
{{/body}}
{{/layout}}
+25
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>
+68
View File
@@ -0,0 +1,68 @@
#include <nerak.h>
#include <sqlite.h>
#include <datastar.h>
#include <session_auth.h>
config(main){
middleware(session());
sqlite_database(
.name = "todos_db",
.connect = "file:todo.db?mode=rwc",
.migrations = {"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", m_not_empty}),
sqlite_query({"todos_db", "create_todo", "todo_data", .must_exist = true}),
mustache("todo", "todo_s"),
datastar_sse("todos:{{user_id}}", .target = "todos", .mode = mode_prepend, .elements = "todo_s")
}
);
resource("todo", "/todos/:id",
.all = {
logged_in(),
input({"id", m_positive})
},
.patch = {
input({"finished", "1", "must be 1", .optional = true}),
sqlite_query({"todos_db", "update_todo", "todo_data", .must_exist = true}),
mustache("todo", "todo_s"),
datastar_sse("todos:{{user_id}}", .target = "todo_{{id}}", .mode = mode_replace, .elements = "todo_s")
},
.delete = {
sqlite_query({"todos_db", "delete_todo", .must_exist = true}),
datastar_sse("todos:{{user_id}}", .target = "todo_{{id}}", .mode = mode_remove)
}
);
}
+3
View File
@@ -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}}?finished=1')">
{{/finished}}
{{#finished}}
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:todo}}')">
{{/finished}}
{{title}}
<button data-on:click="@delete('{{url:todo}}')">delete</button>
</div>
+19
View File
@@ -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}}') && ($title = '');"
>
<button data-on:click="@post('{{url:todos}}') && ($title = '')">add</button>
<div id="todos" data-init="@get('{{url:todos}}?http_method=sse')">
{{^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;
@@ -0,0 +1,26 @@
#include <nerak.h>
#include <sqlite.h>
#include <pubsub.h>
#include <session_auth.h>
config(activity){
middleware(logged_in(), session());
sqlite_database(
.name = "activity_db",
.connect = "file:activity.db?mode=rwc",
.migrations = {"create_activity_table"}
);
subscribe("todo_created", {
sqlite_query({"activity_db", "insert_activity"})
});
resource("activity", "/activity",
.get = {
sqlite_query({"activity_db", "get_activities", "activity"}),
mustache("activity", "activity_s"),
respond("activity_s")
}
);
}
@@ -0,0 +1,13 @@
{{< layout}}
{{$body}}
{{^activity}}
<p>no activity</p>
{{/activity}}
{{#activity}}
<p>activity</p>
{{#.}}
<p>{{action}}: {{title}} ({{created_at}})</p>
{{/.}}
{{/activity}}
{{/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}});
+15
View File
@@ -0,0 +1,15 @@
#include <nerak.h>
#include <session_auth.h>
#include "todos/todos.c"
#include "activity/activity.c"
config(main){
middleware(session());
resource("home", "/",
.get = {
mustache("home", "home_s"),
respond("home_s")
}
);
}
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>not found</p>
{{/body}}
{{/layout}};
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>error</p>
{{/body}}
{{/layout}};
+5
View File
@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>home</p>
{{/body}}
{{/layout}};
@@ -0,0 +1,20 @@
<html>
<head>
<link rel='icon' href='{{asset:favicon.png}}'>
</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>
</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}};
+45
View File
@@ -0,0 +1,45 @@
#include <nerak.h>
#include <sqlite.h>
#include <pubsub.h>
#include <session_auth.h>
config(todos){
middleware(logged_in(), session());
sqlite_database(
.name = "todos_db",
.connect = "file:todo.db?mode=rwc",
.migrations = {"create_todos_table"}
);
publish("todo_created",
.with = {"user_id", "title"}
);
resource("todos", "/todos",
.get = {
sqlite_query({"todos_db", "get_todos", "todos_data"}),
mustache("todos", "todos_s"),
respond("todos_s")
},
.post = {
input({"title", m_not_empty}),
sqlite_query({"todos_db", "create_todo"}),
emit("todo_created"),
redirect("todos")
}
);
resource("todo", "/todos/:id",
.all = {input({"id", m_positive})},
.patch = {
input({"finished", "1", "must be 1", .optional = true}),
sqlite_query({"todos_db", "update_todo"}),
redirect("todos")
},
.delete = {
sqlite_query({"todos_db", "delete_todo"}),
redirect("todos")
}
);
}
+30
View File
@@ -0,0 +1,30 @@
{{< layout}}
{{$body}}
<form action="{{url: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:todo}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="patch">
{{^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:todo}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="delete">
<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,
loses INTEGER NOT NULL DEFAULT 0
);
@@ -0,0 +1,4 @@
select id, name, sprite
from pokemons
order by random()
limit 2;
@@ -0,0 +1,5 @@
select id, name, sprite, wins, loses,
cast(wins as real) / nullif(loses, 0) as ratio_of_wins_to_loses,
row_number() over (order by (cast(wins as real) / nullif(loses, 0)) desc) as rank
from pokemons
order by ratio_of_wins_to_loses desc;
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<title>Roundest (MaCH 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: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'>
(MaCH Version)
</p>
</div>
<div class='flex flex-row items-center gap-8'>
<a href='{{url: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: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/mach_examples' target='_blank'>GitHub</a>
</footer>
</div>
</body>
</html>
+43
View File
@@ -0,0 +1,43 @@
#include <nerak.h>
#include <htmx.h>
#include <sqlite.h>
#include <tailwind.h>
config(main){
sqlite_database(
.name = "pokemon_db",
.connect = "file::memory:?cache=shared",
.migrations = {"create_pokemons_table"}
);
resource("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"));
}),
mustache("home", "home_s"),
respond("home_s")
},
.post = {
input(
{"winner", m_positive},
{"loser", m_positive}
),
sqlite_query({"pokemon_db", "vote"}),
reroute("home")
}
);
resource("result", "/results",
.get = {
sqlite_query({"pokemon_db", "get_results", "results"}),
mustache("results", "results_s"),
respond("results_s")
}
);
}
@@ -0,0 +1,28 @@
{{< home}}
{{$body}}
<div class='container mx-auto px-4 py-8 text-white'>
<div class='grid gap-4'>
{{#results}}
<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'>
{{ratio_of_wins_to_loses}}
</div>
<div class='text-sm text-gray-400'>
{{wins}}W - {{loses}}L
</div>
</div>
</div>
{{/results}}
</div>
</div>
{{/body}}
{{/home}}
@@ -0,0 +1,8 @@
begin transaction;
update pokemons
set wins = wins + 1
where id = {{winner}};
update pokemons
set loses = loses + 1
where id = {{loser}};
commit;