MaCH repo
13
00_hello_text/main.c
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <mach.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", .mime = mime_txt,
|
||||
.get = {
|
||||
render(.template= "hello")
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
155
01-multi-reactor-architecture.svg
Normal file
@@ -0,0 +1,155 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 920 580" font-family="system-ui, -apple-system, sans-serif">
|
||||
<defs>
|
||||
<marker id="arrow" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
|
||||
<path d="M0,0 L8,3 L0,6" fill="#64748b"/>
|
||||
</marker>
|
||||
<marker id="arrow-amber" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
|
||||
<path d="M0,0 L8,3 L0,6" fill="#f59e0b"/>
|
||||
</marker>
|
||||
<marker id="arrow-emerald" markerWidth="8" markerHeight="6" refX="8" refY="3" orient="auto">
|
||||
<path d="M0,0 L8,3 L0,6" fill="#10b981"/>
|
||||
</marker>
|
||||
</defs>
|
||||
|
||||
<!-- Background -->
|
||||
<rect width="920" height="580" rx="12" fill="#0f172a"/>
|
||||
|
||||
<!-- Title -->
|
||||
<text x="460" y="38" text-anchor="middle" fill="#f8fafc" font-size="18" font-weight="600">Multi-Reactor Architecture</text>
|
||||
|
||||
<!-- Subtitle -->
|
||||
<text x="460" y="58" text-anchor="middle" fill="#475569" font-size="10">request/task/cpu ratio configurable in compose.yml</text>
|
||||
|
||||
<!-- HTTP Clients -->
|
||||
<rect x="20" y="105" width="110" height="44" rx="8" fill="#1e293b" stroke="#334155" stroke-width="1.5"/>
|
||||
<text x="75" y="132" text-anchor="middle" fill="#94a3b8" font-size="13" font-weight="500">HTTP Clients</text>
|
||||
|
||||
<!-- Arrows from clients to request reactors -->
|
||||
<line x1="130" y1="117" x2="178" y2="117" stroke="#64748b" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
<line x1="130" y1="127" x2="178" y2="185" stroke="#64748b" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
<line x1="130" y1="137" x2="178" y2="255" stroke="#64748b" stroke-width="1.5" marker-end="url(#arrow)"/>
|
||||
|
||||
<!-- ==================== REQUEST REACTORS ==================== -->
|
||||
<rect x="173" y="76" width="270" height="228" rx="10" fill="none" stroke="#334155" stroke-width="1" stroke-dasharray="6,4"/>
|
||||
<text x="308" y="96" text-anchor="middle" fill="#60a5fa" font-size="12" font-weight="600" letter-spacing="0.5">REQUEST REACTORS</text>
|
||||
|
||||
<!-- Request Reactor 0 -->
|
||||
<rect x="188" y="106" width="240" height="40" rx="8" fill="#1e3a5f" stroke="#3b82f6" stroke-width="1.5"/>
|
||||
<text x="203" y="131" fill="#93c5fd" font-size="11" font-weight="600">Core 0</text>
|
||||
<text x="260" y="131" fill="#cbd5e1" font-size="11">event loop</text>
|
||||
<rect x="348" y="114" width="72" height="24" rx="5" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<text x="384" y="131" text-anchor="middle" fill="#94a3b8" font-size="10">pipeline</text>
|
||||
|
||||
<!-- Request Reactor 1 -->
|
||||
<rect x="188" y="158" width="240" height="40" rx="8" fill="#1e3a5f" stroke="#3b82f6" stroke-width="1.5"/>
|
||||
<text x="203" y="183" fill="#93c5fd" font-size="11" font-weight="600">Core 1</text>
|
||||
<text x="260" y="183" fill="#cbd5e1" font-size="11">event loop</text>
|
||||
<rect x="348" y="166" width="72" height="24" rx="5" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<text x="384" y="183" text-anchor="middle" fill="#94a3b8" font-size="10">pipeline</text>
|
||||
|
||||
<!-- Ellipsis -->
|
||||
<circle cx="308" cy="212" r="2" fill="#475569"/>
|
||||
<circle cx="308" cy="222" r="2" fill="#475569"/>
|
||||
|
||||
<!-- Request Reactor N -->
|
||||
<rect x="188" y="240" width="240" height="40" rx="8" fill="#1e3a5f" stroke="#3b82f6" stroke-width="1.5"/>
|
||||
<text x="203" y="265" fill="#93c5fd" font-size="11" font-weight="600">Core N</text>
|
||||
<text x="260" y="265" fill="#cbd5e1" font-size="11">event loop</text>
|
||||
<rect x="348" y="248" width="72" height="24" rx="5" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<text x="384" y="265" text-anchor="middle" fill="#94a3b8" font-size="10">pipeline</text>
|
||||
|
||||
<!-- ==================== TASK REACTORS ==================== -->
|
||||
<rect x="173" y="330" width="270" height="228" rx="10" fill="none" stroke="#334155" stroke-width="1" stroke-dasharray="6,4"/>
|
||||
<text x="308" y="350" text-anchor="middle" fill="#fbbf24" font-size="12" font-weight="600" letter-spacing="0.5">TASK REACTORS</text>
|
||||
|
||||
<!-- Task Reactor 0 -->
|
||||
<rect x="188" y="360" width="240" height="40" rx="8" fill="#422006" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<text x="203" y="385" fill="#fcd34d" font-size="11" font-weight="600">Core</text>
|
||||
<text x="236" y="385" fill="#cbd5e1" font-size="11">event loop</text>
|
||||
<rect x="348" y="368" width="72" height="24" rx="5" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<text x="384" y="385" text-anchor="middle" fill="#94a3b8" font-size="10">cron / jobs</text>
|
||||
|
||||
<!-- Task Reactor 1 -->
|
||||
<rect x="188" y="412" width="240" height="40" rx="8" fill="#422006" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<text x="203" y="437" fill="#fcd34d" font-size="11" font-weight="600">Core</text>
|
||||
<text x="236" y="437" fill="#cbd5e1" font-size="11">event loop</text>
|
||||
<rect x="348" y="420" width="72" height="24" rx="5" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<text x="384" y="437" text-anchor="middle" fill="#94a3b8" font-size="10">cron / jobs</text>
|
||||
|
||||
<!-- Ellipsis -->
|
||||
<circle cx="308" cy="466" r="2" fill="#475569"/>
|
||||
<circle cx="308" cy="476" r="2" fill="#475569"/>
|
||||
|
||||
<!-- Task Reactor N -->
|
||||
<rect x="188" y="494" width="240" height="40" rx="8" fill="#422006" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<text x="203" y="519" fill="#fcd34d" font-size="11" font-weight="600">Core</text>
|
||||
<text x="236" y="519" fill="#cbd5e1" font-size="11">event loop</text>
|
||||
<rect x="348" y="502" width="72" height="24" rx="5" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<text x="384" y="519" text-anchor="middle" fill="#94a3b8" font-size="10">cron / jobs</text>
|
||||
|
||||
<!-- Task DB -->
|
||||
<rect x="20" y="410" width="110" height="44" rx="8" fill="#1e293b" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<text x="75" y="429" text-anchor="middle" fill="#fcd34d" font-size="11" font-weight="600">mach_tasks</text>
|
||||
<text x="75" y="444" text-anchor="middle" fill="#94a3b8" font-size="10">database</text>
|
||||
|
||||
<!-- Arrows: task DB to/from task reactors -->
|
||||
<line x1="130" y1="425" x2="183" y2="425" stroke="#f59e0b" stroke-width="1.5" marker-end="url(#arrow-amber)"/>
|
||||
<line x1="183" y1="437" x2="130" y2="437" stroke="#f59e0b" stroke-width="1.5" marker-end="url(#arrow-amber)"/>
|
||||
|
||||
<!-- ==================== SHARED THREAD POOL ==================== -->
|
||||
<rect x="540" y="76" width="350" height="270" rx="10" fill="none" stroke="#334155" stroke-width="1" stroke-dasharray="6,4"/>
|
||||
<text x="715" y="100" text-anchor="middle" fill="#34d399" font-size="12" font-weight="600" letter-spacing="0.5">SHARED THREAD POOL</text>
|
||||
<text x="715" y="116" text-anchor="middle" fill="#6b7280" font-size="10">remaining cores</text>
|
||||
|
||||
<!-- Work queue label -->
|
||||
<text x="715" y="140" text-anchor="middle" fill="#94a3b8" font-size="10">work queue</text>
|
||||
|
||||
<!-- Queue visualization -->
|
||||
<rect x="568" y="148" width="294" height="36" rx="6" fill="#1e293b" stroke="#475569" stroke-width="1"/>
|
||||
<rect x="576" y="154" width="24" height="24" rx="4" fill="#064e3b" stroke="#10b981" stroke-width="1"/>
|
||||
<rect x="606" y="154" width="24" height="24" rx="4" fill="#064e3b" stroke="#10b981" stroke-width="1"/>
|
||||
<rect x="636" y="154" width="24" height="24" rx="4" fill="#064e3b" stroke="#10b981" stroke-width="1"/>
|
||||
<rect x="666" y="154" width="24" height="24" rx="4" fill="#064e3b" stroke="#10b981" stroke-width="1"/>
|
||||
<rect x="696" y="154" width="24" height="24" rx="4" fill="#0f172a" stroke="#334155" stroke-width="1" stroke-dasharray="3,2"/>
|
||||
<rect x="726" y="154" width="24" height="24" rx="4" fill="#0f172a" stroke="#334155" stroke-width="1" stroke-dasharray="3,2"/>
|
||||
<rect x="756" y="154" width="24" height="24" rx="4" fill="#0f172a" stroke="#334155" stroke-width="1" stroke-dasharray="3,2"/>
|
||||
<rect x="786" y="154" width="24" height="24" rx="4" fill="#0f172a" stroke="#334155" stroke-width="1" stroke-dasharray="3,2"/>
|
||||
<rect x="816" y="154" width="24" height="24" rx="4" fill="#0f172a" stroke="#334155" stroke-width="1" stroke-dasharray="3,2"/>
|
||||
|
||||
<!-- Arrows from queue down to threads -->
|
||||
<line x1="630" y1="184" x2="630" y2="214" stroke="#475569" stroke-width="1" marker-end="url(#arrow)"/>
|
||||
<line x1="720" y1="184" x2="720" y2="214" stroke="#475569" stroke-width="1" marker-end="url(#arrow)"/>
|
||||
<line x1="810" y1="184" x2="810" y2="214" stroke="#475569" stroke-width="1" marker-end="url(#arrow)"/>
|
||||
|
||||
<!-- Thread boxes -->
|
||||
<rect x="570" y="218" width="110" height="48" rx="6" fill="#064e3b" stroke="#10b981" stroke-width="1.5"/>
|
||||
<text x="625" y="247" text-anchor="middle" fill="#6ee7b7" font-size="11" font-weight="500">Thread 1</text>
|
||||
|
||||
<rect x="690" y="218" width="60" height="48" rx="6" fill="#064e3b" stroke="#10b981" stroke-width="1.5"/>
|
||||
<text x="720" y="247" text-anchor="middle" fill="#6ee7b7" font-size="11" font-weight="500">...</text>
|
||||
|
||||
<rect x="760" y="218" width="110" height="48" rx="6" fill="#064e3b" stroke="#10b981" stroke-width="1.5"/>
|
||||
<text x="815" y="247" text-anchor="middle" fill="#6ee7b7" font-size="11" font-weight="500">Thread N</text>
|
||||
|
||||
<!-- Result return -->
|
||||
<text x="715" y="298" text-anchor="middle" fill="#6b7280" font-size="10">on complete, resumes pipeline on reactor</text>
|
||||
|
||||
<!-- ==================== INVOKE ARROWS ==================== -->
|
||||
<!-- Arrow from request reactors to pool -->
|
||||
<path d="M 443,185 L 535,168" fill="none" stroke="#10b981" stroke-width="1.5" marker-end="url(#arrow-emerald)" stroke-dasharray="5,3"/>
|
||||
<text x="476" y="164" fill="#34d399" font-size="10" font-weight="500">invoke()</text>
|
||||
|
||||
<!-- Arrow from task reactors to pool -->
|
||||
<path d="M 443,430 Q 510,390 535,310" fill="none" stroke="#10b981" stroke-width="1.5" marker-end="url(#arrow-emerald)" stroke-dasharray="5,3"/>
|
||||
<text x="518" y="390" fill="#34d399" font-size="10" font-weight="500">invoke()</text>
|
||||
|
||||
<!-- ==================== TASK ARROWS ==================== -->
|
||||
<!-- task() from request reactors to task DB -->
|
||||
<path d="M 188,286 Q 110,320 85,405" fill="none" stroke="#f59e0b" stroke-width="1.5" marker-end="url(#arrow-amber)" stroke-dasharray="5,3"/>
|
||||
<text x="108" y="330" fill="#fbbf24" font-size="10" font-weight="500">task()</text>
|
||||
|
||||
<!-- task() from task reactors to task DB (self-enqueue) -->
|
||||
<path d="M 188,540 Q 130,556 85,458" fill="none" stroke="#f59e0b" stroke-width="1.5" marker-end="url(#arrow-amber)" stroke-dasharray="5,3"/>
|
||||
<text x="120" y="540" fill="#fbbf24" font-size="10" font-weight="500">task()</text>
|
||||
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 10 KiB |
18
01_hello_world_text/main.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <mach.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", .mime = mime_txt,
|
||||
.get = {
|
||||
validate({"name",
|
||||
.validation = "^\\S{1,16}$",
|
||||
.fallback = "world",
|
||||
.message = "must be 1-16 characters, no spaces"
|
||||
}),
|
||||
render(.template = "Hello {{name}}")
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
95
02-request-pipeline-flow.svg
Normal file
@@ -0,0 +1,95 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 300" width="760" height="300">
|
||||
<rect width="760" height="300" fill="#09080f"/>
|
||||
<defs>
|
||||
<linearGradient id="sg1" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0%" stop-color="#3b82f6"/>
|
||||
<stop offset="100%" stop-color="#2563eb"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="sg2" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0%" stop-color="#8b5cf6"/>
|
||||
<stop offset="100%" stop-color="#7c3aed"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="sg3" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0%" stop-color="#f59e0b"/>
|
||||
<stop offset="100%" stop-color="#d97706"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="sg4" x1="0" y1="0" x2="1" y2="1">
|
||||
<stop offset="0%" stop-color="#10b981"/>
|
||||
<stop offset="100%" stop-color="#059669"/>
|
||||
</linearGradient>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<!-- Outer container -->
|
||||
<rect x="20" y="16" width="720" height="268" rx="14" fill="#0f0d1a" stroke="#1e1b2e" stroke-width="1.5"/>
|
||||
|
||||
<!-- Route label -->
|
||||
<text x="44" y="46" fill="#94a3b8" font-size="12" font-weight="600">POST /todos</text>
|
||||
|
||||
<!-- .before label -->
|
||||
<text x="44" y="72" fill="#475569" font-size="9">.before</text>
|
||||
|
||||
<!-- session() box: x=44 w=108 → right edge 152 -->
|
||||
<rect x="44" y="80" width="108" height="40" rx="8" fill="#1e293b" stroke="#3b82f6" stroke-width="1"/>
|
||||
<text x="98" y="105" text-anchor="middle" fill="#93c5fd" font-size="11" font-weight="600">session()</text>
|
||||
|
||||
<!-- Arrow 1: line 152→170, triangle tip at 180 -->
|
||||
<line x1="152" y1="100" x2="170" y2="100" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="180,100 170,95 170,105" fill="#64748b"/>
|
||||
|
||||
<!-- .post pipeline label -->
|
||||
<text x="188" y="72" fill="#475569" font-size="9">.post pipeline</text>
|
||||
|
||||
<!-- param() box: x=188 w=108 → right edge 296 -->
|
||||
<rect x="188" y="80" width="108" height="40" rx="8" fill="url(#sg1)"/>
|
||||
<text x="242" y="98" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">param()</text>
|
||||
<text x="242" y="113" text-anchor="middle" fill="rgba(255,255,255,0.65)" font-size="8">validate title</text>
|
||||
|
||||
<!-- Arrow 2: line 296→314, triangle tip at 324 -->
|
||||
<line x1="296" y1="100" x2="314" y2="100" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="324,100 314,95 314,105" fill="#64748b"/>
|
||||
|
||||
<!-- db() box: x=324 w=108 → right edge 432 -->
|
||||
<rect x="324" y="80" width="108" height="40" rx="8" fill="url(#sg2)"/>
|
||||
<text x="378" y="98" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">db()</text>
|
||||
<text x="378" y="113" text-anchor="middle" fill="rgba(255,255,255,0.65)" font-size="8">INSERT todo</text>
|
||||
|
||||
<!-- Arrow 3: line 432→450, triangle tip at 460 -->
|
||||
<line x1="432" y1="100" x2="450" y2="100" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="460,100 450,95 450,105" fill="#64748b"/>
|
||||
|
||||
<!-- emit() box: x=460 w=108 → right edge 568 -->
|
||||
<rect x="460" y="80" width="108" height="40" rx="8" fill="url(#sg3)"/>
|
||||
<text x="514" y="98" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">emit()</text>
|
||||
<text x="514" y="113" text-anchor="middle" fill="rgba(255,255,255,0.65)" font-size="8">todo_created</text>
|
||||
|
||||
<!-- Arrow 4: line 568→586, triangle tip at 596 -->
|
||||
<line x1="568" y1="100" x2="586" y2="100" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="596,100 586,95 586,105" fill="#64748b"/>
|
||||
|
||||
<!-- redirect() box: x=596 w=108 → right edge 704 -->
|
||||
<rect x="596" y="80" width="108" height="40" rx="8" fill="url(#sg4)"/>
|
||||
<text x="650" y="98" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">redirect()</text>
|
||||
<text x="650" y="113" text-anchor="middle" fill="rgba(255,255,255,0.65)" font-size="8">302 → /todos</text>
|
||||
|
||||
<!-- ===== Pipeline Context ===== -->
|
||||
<rect x="44" y="146" width="672" height="120" rx="10" fill="#111827" stroke="#1f2937" stroke-width="1"/>
|
||||
<text x="64" y="170" fill="#6b7280" font-size="10" font-weight="600">PIPELINE CONTEXT</text>
|
||||
|
||||
<!-- Context cards: each 148w x 52h, gap 12 -->
|
||||
<rect x="60" y="182" width="148" height="52" rx="6" fill="#1e1b2e" stroke="#3b82f6" stroke-width="0.75"/>
|
||||
<text x="134" y="206" text-anchor="middle" fill="#e2e8f0" font-size="11" font-weight="600">user_id</text>
|
||||
<text x="134" y="222" text-anchor="middle" fill="#6b7280" font-size="9">← session</text>
|
||||
|
||||
<rect x="220" y="182" width="148" height="52" rx="6" fill="#1e1b2e" stroke="#3b82f6" stroke-width="0.75"/>
|
||||
<text x="294" y="206" text-anchor="middle" fill="#e2e8f0" font-size="11" font-weight="600">title</text>
|
||||
<text x="294" y="222" text-anchor="middle" fill="#6b7280" font-size="9">← param</text>
|
||||
|
||||
<rect x="380" y="182" width="148" height="52" rx="6" fill="#1e1b2e" stroke="#8b5cf6" stroke-width="0.75"/>
|
||||
<text x="454" y="206" text-anchor="middle" fill="#e2e8f0" font-size="11" font-weight="600">todo</text>
|
||||
<text x="454" y="222" text-anchor="middle" fill="#6b7280" font-size="9">← db .set</text>
|
||||
|
||||
<rect x="540" y="182" width="148" height="52" rx="6" fill="#1e1b2e" stroke="#374151" stroke-width="0.75"/>
|
||||
<text x="614" y="206" text-anchor="middle" fill="#e2e8f0" font-size="11" font-weight="600">base_layout</text>
|
||||
<text x="614" y="222" text-anchor="middle" fill="#6b7280" font-size="9">← .context</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.2 KiB |
25
02_hello_world_html/main.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <mach.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/",
|
||||
.get = {
|
||||
validate({"name",
|
||||
.validation = "^\\S{1,16}$",
|
||||
.fallback = "world",
|
||||
.message = "must be 1-16 characters, no spaces"
|
||||
}),
|
||||
render(.template =
|
||||
"<html>"
|
||||
"<head></head>"
|
||||
"<body>"
|
||||
"<p>Hello {{name}}</p>"
|
||||
"</body>"
|
||||
"</html>"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
66
03-event-pub-sub.svg
Normal file
@@ -0,0 +1,66 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 300" width="900" height="300">
|
||||
<rect width="900" height="300" fill="#09080f"/>
|
||||
<defs>
|
||||
<linearGradient id="pubG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#f59e0b"/>
|
||||
<stop offset="100%" stop-color="#d97706"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="subG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#06b6d4"/>
|
||||
<stop offset="100%" stop-color="#0891b2"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="busG" x1="0" y1="0" x2="1" y2="0">
|
||||
<stop offset="0%" stop-color="#7c3aed"/>
|
||||
<stop offset="50%" stop-color="#a855f7"/>
|
||||
<stop offset="100%" stop-color="#7c3aed"/>
|
||||
</linearGradient>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<!-- ===== Publisher: todos module ===== -->
|
||||
<rect x="30" y="30" width="220" height="130" rx="12" fill="#1e1b2e" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<rect x="30" y="30" width="220" height="36" rx="12" fill="url(#pubG)"/>
|
||||
<text x="140" y="53" text-anchor="middle" fill="#fff" font-size="12" font-weight="700">todos module</text>
|
||||
<text x="50" y="86" fill="#fcd34d" font-size="10">.post pipeline:</text>
|
||||
<text x="58" y="102" fill="#e2e8f0" font-size="10">param() → db()</text>
|
||||
<text x="58" y="118" fill="#e2e8f0" font-size="10">→ emit("todo_created")</text>
|
||||
<text x="58" y="134" fill="#e2e8f0" font-size="10">→ redirect("/todos")</text>
|
||||
|
||||
<!-- .publishes contract -->
|
||||
<text x="140" y="175" text-anchor="middle" fill="#92400e" font-size="9" font-weight="600">.publishes</text>
|
||||
<rect x="40" y="182" width="200" height="28" rx="6" fill="#451a03" stroke="#f59e0b" stroke-width="0.75"/>
|
||||
<text x="140" y="201" text-anchor="middle" fill="#fcd34d" font-size="9">todo_created → user_id, title</text>
|
||||
|
||||
<!-- ===== Event Bus pill ===== -->
|
||||
<rect x="330" y="118" width="140" height="44" rx="22" fill="url(#busG)"/>
|
||||
<text x="400" y="145" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">Event Bus</text>
|
||||
|
||||
<!-- Arrow: publisher → bus -->
|
||||
<line x1="250" y1="140" x2="320" y2="140" stroke="#f59e0b" stroke-width="2"/>
|
||||
<polygon points="330,140 320,135 320,145" fill="#f59e0b"/>
|
||||
|
||||
<!-- ===== Subscriber 1: activity module ===== -->
|
||||
<rect x="570" y="20" width="300" height="100" rx="12" fill="#1e1b2e" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<rect x="570" y="20" width="300" height="36" rx="12" fill="url(#subG)"/>
|
||||
<text x="720" y="43" text-anchor="middle" fill="#fff" font-size="12" font-weight="700">activity module</text>
|
||||
<text x="595" y="74" fill="#67e8f9" font-size="10">.events:</text>
|
||||
<text x="603" y="92" fill="#e2e8f0" font-size="9">"todo_created" → db(log.sql)</text>
|
||||
|
||||
<!-- Elbow: bus right → subscriber 1 -->
|
||||
<path d="M470,140 L525,140 L525,70 L560,70" stroke="#06b6d4" stroke-width="1.5" fill="none"/>
|
||||
<polygon points="570,70 560,65 560,75" fill="#06b6d4"/>
|
||||
|
||||
<!-- ===== Subscriber 2: notifications module ===== -->
|
||||
<rect x="570" y="160" width="300" height="100" rx="12" fill="#1e1b2e" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<rect x="570" y="160" width="300" height="36" rx="12" fill="url(#subG)"/>
|
||||
<text x="720" y="183" text-anchor="middle" fill="#fff" font-size="12" font-weight="700">notifications module</text>
|
||||
<text x="595" y="214" fill="#67e8f9" font-size="10">.events:</text>
|
||||
<text x="603" y="232" fill="#e2e8f0" font-size="9">"todo_created" → call(notify)</text>
|
||||
|
||||
<!-- Elbow: bus right → subscriber 2 -->
|
||||
<path d="M470,140 L525,140 L525,210 L560,210" stroke="#06b6d4" stroke-width="1.5" fill="none"/>
|
||||
<polygon points="570,210 560,205 560,215" fill="#06b6d4"/>
|
||||
|
||||
<!-- Footer -->
|
||||
<text x="450" y="290" text-anchor="middle" fill="#64748b" font-size="10">Modules are decoupled — publishers don't know about subscribers</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.8 KiB |
48
03_hello_world_html_db/main.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/",
|
||||
.get = {
|
||||
query({
|
||||
.set_key = "greeting",
|
||||
.db = "hello_db",
|
||||
.query =
|
||||
"select name "
|
||||
"from greetings "
|
||||
"limit 1;"
|
||||
}),
|
||||
render(.template =
|
||||
"<html>"
|
||||
"<body>"
|
||||
"{{#greeting}}"
|
||||
"<p>Hello {{name}}</p>"
|
||||
"{{/greeting}}"
|
||||
"</body>"
|
||||
"</html>"
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "hello_db",
|
||||
.connect = "file:hello.db?mode=rwc",
|
||||
.migrations = {
|
||||
"CREATE TABLE greetings ("
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"name TEXT NOT NULL"
|
||||
");"
|
||||
},
|
||||
.seeds = {
|
||||
"INSERT INTO greetings(name)"
|
||||
"VALUES('World');"
|
||||
}
|
||||
}},
|
||||
|
||||
.modules = {sqlite}
|
||||
};
|
||||
}
|
||||
46
04-error-resolution.svg
Normal file
@@ -0,0 +1,46 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 320" width="760" height="320">
|
||||
<rect width="760" height="320" fill="#09080f"/>
|
||||
<defs>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<!-- Level 1: Root Config — y=24 h=60 → bottom 84 -->
|
||||
<rect x="40" y="24" width="680" height="60" rx="10" fill="#2d2640" stroke="#7c3aed" stroke-width="1.5"/>
|
||||
<text x="380" y="50" text-anchor="middle" fill="#c084fc" font-size="12" font-weight="700">Root Config</text>
|
||||
<text x="380" y="70" text-anchor="middle" fill="#94a3b8" font-size="10">.errors = { 404 → render 404.html }</text>
|
||||
|
||||
<!-- Gap: 84 → 118 (34px gap) -->
|
||||
|
||||
<!-- Level 2: Module Config — y=118 h=60 → bottom 178 -->
|
||||
<rect x="120" y="118" width="520" height="60" rx="10" fill="#1e293b" stroke="#3b82f6" stroke-width="1.5" stroke-dasharray="5,3"/>
|
||||
<text x="380" y="144" text-anchor="middle" fill="#60a5fa" font-size="12" font-weight="700">Module Config</text>
|
||||
<text x="380" y="164" text-anchor="middle" fill="#94a3b8" font-size="10">.errors = { ... }</text>
|
||||
|
||||
<!-- Gap: 178 → 212 (34px gap) -->
|
||||
|
||||
<!-- Level 3: Route — y=212 h=60 → bottom 272 -->
|
||||
<rect x="200" y="212" width="360" height="60" rx="10" fill="#0c2d3e" stroke="#06b6d4" stroke-width="1.5" stroke-dasharray="5,3"/>
|
||||
<text x="380" y="238" text-anchor="middle" fill="#22d3ee" font-size="12" font-weight="700">Route /todos/:id</text>
|
||||
<text x="380" y="258" text-anchor="middle" fill="#94a3b8" font-size="10">.errors = { ... }</text>
|
||||
|
||||
<!-- Error origin dot: y=296 -->
|
||||
<circle cx="380" cy="296" r="10" fill="#ef4444" opacity="0.85"/>
|
||||
<text x="380" y="300" text-anchor="middle" fill="#fff" font-size="10" font-weight="700">!</text>
|
||||
<text x="400" y="300" fill="#fca5a5" font-size="10"> 404 raised in pipeline step</text>
|
||||
|
||||
<!-- Arrow 1: error dot (286) → route bottom (272). Line + up triangle -->
|
||||
<line x1="380" y1="286" x2="380" y2="275" stroke="#ef4444" stroke-width="2"/>
|
||||
<polygon points="380,272 375,279 385,279" fill="#ef4444"/>
|
||||
|
||||
<!-- Arrow 2: route top (212) → module bottom (178). Line in gap + up triangle -->
|
||||
<line x1="380" y1="212" x2="380" y2="181" stroke="#ef4444" stroke-width="2"/>
|
||||
<polygon points="380,178 375,185 385,185" fill="#ef4444"/>
|
||||
<!-- Label -->
|
||||
<text x="400" y="200" fill="#6b7280" font-size="9">no 404 handler → bubble up</text>
|
||||
|
||||
<!-- Arrow 3: module top (118) → root bottom (84). Line in gap + up triangle -->
|
||||
<line x1="380" y1="118" x2="380" y2="87" stroke="#ef4444" stroke-width="2"/>
|
||||
<polygon points="380,84 375,91 385,91" fill="#ef4444"/>
|
||||
<!-- Label -->
|
||||
<text x="400" y="106" fill="#22c55e" font-size="9" font-weight="600">✓ matched → render 404.html</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.7 KiB |
5
04_todo/404.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
04_todo/5xx.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
04_todo/about.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
04_todo/contact.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
2
04_todo/create_todo.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}});
|
||||
7
04_todo/create_todos_table.sql
Normal 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
04_todo/delete_todo.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
3
04_todo/get_todos.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
5
04_todo/home.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
23
04_todo/layout.mustache.html
Normal 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>
|
||||
132
04_todo/main.c
Normal file
@@ -0,0 +1,132 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", {session()},
|
||||
.get = {
|
||||
render("home")
|
||||
}
|
||||
},
|
||||
|
||||
{"about", "/about", {session()},
|
||||
.get = {
|
||||
render("about")
|
||||
}
|
||||
},
|
||||
|
||||
{"contact", "/contact", {session()},
|
||||
.get = {
|
||||
render("contact")
|
||||
}
|
||||
},
|
||||
|
||||
{"todos", "/todos", {logged_in()},
|
||||
.get = {
|
||||
query({"get_todos",
|
||||
.set_key = "todos",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
render("todos")
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate({"title",
|
||||
.validation = "^\\S{1,16}$",
|
||||
.message = "must be 1-16 characters, no spaces"
|
||||
}),
|
||||
query({"create_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
}
|
||||
},
|
||||
|
||||
{"todo", "/todos/:id", {
|
||||
logged_in(),
|
||||
validate({"id",
|
||||
.validation = "^\\d{1,10}$",
|
||||
.message = "must be between 1-9999999999"
|
||||
})},
|
||||
|
||||
.patch = {
|
||||
validate({"finished",
|
||||
.optional = true,
|
||||
.validation = "1",
|
||||
.message = "must be 1"
|
||||
}),
|
||||
find({"update_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
},
|
||||
|
||||
.delete = {
|
||||
find({"delete_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.errors = {
|
||||
{http_error, {
|
||||
render("5xx")
|
||||
}},
|
||||
|
||||
{http_not_found, {
|
||||
render("404")
|
||||
}}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"layout", (asset){
|
||||
#embed "layout.mustache.html"
|
||||
}},
|
||||
{"home", (asset){
|
||||
#embed "home.mustache.html"
|
||||
}},
|
||||
{"about", (asset){
|
||||
#embed "about.mustache.html"
|
||||
}},
|
||||
{"contact", (asset){
|
||||
#embed "contact.mustache.html"
|
||||
}},
|
||||
{"5xx", (asset){
|
||||
#embed "5xx.mustache.html"
|
||||
}},
|
||||
{"404", (asset){
|
||||
#embed "404.mustache.html"
|
||||
}},
|
||||
{"todos", (asset){
|
||||
#embed "todos.mustache.html"
|
||||
}},
|
||||
{"get_todos", (asset){
|
||||
#embed "get_todos.sql"
|
||||
}},
|
||||
{"create_todo", (asset){
|
||||
#embed "create_todo.sql"
|
||||
}},
|
||||
{"update_todo", (asset){
|
||||
#embed "update_todo.sql"
|
||||
}},
|
||||
{"delete_todo", (asset){
|
||||
#embed "delete_todo.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "todos_db",
|
||||
.connect = "file:{{user_id}}_todo.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_todos_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
0
04_todo/public/favicon.png
Normal file
32
04_todo/todos.mustache.html
Normal file
@@ -0,0 +1,32 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<form action="{{url:todos}}" method="post">
|
||||
<input type="text" name="title" placeholder="new todo" required>
|
||||
<button type="submit">add</button>
|
||||
</form>
|
||||
{{^todos}}
|
||||
<p>no todos</p>
|
||||
{{/todos}}
|
||||
{{#todos}}
|
||||
{{#.}}
|
||||
<div>
|
||||
<form action="{{url:todo:id}}" 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:id}}" method="post" style="display:inline">
|
||||
<input type="hidden" name="http_method" value="delete">
|
||||
<button type="submit">delete</button>
|
||||
</form>
|
||||
</div>
|
||||
{{/.}}
|
||||
{{/todos}}
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
4
04_todo/update_todo.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
58
05-app-composition-tree.svg
Normal file
@@ -0,0 +1,58 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 270" width="760" height="270">
|
||||
<rect width="760" height="270" fill="#09080f"/>
|
||||
<defs>
|
||||
<linearGradient id="rG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#a855f7"/>
|
||||
<stop offset="100%" stop-color="#7c3aed"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="mG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#3b82f6"/>
|
||||
<stop offset="100%" stop-color="#2563eb"/>
|
||||
</linearGradient>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<!-- Root node: x=290 y=20 w=180 h=44 → center 380, bottom 64 -->
|
||||
<rect x="290" y="20" width="180" height="44" rx="10" fill="url(#rG)"/>
|
||||
<text x="380" y="47" text-anchor="middle" fill="#fff" font-size="12" font-weight="700">mach() — root</text>
|
||||
|
||||
<!-- Elbow connectors: root bottom center → down to 82 → horizontal → down to child tops at 108 -->
|
||||
<!-- Vertical trunk from root -->
|
||||
<line x1="380" y1="64" x2="380" y2="82" stroke="#64748b" stroke-width="1.5"/>
|
||||
<!-- Horizontal bar -->
|
||||
<line x1="130" y1="82" x2="630" y2="82" stroke="#64748b" stroke-width="1.5"/>
|
||||
<!-- Vertical drops to each child -->
|
||||
<line x1="130" y1="82" x2="130" y2="108" stroke="#64748b" stroke-width="1.5"/>
|
||||
<line x1="380" y1="82" x2="380" y2="108" stroke="#64748b" stroke-width="1.5"/>
|
||||
<line x1="630" y1="82" x2="630" y2="108" stroke="#64748b" stroke-width="1.5"/>
|
||||
|
||||
<!-- Level 1: each w=170 h=40, top at y=108 → bottom at 148 -->
|
||||
<rect x="45" y="108" width="170" height="40" rx="8" fill="url(#mG)"/>
|
||||
<text x="130" y="133" text-anchor="middle" fill="#fff" font-size="11" font-weight="600">sqlite_config()</text>
|
||||
|
||||
<rect x="295" y="108" width="170" height="40" rx="8" fill="url(#mG)"/>
|
||||
<text x="380" y="133" text-anchor="middle" fill="#fff" font-size="11" font-weight="600">todos_config()</text>
|
||||
|
||||
<rect x="545" y="108" width="170" height="40" rx="8" fill="url(#mG)"/>
|
||||
<text x="630" y="133" text-anchor="middle" fill="#fff" font-size="11" font-weight="600">auth_config()</text>
|
||||
|
||||
<!-- Elbow connectors: todos bottom center → down to 162 → horizontal → down to leaf tops at 178 -->
|
||||
<line x1="380" y1="148" x2="380" y2="162" stroke="#64748b" stroke-width="1"/>
|
||||
<line x1="260" y1="162" x2="500" y2="162" stroke="#64748b" stroke-width="1"/>
|
||||
<line x1="260" y1="162" x2="260" y2="178" stroke="#64748b" stroke-width="1"/>
|
||||
<line x1="500" y1="162" x2="500" y2="178" stroke="#64748b" stroke-width="1"/>
|
||||
|
||||
<!-- Level 2 leaf nodes: w=170 h=64, top at y=178 -->
|
||||
<rect x="175" y="178" width="170" height="64" rx="8" fill="#0f0d1a" stroke="#10b981" stroke-width="1"/>
|
||||
<text x="260" y="200" text-anchor="middle" fill="#6ee7b7" font-size="10">.databases</text>
|
||||
<text x="260" y="216" text-anchor="middle" fill="#6ee7b7" font-size="10">.resources</text>
|
||||
<text x="260" y="232" text-anchor="middle" fill="#6ee7b7" font-size="10">.events</text>
|
||||
|
||||
<rect x="415" y="178" width="170" height="64" rx="8" fill="#0f0d1a" stroke="#10b981" stroke-width="1"/>
|
||||
<text x="500" y="200" text-anchor="middle" fill="#6ee7b7" font-size="10">.publishes</text>
|
||||
<text x="500" y="216" text-anchor="middle" fill="#6ee7b7" font-size="10">.context</text>
|
||||
<text x="500" y="232" text-anchor="middle" fill="#6ee7b7" font-size="10">.errors</text>
|
||||
|
||||
<!-- Footer -->
|
||||
<text x="380" y="262" text-anchor="middle" fill="#64748b" font-size="10">First registration wins — root can override module defaults</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.4 KiB |
5
05_todo_sse_datastar/404.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/5xx.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/about.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/contact.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
3
05_todo_sse_datastar/create_todo.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}})
|
||||
returning id, title, finished;
|
||||
7
05_todo_sse_datastar/create_todos_table.sql
Normal 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);
|
||||
4
05_todo_sse_datastar/delete_todo.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id;
|
||||
3
05_todo_sse_datastar/get_todos.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
5
05_todo_sse_datastar/home.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
25
05_todo_sse_datastar/layout.mustache.html
Normal 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>
|
||||
151
05_todo_sse_datastar/main.c
Normal file
@@ -0,0 +1,151 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <datastar.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", {session()},
|
||||
.get = {
|
||||
render("home")
|
||||
}
|
||||
},
|
||||
|
||||
{"about", "/about", {session()},
|
||||
.get = {
|
||||
render("about")
|
||||
}
|
||||
},
|
||||
|
||||
{"contact", "/contact", {session()},
|
||||
.get = {
|
||||
render("contact")
|
||||
}
|
||||
},
|
||||
|
||||
{"todos", "/todos", {logged_in()},
|
||||
.sse = {"todos:{{user_id}}"},
|
||||
|
||||
.get = {
|
||||
query({"get_todos",
|
||||
.set_key = "todos",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
render("todos")
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate({"title",
|
||||
.validation = "^\\S{1,16}$",
|
||||
.message = "must be 1-16 characters, no spaces"
|
||||
}),
|
||||
query({"create_todo",
|
||||
.set_key = "todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
ds_sse("todos:{{user_id}}",
|
||||
.target = "todos",
|
||||
.mode = mode_prepend,
|
||||
.elements = {"todo"}
|
||||
)
|
||||
}
|
||||
},
|
||||
|
||||
{"todo", "/todos/:id", {
|
||||
logged_in(),
|
||||
validate({"id",
|
||||
.validation = "^\\d{1,10}$",
|
||||
.message = "must be between 1-9999999999"
|
||||
})},
|
||||
|
||||
.patch = {
|
||||
validate({"finished",
|
||||
.optional = true,
|
||||
.validation = "1",
|
||||
.message = "must be 1"
|
||||
}),
|
||||
find({"update_todo",
|
||||
.set_key = "todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
ds_sse("todos:{{user_id}}",
|
||||
.target = "todo_{{id}}",
|
||||
.mode = mode_replace,
|
||||
.elements = {"todo"}
|
||||
)
|
||||
},
|
||||
|
||||
.delete = {
|
||||
find({"delete_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
ds_sse("todos:{{user_id}}",
|
||||
.target = "todo_{{id}}",
|
||||
.mode = mode_remove
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.errors = {
|
||||
{http_error, {
|
||||
render("5xx")
|
||||
}},
|
||||
|
||||
{http_not_found, {
|
||||
render("404")
|
||||
}}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"layout", (asset){
|
||||
#embed "layout.mustache.html"
|
||||
}},
|
||||
{"home", (asset){
|
||||
#embed "home.mustache.html"
|
||||
}},
|
||||
{"about", (asset){
|
||||
#embed "about.mustache.html"
|
||||
}},
|
||||
{"contact", (asset){
|
||||
#embed "contact.mustache.html"
|
||||
}},
|
||||
{"5xx", (asset){
|
||||
#embed "5xx.mustache.html"
|
||||
}},
|
||||
{"404", (asset){
|
||||
#embed "404.mustache.html"
|
||||
}},
|
||||
{"todos", (asset){
|
||||
#embed "todos.mustache.html"
|
||||
}},
|
||||
{"todo", (asset){
|
||||
#embed "todo.mustache.html"
|
||||
}},
|
||||
{"get_todos", (asset){
|
||||
#embed "get_todos.sql"
|
||||
}},
|
||||
{"create_todo", (asset){
|
||||
#embed "create_todo.sql"
|
||||
}},
|
||||
{"update_todo", (asset){
|
||||
#embed "update_todo.sql"
|
||||
}},
|
||||
{"delete_todo", (asset){
|
||||
#embed "delete_todo.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.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}
|
||||
};
|
||||
}
|
||||
0
05_todo_sse_datastar/public/favicon.png
Normal file
10
05_todo_sse_datastar/todo.mustache.html
Normal 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>
|
||||
21
05_todo_sse_datastar/todos.mustache.html
Normal 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}}
|
||||
5
05_todo_sse_datastar/update_todo.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id, title, finished;
|
||||
52
06-middleware-scoping.svg
Normal file
@@ -0,0 +1,52 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 360" width="760" height="360">
|
||||
<rect width="760" height="360" fill="#09080f"/>
|
||||
<defs>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<!-- Outermost: Global .before / .after -->
|
||||
<rect x="30" y="20" width="700" height="310" rx="14" fill="#1a1328" stroke="#7c3aed" stroke-width="1.5"/>
|
||||
<text x="50" y="46" fill="#c084fc" font-size="11" font-weight="700">Global .before</text>
|
||||
<text x="50" y="62" fill="#9580b8" font-size="9">session()</text>
|
||||
<text x="610" y="46" fill="#c084fc" font-size="11" font-weight="700">Global .after</text>
|
||||
<text x="610" y="62" fill="#9580b8" font-size="9">call(log_request)</text>
|
||||
|
||||
<!-- Middle: Resource .before / .after -->
|
||||
<rect x="60" y="78" width="640" height="230" rx="12" fill="#111827" stroke="#3b82f6" stroke-width="1.5"/>
|
||||
<text x="80" y="102" fill="#60a5fa" font-size="11" font-weight="700">Resource .before</text>
|
||||
<text x="80" y="118" fill="#7b9ec9" font-size="9">logged_in()</text>
|
||||
<text x="570" y="102" fill="#60a5fa" font-size="11" font-weight="700">Resource .after</text>
|
||||
|
||||
<!-- Inner: Route pipeline -->
|
||||
<rect x="90" y="134" width="580" height="152" rx="10" fill="#0f0d1a" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<text x="110" y="158" fill="#22d3ee" font-size="11" font-weight="700">Route Pipeline — POST /todos</text>
|
||||
|
||||
<!-- Steps inside route -->
|
||||
<rect x="110" y="172" width="100" height="36" rx="6" fill="#1e293b" stroke="#3b82f6" stroke-width="0.75"/>
|
||||
<text x="160" y="194" text-anchor="middle" fill="#93c5fd" font-size="10" font-weight="600">param()</text>
|
||||
|
||||
<line x1="210" y1="190" x2="228" y2="190" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="238,190 228,185 228,195" fill="#64748b"/>
|
||||
|
||||
<rect x="238" y="172" width="100" height="36" rx="6" fill="#1e293b" stroke="#8b5cf6" stroke-width="0.75"/>
|
||||
<text x="288" y="194" text-anchor="middle" fill="#c4b5fd" font-size="10" font-weight="600">db()</text>
|
||||
|
||||
<line x1="338" y1="190" x2="356" y2="190" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="366,190 356,185 356,195" fill="#64748b"/>
|
||||
|
||||
<rect x="366" y="172" width="100" height="36" rx="6" fill="#1e293b" stroke="#f59e0b" stroke-width="0.75"/>
|
||||
<text x="416" y="194" text-anchor="middle" fill="#fcd34d" font-size="10" font-weight="600">emit()</text>
|
||||
|
||||
<line x1="466" y1="190" x2="484" y2="190" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="494,190 484,185 484,195" fill="#64748b"/>
|
||||
|
||||
<rect x="494" y="172" width="100" height="36" rx="6" fill="#1e293b" stroke="#10b981" stroke-width="0.75"/>
|
||||
<text x="544" y="194" text-anchor="middle" fill="#6ee7b7" font-size="10" font-weight="600">redirect()</text>
|
||||
|
||||
<!-- Route .before / .after labels -->
|
||||
<text x="110" y="244" fill="#22d3ee" font-size="9">Route .before runs first inside this scope</text>
|
||||
<text x="110" y="260" fill="#22d3ee" font-size="9">Route .after runs last inside this scope</text>
|
||||
|
||||
<!-- Execution order arrows at bottom -->
|
||||
<text x="380" y="348" text-anchor="middle" fill="#64748b" font-size="10">Execution order: global .before → resource .before → route pipeline → resource .after → global .after</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 3.1 KiB |
52
06_todo_events/activity/activity.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config activity(){
|
||||
return (config) {
|
||||
.name = "activity",
|
||||
|
||||
.resources = {
|
||||
{"activity", "/activity", {logged_in()},
|
||||
.get = {
|
||||
query({"get_activities",
|
||||
.set_key = "activity",
|
||||
.db = "activity_db"
|
||||
}),
|
||||
render("activity")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"activity", (asset){
|
||||
#embed "activity.mustache.html"
|
||||
}},
|
||||
{"get_activities", (asset){
|
||||
#embed "get_activities.sql"
|
||||
}},
|
||||
{"insert_activity", (asset){
|
||||
#embed "insert_activity.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.events = {
|
||||
{"todo_created", {
|
||||
query({"insert_activity",
|
||||
.db = "activity_db"
|
||||
})
|
||||
}}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.name = "activity_db",
|
||||
.engine = sqlite_db,
|
||||
.connect = "file:activity.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_activity_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
13
06_todo_events/activity/activity.mustache.html
Normal file
@@ -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}}
|
||||
8
06_todo_events/activity/create_activity_table.sql
Normal file
@@ -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);
|
||||
5
06_todo_events/activity/get_activities.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
select action, title, created_at
|
||||
from activity
|
||||
where user_id = {{user_id}}
|
||||
order by created_at desc
|
||||
limit 50;
|
||||
2
06_todo_events/activity/insert_activity.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
insert into activity(user_id, action, title)
|
||||
values({{user_id}}, 'created', {{title}});
|
||||
43
06_todo_events/main.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <mach.h>
|
||||
#include <session_auth.h>
|
||||
#include "todos/todos.c"
|
||||
#include "activity/activity.c"
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", {session()},
|
||||
.get = {
|
||||
render("home")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.errors = {
|
||||
{http_error, {
|
||||
render("5xx")
|
||||
}},
|
||||
|
||||
{http_not_found, {
|
||||
render("404")
|
||||
}}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"layout", (asset){
|
||||
#embed "static/layout.mustache.html"
|
||||
}},
|
||||
{"home", (asset){
|
||||
#embed "static/home.mustache.html"
|
||||
}},
|
||||
{"5xx", (asset){
|
||||
#embed "static/5xx.mustache.html"
|
||||
}},
|
||||
{"404", (asset){
|
||||
#embed "static/404.mustache.html"
|
||||
}}
|
||||
},
|
||||
|
||||
.modules = {todos, activity, session_auth}
|
||||
};
|
||||
}
|
||||
0
06_todo_events/public/favicon.png
Normal file
5
06_todo_events/static/404.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}};
|
||||
5
06_todo_events/static/5xx.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}};
|
||||
5
06_todo_events/static/home.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}};
|
||||
20
06_todo_events/static/layout.mustache.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='/favicon.png'>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='/login'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='/'>home</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
2
06_todo_events/todos/create_todo.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}});
|
||||
7
06_todo_events/todos/create_todos_table.sql
Normal 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
06_todo_events/todos/delete_todo.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
3
06_todo_events/todos/get_todos.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
98
06_todo_events/todos/todos.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config todos(){
|
||||
return (config) {
|
||||
.name = "todos",
|
||||
|
||||
.resources = {
|
||||
{"todos", "/todos", {logged_in()},
|
||||
.get = {
|
||||
query({"get_todos",
|
||||
.set_key = "todos",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
render("todos")
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate({"title",
|
||||
.validation = "^\\S{1,16}$",
|
||||
.message = "must be 1-16 characters, no spaces"
|
||||
}),
|
||||
query({"create_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
emit("todo_created"),
|
||||
redirect("todos")
|
||||
}
|
||||
},
|
||||
|
||||
{"todo", "/todos/:id", {
|
||||
logged_in(),
|
||||
validate({"id",
|
||||
.validation = "^\\d{1,10}$",
|
||||
.message = "must be between 1-9999999999"
|
||||
})},
|
||||
|
||||
.patch = {
|
||||
validate({"finished",
|
||||
.optional = true,
|
||||
.validation = "1",
|
||||
.message = "must be 1"
|
||||
}),
|
||||
find({"update_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
},
|
||||
|
||||
.delete = {
|
||||
find({"delete_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"todos", (asset){
|
||||
#embed "todos.mustache.html"
|
||||
}},
|
||||
{"get_todos", (asset){
|
||||
#embed "get_todos.sql"
|
||||
}},
|
||||
{"create_todos", (asset){
|
||||
#embed "create_todo.sql"
|
||||
}},
|
||||
{"update_todo", (asset){
|
||||
#embed "update_todo.sql"
|
||||
}},
|
||||
{"delete_todo", (asset){
|
||||
#embed "delete_todo.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.publishes = {
|
||||
{"todo_created",
|
||||
.with = {
|
||||
"user_id",
|
||||
"title"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "todos_db",
|
||||
.connect = "file:todo.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_todos_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
32
06_todo_events/todos/todos.mustache.html
Normal file
@@ -0,0 +1,32 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<form action="{{url:todos}}" method="post">
|
||||
<input type="text" name="title" placeholder="new todo" required>
|
||||
<button type="submit">add</button>
|
||||
</form>
|
||||
{{^todos}}
|
||||
<p>no todos</p>
|
||||
{{/todos}}
|
||||
{{#todos}}
|
||||
{{#.}}
|
||||
<div>
|
||||
<form action="{{url:todo:id}}" 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:id}}" method="post" style="display:inline">
|
||||
<input type="hidden" name="http_method" value="delete">
|
||||
<button type="submit">delete</button>
|
||||
</form>
|
||||
</div>
|
||||
{{/.}}
|
||||
{{/todos}}
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
4
06_todo_events/todos/update_todo.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
50
07-context-scoping.svg
Normal file
@@ -0,0 +1,50 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 360" width="760" height="360">
|
||||
<rect width="760" height="360" fill="#09080f"/>
|
||||
<defs>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<!-- Outermost: Global .context -->
|
||||
<rect x="30" y="20" width="700" height="310" rx="14" fill="#1a1328" stroke="#7c3aed" stroke-width="1.5"/>
|
||||
<text x="50" y="46" fill="#c084fc" font-size="11" font-weight="700">Global .context</text>
|
||||
<text x="50" y="62" fill="#9580b8" font-size="9">base_layout, site_name</text>
|
||||
|
||||
<!-- Middle: Resource .context -->
|
||||
<rect x="60" y="78" width="640" height="230" rx="12" fill="#111827" stroke="#3b82f6" stroke-width="1.5"/>
|
||||
<text x="80" y="102" fill="#60a5fa" font-size="11" font-weight="700">Resource .context</text>
|
||||
<text x="80" y="118" fill="#7b9ec9" font-size="9">layout, sidebar</text>
|
||||
|
||||
<!-- Inner: Route pipeline -->
|
||||
<rect x="90" y="134" width="580" height="152" rx="10" fill="#0f0d1a" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<text x="110" y="158" fill="#22d3ee" font-size="11" font-weight="700">Route Pipeline — GET /todos</text>
|
||||
|
||||
<!-- Steps inside route -->
|
||||
<rect x="110" y="172" width="120" height="36" rx="6" fill="#1e293b" stroke="#8b5cf6" stroke-width="0.75"/>
|
||||
<text x="170" y="194" text-anchor="middle" fill="#c4b5fd" font-size="10" font-weight="600">db() → todos</text>
|
||||
|
||||
<line x1="230" y1="190" x2="258" y2="190" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="268,190 258,185 258,195" fill="#64748b"/>
|
||||
|
||||
<rect x="268" y="172" width="120" height="36" rx="6" fill="#1e293b" stroke="#f59e0b" stroke-width="0.75"/>
|
||||
<text x="328" y="194" text-anchor="middle" fill="#fcd34d" font-size="10" font-weight="600">call() → count</text>
|
||||
|
||||
<line x1="388" y1="190" x2="416" y2="190" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="426,190 416,185 416,195" fill="#64748b"/>
|
||||
|
||||
<rect x="426" y="172" width="120" height="36" rx="6" fill="#1e293b" stroke="#10b981" stroke-width="0.75"/>
|
||||
<text x="486" y="194" text-anchor="middle" fill="#6ee7b7" font-size="10" font-weight="600">render()</text>
|
||||
|
||||
<!-- Available context note -->
|
||||
<text x="110" y="237" fill="#22d3ee" font-size="9">render() sees: base_layout, site_name, layout, sidebar, todos, count</text>
|
||||
<text x="110" y="253" fill="#22d3ee" font-size="9">Context merges top-down; first named registration wins</text>
|
||||
|
||||
<!-- Cascade arrows on the left side -->
|
||||
<line x1="42" y1="68" x2="42" y2="78" stroke="#7c3aed" stroke-width="1.5" stroke-dasharray="3,2"/>
|
||||
<polygon points="42,84 38,76 46,76" fill="#7c3aed"/>
|
||||
|
||||
<line x1="72" y1="124" x2="72" y2="134" stroke="#3b82f6" stroke-width="1.5" stroke-dasharray="3,2"/>
|
||||
<polygon points="72,140 68,132 76,132" fill="#3b82f6"/>
|
||||
|
||||
<!-- Bottom summary -->
|
||||
<text x="380" y="348" text-anchor="middle" fill="#64748b" font-size="10">Context cascades: global .context → resource .context → pipeline steps add to context → render() merges all</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.9 KiB |
1
07_roundest_pokemon_htmx/README.md
Normal file
@@ -0,0 +1 @@
|
||||
compare to https://github.com/t3dotgg/1app5stacks
|
||||
7
07_roundest_pokemon_htmx/create_pokemons_table.sql
Normal file
@@ -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
|
||||
);
|
||||
4
07_roundest_pokemon_htmx/get_challengers.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
select id, name, sprite
|
||||
from pokemons
|
||||
order by random()
|
||||
limit 2;
|
||||
5
07_roundest_pokemon_htmx/get_results.sql
Normal file
@@ -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;
|
||||
53
07_roundest_pokemon_htmx/home.mustache.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Roundest (MaCH Version)</title>
|
||||
{{> htmx_script}}
|
||||
{{> tailwind_script}}
|
||||
</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>
|
||||
83
07_roundest_pokemon_htmx/main.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <mach.h>
|
||||
#include <htmx.h>
|
||||
#include <sqlite.h>
|
||||
#include <tailwind.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/",
|
||||
.get = {
|
||||
query({"get_challengers",
|
||||
.set_key = "challengers",
|
||||
.db = "pokemon_db"
|
||||
}),
|
||||
exec(^(){
|
||||
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"));
|
||||
}),
|
||||
render("home")
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate(
|
||||
{"winner",
|
||||
.validation = "^\\d{1,8}$",
|
||||
.message = "must be 1-99999999"
|
||||
},
|
||||
{"loser",
|
||||
.validation = "^\\d{1,8}$",
|
||||
.message = "must be 1-99999999"
|
||||
}
|
||||
),
|
||||
find({"vote",
|
||||
.db = "pokemon_db"
|
||||
}),
|
||||
reroute("home")
|
||||
}
|
||||
},
|
||||
|
||||
{"result", "/results",
|
||||
.get = {
|
||||
query({"get_results",
|
||||
.set_key = "results",
|
||||
.db = "pokemon_db"
|
||||
}),
|
||||
render("results")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"home", (asset){
|
||||
#embed "home.mustache.html"
|
||||
}},
|
||||
{"results", (asset){
|
||||
#embed "results.mustache.html"
|
||||
}},
|
||||
{"get_challengers", (asset){
|
||||
#embed "get_challengers.sql"
|
||||
}},
|
||||
{"vote", (asset){
|
||||
#embed "vote.sql"
|
||||
}},
|
||||
{"get_results", (asset){
|
||||
#embed "get_results.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.name = "pokemon_db",
|
||||
.engine = sqlite_db,
|
||||
.connect = "file::memory:?cache=shared",
|
||||
.migrations = {(asset){
|
||||
#embed "create_pokemons_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {htmx, sqlite, tailwind}
|
||||
};
|
||||
}
|
||||
28
07_roundest_pokemon_htmx/results.mustache.html
Normal file
@@ -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}}
|
||||
8
07_roundest_pokemon_htmx/vote.sql
Normal file
@@ -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;
|
||||
96
08-sse-datastar-flow.svg
Normal file
@@ -0,0 +1,96 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 400" width="760" height="400">
|
||||
<rect width="760" height="400" fill="#09080f"/>
|
||||
<defs>
|
||||
<linearGradient id="clientG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#06b6d4"/>
|
||||
<stop offset="100%" stop-color="#0891b2"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="serverG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#8b5cf6"/>
|
||||
<stop offset="100%" stop-color="#7c3aed"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="chanG" x1="0" y1="0" x2="1" y2="0">
|
||||
<stop offset="0%" stop-color="#f59e0b"/>
|
||||
<stop offset="50%" stop-color="#fbbf24"/>
|
||||
<stop offset="100%" stop-color="#f59e0b"/>
|
||||
</linearGradient>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<text x="380" y="28" text-anchor="middle" fill="#e2e8f0" font-size="13" font-weight="700">SSE / Datastar — Real-Time Push</text>
|
||||
|
||||
<!-- ===== Left: Client A (subscriber) ===== -->
|
||||
<rect x="30" y="50" width="180" height="80" rx="10" fill="#1e1b2e" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<rect x="30" y="50" width="180" height="32" rx="10" fill="url(#clientG)"/>
|
||||
<text x="120" y="71" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">Client A</text>
|
||||
<text x="50" y="100" fill="#67e8f9" font-size="9">GET /todos</text>
|
||||
<text x="50" y="114" fill="#67e8f9" font-size="9">.sse channel connected</text>
|
||||
|
||||
<!-- ===== Left: Client B (subscriber) ===== -->
|
||||
<rect x="30" y="150" width="180" height="80" rx="10" fill="#1e1b2e" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<rect x="30" y="150" width="180" height="32" rx="10" fill="url(#clientG)"/>
|
||||
<text x="120" y="171" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">Client B</text>
|
||||
<text x="50" y="200" fill="#67e8f9" font-size="9">GET /todos</text>
|
||||
<text x="50" y="214" fill="#67e8f9" font-size="9">.sse channel connected</text>
|
||||
|
||||
<!-- ===== Channel pill ===== -->
|
||||
<rect x="280" y="110" width="200" height="44" rx="22" fill="url(#chanG)"/>
|
||||
<text x="380" y="128" text-anchor="middle" fill="#1e1b2e" font-size="10" font-weight="700">Channel</text>
|
||||
<text x="380" y="143" text-anchor="middle" fill="#78350f" font-size="9">todos/{{user_id}}</text>
|
||||
|
||||
<!-- Arrows: clients → channel (subscribe) -->
|
||||
<line x1="210" y1="90" x2="270" y2="132" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<polygon points="280,132 270,127 268,137" fill="#06b6d4"/>
|
||||
<line x1="210" y1="190" x2="270" y2="132" stroke="#06b6d4" stroke-width="1.5"/>
|
||||
<polygon points="280,132 270,127 268,137" fill="#06b6d4"/>
|
||||
|
||||
<text x="220" y="86" fill="#475569" font-size="8">subscribe</text>
|
||||
<text x="220" y="198" fill="#475569" font-size="8">subscribe</text>
|
||||
|
||||
<!-- ===== Right: Server pipeline (POST) ===== -->
|
||||
<rect x="30" y="270" width="700" height="110" rx="12" fill="#0f0d1a" stroke="#7c3aed" stroke-width="1.5"/>
|
||||
<text x="50" y="296" fill="#c084fc" font-size="11" font-weight="700">POST /todos — another client submits a form</text>
|
||||
|
||||
<!-- Pipeline steps -->
|
||||
<rect x="50" y="310" width="90" height="32" rx="6" fill="#1e293b" stroke="#3b82f6" stroke-width="0.75"/>
|
||||
<text x="95" y="330" text-anchor="middle" fill="#93c5fd" font-size="9">param()</text>
|
||||
|
||||
<line x1="140" y1="326" x2="155" y2="326" stroke="#64748b" stroke-width="1"/>
|
||||
<polygon points="163,326 155,322 155,330" fill="#64748b"/>
|
||||
|
||||
<rect x="163" y="310" width="90" height="32" rx="6" fill="#1e293b" stroke="#8b5cf6" stroke-width="0.75"/>
|
||||
<text x="208" y="330" text-anchor="middle" fill="#c4b5fd" font-size="9">db()</text>
|
||||
|
||||
<line x1="253" y1="326" x2="268" y2="326" stroke="#64748b" stroke-width="1"/>
|
||||
<polygon points="276,326 268,322 268,330" fill="#64748b"/>
|
||||
|
||||
<!-- datastar / sse step highlighted -->
|
||||
<rect x="276" y="306" width="180" height="40" rx="6" fill="#451a03" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<text x="366" y="324" text-anchor="middle" fill="#fcd34d" font-size="10" font-weight="700">datastar() / sse()</text>
|
||||
<text x="366" y="339" text-anchor="middle" fill="#fbbf24" font-size="8">.channel = "todos/{{user_id}}"</text>
|
||||
|
||||
<!-- Arrow from datastar step up to channel -->
|
||||
<path d="M366,306 L366,280 L380,280 L380,162" stroke="#f59e0b" stroke-width="2" fill="none"/>
|
||||
<polygon points="380,154 375,162 385,162" fill="#f59e0b"/>
|
||||
|
||||
<text x="394" y="230" fill="#f59e0b" font-size="9" font-weight="600">push to channel</text>
|
||||
|
||||
<!-- Arrows: channel → clients (broadcast) -->
|
||||
<line x1="280" y1="125" x2="218" y2="94" stroke="#f59e0b" stroke-width="1.5" stroke-dasharray="4,3"/>
|
||||
<polygon points="210,90 218,89 216,99" fill="#f59e0b"/>
|
||||
|
||||
<line x1="280" y1="140" x2="218" y2="186" stroke="#f59e0b" stroke-width="1.5" stroke-dasharray="4,3"/>
|
||||
<polygon points="210,190 218,181 216,191" fill="#f59e0b"/>
|
||||
|
||||
<text x="222" y="110" fill="#92400e" font-size="8">broadcast</text>
|
||||
<text x="222" y="180" fill="#92400e" font-size="8">broadcast</text>
|
||||
|
||||
<!-- Datastar DOM update note -->
|
||||
<rect x="530" y="100" width="200" height="64" rx="8" fill="#111827" stroke="#1f2937" stroke-width="1"/>
|
||||
<text x="550" y="120" fill="#6b7280" font-size="9" font-weight="600">DATASTAR MODE</text>
|
||||
<text x="550" y="136" fill="#94a3b8" font-size="9">.target = "todos"</text>
|
||||
<text x="550" y="152" fill="#94a3b8" font-size="9">.mode = mode_prepend</text>
|
||||
|
||||
<!-- Footer -->
|
||||
<text x="380" y="396" text-anchor="middle" fill="#64748b" font-size="10">Without .channel, events push directly to the requesting client</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 5.4 KiB |
300
08_todo_tasks/main.c
Normal file
@@ -0,0 +1,300 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <tailwind.h>
|
||||
|
||||
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(
|
||||
"<html>"
|
||||
"<head>"
|
||||
"<title>MACH Tasks Demo</title>"
|
||||
"{{> tailwind_script}}"
|
||||
"</head>"
|
||||
"<body class='bg-gray-50 min-h-screen p-8'>"
|
||||
"<div class='max-w-lg mx-auto'>"
|
||||
"<h1 class='text-2xl font-bold mb-1'>Todos</h1>"
|
||||
"{{#stats}}"
|
||||
"<p class='text-sm text-gray-500 mb-4'>"
|
||||
"{{done}} of {{total}} done"
|
||||
"</p>"
|
||||
"{{/stats}}"
|
||||
"<form method='POST' class='flex gap-2 mb-6'>"
|
||||
"<input name='title' placeholder='What needs doing?'"
|
||||
" class='flex-1 border rounded px-3 py-2' required>"
|
||||
"<button class='bg-blue-600 text-white px-4 py-2 rounded'>"
|
||||
"Add"
|
||||
"</button>"
|
||||
"</form>"
|
||||
"{{#todos}}"
|
||||
"<div class='flex items-center gap-3 py-2 border-b'>"
|
||||
"<form method='POST' action='{{url:todo:id}}'>"
|
||||
"<input type='hidden' name='http_method' value='put'>"
|
||||
"{{#done}}"
|
||||
"<button class='text-green-600'>✓</button>"
|
||||
"{{/done}}"
|
||||
"{{^done}}"
|
||||
"<button class='text-gray-300 hover:text-green-600'>○</button>"
|
||||
"{{/done}}"
|
||||
"</form>"
|
||||
"{{#done}}"
|
||||
"<span class='flex-1 line-through text-gray-400'>{{title}}</span>"
|
||||
"{{/done}}"
|
||||
"{{^done}}"
|
||||
"<span class='flex-1'>{{title}}</span>"
|
||||
"{{/done}}"
|
||||
"<form method='POST' action='{{url:todo:id}}'>"
|
||||
"<input type='hidden' name='http_method' value='delete'>"
|
||||
"<button class='text-red-400 hover:text-red-600 text-sm'>"
|
||||
"✕"
|
||||
"</button>"
|
||||
"</form>"
|
||||
"</div>"
|
||||
"{{/todos}}"
|
||||
"{{^todos}}"
|
||||
"<p class='text-gray-400 italic'>No todos yet.</p>"
|
||||
"{{/todos}}"
|
||||
"<a href='/activity' class='inline-block mt-6 text-sm text-blue-600 underline'>"
|
||||
"View worker activity log →"
|
||||
"</a>"
|
||||
"</div>"
|
||||
"</body>"
|
||||
"</html>"
|
||||
)
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate({"title",
|
||||
.validation = validate_not_empty,
|
||||
.message = "Title cannot be 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", {
|
||||
validate({"id",
|
||||
.validation = validate_positive,
|
||||
.message = "Invalid id"
|
||||
})},
|
||||
|
||||
.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(
|
||||
"<html>"
|
||||
"<head>"
|
||||
"<title>Worker Activity</title>"
|
||||
"{{> tailwind_script}}"
|
||||
"</head>"
|
||||
"<body class='bg-gray-50 min-h-screen p-8'>"
|
||||
"<div class='max-w-lg mx-auto'>"
|
||||
"<h1 class='text-2xl font-bold mb-1'>Worker Activity</h1>"
|
||||
"<p class='text-sm text-gray-500 mb-4'>"
|
||||
"Task executions from the worker reactor"
|
||||
"</p>"
|
||||
"<a href='/todos'"
|
||||
" class='inline-block mb-4 text-sm text-blue-600 underline'>"
|
||||
"← Back to todos"
|
||||
"</a>"
|
||||
"{{#logs}}"
|
||||
"<div class='flex items-baseline gap-2 py-2 border-b text-sm'>"
|
||||
"<span class='font-mono text-purple-700 shrink-0'>{{action}}</span>"
|
||||
"<span class='text-gray-600 flex-1'>{{detail}}</span>"
|
||||
"<span class='text-gray-400 shrink-0'>{{ran_at}}</span>"
|
||||
"</div>"
|
||||
"{{/logs}}"
|
||||
"{{^logs}}"
|
||||
"<p class='text-gray-400 italic'>No activity yet.</p>"
|
||||
"{{/logs}}"
|
||||
"</div>"
|
||||
"</body>"
|
||||
"</html>"
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.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 = {
|
||||
{http_bad_request, {
|
||||
render(
|
||||
"<html>"
|
||||
"<head>"
|
||||
"<title>Error</title>"
|
||||
"{{> tailwind_script}}"
|
||||
"</head>"
|
||||
"<body class='bg-gray-50 p-8'>"
|
||||
"<div class='max-w-lg mx-auto'>"
|
||||
"<p class='text-red-600 mb-4'>{{error_message:title}}</p>"
|
||||
"<a href='/todos' class='text-blue-600 underline'>← Back</a>"
|
||||
"</div>"
|
||||
"</body>"
|
||||
"</html>"
|
||||
)
|
||||
}}
|
||||
},
|
||||
|
||||
.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}
|
||||
};
|
||||
}
|
||||
89
09-database-multi-tenancy.svg
Normal file
@@ -0,0 +1,89 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 340" width="760" height="340">
|
||||
<rect width="760" height="340" fill="#09080f"/>
|
||||
<defs>
|
||||
<linearGradient id="dbG" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="#8b5cf6"/>
|
||||
<stop offset="100%" stop-color="#7c3aed"/>
|
||||
</linearGradient>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<text x="380" y="28" text-anchor="middle" fill="#e2e8f0" font-size="13" font-weight="700">Database Multi-Tenancy</text>
|
||||
|
||||
<!-- Config block -->
|
||||
<rect x="40" y="50" width="320" height="86" rx="10" fill="#111827" stroke="#1f2937" stroke-width="1"/>
|
||||
<text x="60" y="74" fill="#6b7280" font-size="9" font-weight="600">.databases config</text>
|
||||
<text x="60" y="96" fill="#c4b5fd" font-size="10">.connect =</text>
|
||||
<text x="60" y="114" fill="#fcd34d" font-size="10">"file:<tspan fill="#f59e0b" font-weight="700">{{user_id}}</tspan>_todo.db?mode=rwc"</text>
|
||||
|
||||
<!-- Arrow right -->
|
||||
<line x1="360" y1="93" x2="398" y2="93" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="408,93 398,88 398,98" fill="#64748b"/>
|
||||
|
||||
<!-- Resolution box -->
|
||||
<rect x="408" y="50" width="180" height="86" rx="10" fill="#111827" stroke="#f59e0b" stroke-width="1"/>
|
||||
<text x="428" y="74" fill="#f59e0b" font-size="9" font-weight="600">RUNTIME RESOLUTION</text>
|
||||
<text x="428" y="96" fill="#94a3b8" font-size="10">user_id from</text>
|
||||
<text x="428" y="114" fill="#94a3b8" font-size="10">pipeline context</text>
|
||||
|
||||
<!-- Three request arrows going down -->
|
||||
<!-- User alice -->
|
||||
<rect x="40" y="170" width="180" height="36" rx="8" fill="#1e293b" stroke="#3b82f6" stroke-width="1"/>
|
||||
<text x="130" y="192" text-anchor="middle" fill="#93c5fd" font-size="10">Request: user_id = alice</text>
|
||||
|
||||
<!-- User bob -->
|
||||
<rect x="40" y="224" width="180" height="36" rx="8" fill="#1e293b" stroke="#3b82f6" stroke-width="1"/>
|
||||
<text x="130" y="246" text-anchor="middle" fill="#93c5fd" font-size="10">Request: user_id = bob</text>
|
||||
|
||||
<!-- User carol -->
|
||||
<rect x="40" y="278" width="180" height="36" rx="8" fill="#1e293b" stroke="#3b82f6" stroke-width="1"/>
|
||||
<text x="130" y="300" text-anchor="middle" fill="#93c5fd" font-size="10">Request: user_id = carol</text>
|
||||
|
||||
<!-- Arrows to databases -->
|
||||
<line x1="220" y1="188" x2="398" y2="188" stroke="#64748b" stroke-width="1"/>
|
||||
<polygon points="408,188 398,183 398,193" fill="#64748b"/>
|
||||
|
||||
<line x1="220" y1="242" x2="398" y2="242" stroke="#64748b" stroke-width="1"/>
|
||||
<polygon points="408,242 398,237 398,247" fill="#64748b"/>
|
||||
|
||||
<line x1="220" y1="296" x2="398" y2="296" stroke="#64748b" stroke-width="1"/>
|
||||
<polygon points="408,296 398,291 398,301" fill="#64748b"/>
|
||||
|
||||
<!-- Database: alice -->
|
||||
<rect x="408" y="170" width="220" height="36" rx="8" fill="#2d2640" stroke="#8b5cf6" stroke-width="1.5"/>
|
||||
<text x="440" y="192" fill="#c4b5fd" font-size="10">file:</text>
|
||||
<text x="472" y="192" fill="#fcd34d" font-size="10" font-weight="700">alice</text>
|
||||
<text x="505" y="192" fill="#c4b5fd" font-size="10">_todo.db</text>
|
||||
|
||||
<!-- Database: bob -->
|
||||
<rect x="408" y="224" width="220" height="36" rx="8" fill="#2d2640" stroke="#8b5cf6" stroke-width="1.5"/>
|
||||
<text x="440" y="246" fill="#c4b5fd" font-size="10">file:</text>
|
||||
<text x="472" y="246" fill="#fcd34d" font-size="10" font-weight="700">bob</text>
|
||||
<text x="495" y="246" fill="#c4b5fd" font-size="10">_todo.db</text>
|
||||
|
||||
<!-- Database: carol -->
|
||||
<rect x="408" y="278" width="220" height="36" rx="8" fill="#2d2640" stroke="#8b5cf6" stroke-width="1.5"/>
|
||||
<text x="440" y="300" fill="#c4b5fd" font-size="10">file:</text>
|
||||
<text x="472" y="300" fill="#fcd34d" font-size="10" font-weight="700">carol</text>
|
||||
<text x="505" y="300" fill="#c4b5fd" font-size="10">_todo.db</text>
|
||||
|
||||
<!-- DB icons (simple cylinder shapes) -->
|
||||
<g transform="translate(648, 170)">
|
||||
<ellipse cx="18" cy="6" rx="18" ry="6" fill="#7c3aed" opacity="0.5"/>
|
||||
<rect x="0" y="6" width="36" height="24" fill="#7c3aed" opacity="0.3"/>
|
||||
<ellipse cx="18" cy="30" rx="18" ry="6" fill="#7c3aed" opacity="0.5"/>
|
||||
</g>
|
||||
<g transform="translate(648, 224)">
|
||||
<ellipse cx="18" cy="6" rx="18" ry="6" fill="#7c3aed" opacity="0.5"/>
|
||||
<rect x="0" y="6" width="36" height="24" fill="#7c3aed" opacity="0.3"/>
|
||||
<ellipse cx="18" cy="30" rx="18" ry="6" fill="#7c3aed" opacity="0.5"/>
|
||||
</g>
|
||||
<g transform="translate(648, 278)">
|
||||
<ellipse cx="18" cy="6" rx="18" ry="6" fill="#7c3aed" opacity="0.5"/>
|
||||
<rect x="0" y="6" width="36" height="24" fill="#7c3aed" opacity="0.3"/>
|
||||
<ellipse cx="18" cy="30" rx="18" ry="6" fill="#7c3aed" opacity="0.5"/>
|
||||
</g>
|
||||
|
||||
<!-- Footer -->
|
||||
<text x="380" y="336" text-anchor="middle" fill="#64748b" font-size="10">One config, per-user databases — Mustache interpolation in .connect at runtime</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.7 KiB |
82
10-boot-time-compilation.svg
Normal file
@@ -0,0 +1,82 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 760 300" width="760" height="300">
|
||||
<rect width="760" height="300" fill="#09080f"/>
|
||||
<defs>
|
||||
<linearGradient id="bootG" x1="0" y1="0" x2="1" y2="0">
|
||||
<stop offset="0%" stop-color="#7c3aed"/>
|
||||
<stop offset="100%" stop-color="#a855f7"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="warmG" x1="0" y1="0" x2="1" y2="0">
|
||||
<stop offset="0%" stop-color="#f59e0b"/>
|
||||
<stop offset="100%" stop-color="#fbbf24"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="runG" x1="0" y1="0" x2="1" y2="0">
|
||||
<stop offset="0%" stop-color="#10b981"/>
|
||||
<stop offset="100%" stop-color="#34d399"/>
|
||||
</linearGradient>
|
||||
<style>text{font-family:'JetBrains Mono','Fira Code',monospace}</style>
|
||||
</defs>
|
||||
|
||||
<text x="380" y="28" text-anchor="middle" fill="#e2e8f0" font-size="13" font-weight="700">Boot-Time Compilation</text>
|
||||
|
||||
<!-- Phase 1: config() -->
|
||||
<rect x="30" y="50" width="200" height="110" rx="10" fill="#1e1b2e" stroke="#7c3aed" stroke-width="1.5"/>
|
||||
<rect x="30" y="50" width="200" height="32" rx="10" fill="url(#bootG)"/>
|
||||
<text x="130" y="71" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">config mach()</text>
|
||||
<text x="50" y="96" fill="#c4b5fd" font-size="9">.includes</text>
|
||||
<text x="50" y="112" fill="#c4b5fd" font-size="9">.databases</text>
|
||||
<text x="50" y="128" fill="#c4b5fd" font-size="9">.resources</text>
|
||||
<text x="50" y="144" fill="#c4b5fd" font-size="9">.events / .errors</text>
|
||||
|
||||
<!-- Arrow 1 -->
|
||||
<line x1="230" y1="105" x2="268" y2="105" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="278,105 268,100 268,110" fill="#64748b"/>
|
||||
|
||||
<!-- Phase 2: Precompile -->
|
||||
<rect x="278" y="50" width="200" height="110" rx="10" fill="#1e1b2e" stroke="#f59e0b" stroke-width="1.5"/>
|
||||
<rect x="278" y="50" width="200" height="32" rx="10" fill="url(#warmG)"/>
|
||||
<text x="378" y="71" text-anchor="middle" fill="#1e1b2e" font-size="11" font-weight="700">Precompile</text>
|
||||
<text x="298" y="96" fill="#fcd34d" font-size="9">Compile pipelines</text>
|
||||
<text x="298" y="112" fill="#fcd34d" font-size="9">Compile templates</text>
|
||||
<text x="298" y="128" fill="#fcd34d" font-size="9">Prepare SQL statements</text>
|
||||
<text x="298" y="144" fill="#fcd34d" font-size="9">Build route table</text>
|
||||
|
||||
<!-- Arrow 2 -->
|
||||
<line x1="478" y1="105" x2="516" y2="105" stroke="#64748b" stroke-width="1.5"/>
|
||||
<polygon points="526,105 516,100 516,110" fill="#64748b"/>
|
||||
|
||||
<!-- Phase 3: Execution Graph -->
|
||||
<rect x="526" y="50" width="200" height="110" rx="10" fill="#1e1b2e" stroke="#10b981" stroke-width="1.5"/>
|
||||
<rect x="526" y="50" width="200" height="32" rx="10" fill="url(#runG)"/>
|
||||
<text x="626" y="71" text-anchor="middle" fill="#fff" font-size="11" font-weight="700">Execution Graph</text>
|
||||
<text x="546" y="96" fill="#6ee7b7" font-size="9">Pre-warmed pipelines</text>
|
||||
<text x="546" y="112" fill="#6ee7b7" font-size="9">Optimized query plans</text>
|
||||
<text x="546" y="128" fill="#6ee7b7" font-size="9">Compiled templates</text>
|
||||
<text x="546" y="144" fill="#6ee7b7" font-size="9">Ready to serve</text>
|
||||
|
||||
<!-- Phase labels -->
|
||||
<text x="130" y="180" text-anchor="middle" fill="#7c3aed" font-size="9" font-weight="600">RUNS ONCE AT BOOT</text>
|
||||
<text x="378" y="180" text-anchor="middle" fill="#f59e0b" font-size="9" font-weight="600">RUNS ONCE AT BOOT</text>
|
||||
<text x="626" y="180" text-anchor="middle" fill="#10b981" font-size="9" font-weight="600">SERVES ALL REQUESTS</text>
|
||||
|
||||
<!-- Runtime section -->
|
||||
<rect x="30" y="200" width="696" height="76" rx="10" fill="#0f0d1a" stroke="#10b981" stroke-width="1" stroke-dasharray="5,3"/>
|
||||
<text x="50" y="224" fill="#6ee7b7" font-size="10" font-weight="600">Runtime: each request executes a pre-warmed pipeline</text>
|
||||
|
||||
<!-- Request arrows -->
|
||||
<text x="50" y="248" fill="#475569" font-size="9">req →</text>
|
||||
<rect x="90" y="236" width="80" height="24" rx="4" fill="#1e293b" stroke="#10b981" stroke-width="0.75"/>
|
||||
<text x="130" y="252" text-anchor="middle" fill="#6ee7b7" font-size="9">pipeline</text>
|
||||
<line x1="170" y1="248" x2="184" y2="248" stroke="#10b981" stroke-width="1"/>
|
||||
<polygon points="192,248 184,244 184,252" fill="#10b981"/>
|
||||
<text x="200" y="252" fill="#475569" font-size="9">→ response</text>
|
||||
|
||||
<text x="320" y="248" fill="#475569" font-size="9">req →</text>
|
||||
<rect x="360" y="236" width="80" height="24" rx="4" fill="#1e293b" stroke="#10b981" stroke-width="0.75"/>
|
||||
<text x="400" y="252" text-anchor="middle" fill="#6ee7b7" font-size="9">pipeline</text>
|
||||
<line x1="440" y1="248" x2="454" y2="248" stroke="#10b981" stroke-width="1"/>
|
||||
<polygon points="462,248 454,244 454,252" fill="#10b981"/>
|
||||
<text x="470" y="252" fill="#475569" font-size="9">→ response</text>
|
||||
|
||||
<!-- Footer -->
|
||||
<text x="380" y="296" text-anchor="middle" fill="#64748b" font-size="10">Zero per-request compilation — config builds the graph, requests just execute it</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.8 KiB |
674
COPYING
Normal file
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
165
COPYING.LESSER
Normal file
@@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
165
LICENSE
Normal file
@@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
825
llms-full.md
Normal file
@@ -0,0 +1,825 @@
|
||||
# MACH
|
||||
|
||||
C23 web framework. App = `config mach()` returning `(config){...}` of resources, databases, modules. Each request runs a pipeline of steps over a per-request context. Memory, threads, I/O managed by the framework. Tasks and events are durable.
|
||||
|
||||
---
|
||||
|
||||
## CRITICAL RULES — read before writing any code
|
||||
|
||||
### Rule 1 — NEVER use `.` between `{{` and `}}`
|
||||
|
||||
`{{a.b}}` renders empty. Always use sections to enter nested scope: `{{#name}}...{{/name}}`. Template shape mirrors context shape.
|
||||
|
||||
**Helpers use `:` not `.` — these are fine:** `{{url:name}}`, `{{input:title}}`, `{{error:title}}`, `{{precision:total:2}}`.
|
||||
|
||||
#### Pattern A — Single root scalar
|
||||
|
||||
```c
|
||||
.context = {{"site_name", "MACH App"}}
|
||||
```
|
||||
```html
|
||||
✅ <h1>{{site_name}}</h1>
|
||||
```
|
||||
|
||||
#### Pattern B — `query()` / `find()` result is a TABLE, even single-row
|
||||
|
||||
```c
|
||||
find({.set_key = "todo", .db = "todos_db",
|
||||
.query = "select id, title from todos where id = {{id}};"})
|
||||
```
|
||||
Context: `{ todo: [{id: 5, title: "Learn MACH"}] }`. Open the section to read fields:
|
||||
```html
|
||||
✅ {{#todo}}<h1>{{title}}</h1>{{/todo}}
|
||||
❌ {{todo.title}} dot — renders ""
|
||||
❌ <h1>{{title}}</h1> not inside #todo — renders ""
|
||||
```
|
||||
|
||||
#### Pattern C — After `join()`: parent + nested children (most common; most-failed)
|
||||
|
||||
`query()` produces sibling tables. `join()` **MOVES** children inside parents — children no longer exist at root.
|
||||
|
||||
```c
|
||||
query(
|
||||
{.set_key = "project", .db = "projects_db",
|
||||
.query = "select id, name from projects where id = {{id}};"},
|
||||
{.set_key = "tasks", .db = "projects_db",
|
||||
.query = "select id, project_id, title from tasks where project_id = {{id}};"}
|
||||
),
|
||||
join(.target_table_key="project", .target_field_key="id",
|
||||
.nested_table_key="tasks", .nested_field_key="project_id",
|
||||
.target_join_field_key="tasks"),
|
||||
```
|
||||
Context after join: `{ project: [{id, name, tasks: [{id, project_id, title}, ...]}] }`. Tasks are now INSIDE project.
|
||||
|
||||
```html
|
||||
✅ {{#project}}
|
||||
<h1>{{name}}</h1>
|
||||
<ul>{{#tasks}}<li>{{title}}</li>{{/tasks}}</ul>
|
||||
{{/project}}
|
||||
```
|
||||
|
||||
**❌ Every one of these renders empty:**
|
||||
- `{{project.name}}` — dot
|
||||
- `{{project.tasks.title}}` — dot
|
||||
- `{{#project.tasks}}...{{/project.tasks}}` — dot in section name
|
||||
- `{{#tasks}}<li>{{title}}</li>{{/tasks}}` — at root after join, tasks lives inside project, not at root
|
||||
- `<h1>{{name}}</h1>` then `{{#tasks}}...{{/tasks}}` separately — name is inside project record too; no partial entry. **EVERY field that came from `set_key="X"` requires `{{#X}}` first**, including the parent's own scalars.
|
||||
|
||||
**Rule of thumb:** if a field came from `query().set_key = "X"`, it must be inside `{{#X}}`. No partial entry — you're inside the section or at root.
|
||||
|
||||
#### Pattern D — 3+ levels nested
|
||||
|
||||
`{ org: [{name, projects: [{title, tasks: [{label}]}]}] }`:
|
||||
```html
|
||||
✅ {{#org}}{{name}}{{#projects}}{{title}}{{#tasks}}{{label}}{{/tasks}}{{/projects}}{{/org}}
|
||||
❌ {{org.projects.tasks.label}} dot
|
||||
❌ {{#org.projects}}... dot in section name
|
||||
```
|
||||
|
||||
#### Pattern E — Iteration: `{{#name}}` auto-loops when name is array
|
||||
|
||||
```c
|
||||
{ projects: [{title:"A", tasks:[{label:"x"}]}, {title:"B", tasks:[{label:"z"}]}] }
|
||||
```
|
||||
```html
|
||||
✅ {{#projects}}<li>{{title}}<ul>{{#tasks}}<li>{{label}}</li>{{/tasks}}</ul></li>{{/projects}}
|
||||
```
|
||||
|
||||
#### Template Checklist — run on every `{{...}}` BEFORE emitting
|
||||
|
||||
0. **Well-formed:** exactly two `{` and two `}`, no whitespace inside delimiters. `{{/x}` (one `}`), `{x}}`, `{{{x}}}`, `{ {x} }` are all broken. After typing every `{{/...}}`, count the closers: `}` `}`.
|
||||
1. **No dot:** any `.` between `{{` and `}}` → STOP, add a section.
|
||||
2. **Field reachable:** is the bare name at the current scope? At root, only top-level keys. Inside `{{#X}}`, only fields of `X`'s record. After `join(child→parent)`, child no longer at root — must be inside `{{#parent}}`.
|
||||
3. **Depth = wrappers:** N nesting levels = N section pairs.
|
||||
4. **Section balance + nesting:** every `{{#x}}` has exactly one `{{/x}}` later, and vice versa. Sections nest like parens, never overlap. `{{#a}}{{#b}}{{/a}}{{/b}}` is broken even though counts match. Each named section opens **once** in a given scope.
|
||||
|
||||
---
|
||||
|
||||
### Rule 2 — ONE database per DOMAIN, not one per table
|
||||
|
||||
A "domain" is **what one MODULE owns** — a feature slice — not a noun in your data model.
|
||||
|
||||
A `projects` module owns *everything* about projects: project records, tasks attached to them, comments on those tasks, tags. **All of that lives in ONE database** (`projects_db`) with **many migrations** (one per table). Tasks aren't their own domain just because "tasks" is a noun — they're part of projects because they belong to projects.
|
||||
|
||||
**A new database appears only when a new module appears.** New table inside the existing module → new migration in the existing db, NOT a new db.
|
||||
|
||||
#### ✅ Correct
|
||||
```c
|
||||
.databases = {{
|
||||
.engine = sqlite_db, .name = "projects_db",
|
||||
.connect = "file:projects.db?mode=rwc",
|
||||
.migrations = {
|
||||
"CREATE TABLE projects (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);",
|
||||
"CREATE TABLE tasks (id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"project_id INTEGER NOT NULL REFERENCES projects(id), title TEXT NOT NULL);",
|
||||
"CREATE TABLE comments (id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"task_id INTEGER NOT NULL REFERENCES tasks(id), body TEXT NOT NULL);"
|
||||
}
|
||||
}}
|
||||
```
|
||||
|
||||
#### ❌ Wrong — both forms
|
||||
|
||||
```c
|
||||
// Form A: one db per table
|
||||
.databases = {{...projects_db...}, {...tasks_db...}, {...comments_db...}}
|
||||
|
||||
// Form B (subtler, most common): parent + child split into separate dbs
|
||||
.databases = {{...projects_db (with projects only)...},
|
||||
{...tasks_db (tasks should live in projects_db)...}}
|
||||
query({.db = "projects_db", ...}, {.db = "tasks_db", ...}) // wrong db
|
||||
```
|
||||
|
||||
**Parent-child relations are ONE domain, ONE database.** project+tasks, blog+comments, order+line_items, user+sessions, todo+comments — all one domain each. If `child` has `parent_id REFERENCES parent`, they belong in the same db.
|
||||
|
||||
**Rationalizations to REJECT:**
|
||||
- *"X and Y are different concepts so they should be different domains"* → Wrong. The foreign-key relation IS what makes them one domain.
|
||||
- *"More normalized / cleaner separation"* → MACH's separation unit is the **module**, not the table.
|
||||
- *"Microservices use one db per service"* → Right analogy, wrong unit. In MACH, "service" = module, not table. One module = one db.
|
||||
- *"My data model has X entities so I need X dbs"* → No. Number of dbs = number of modules. A module owns 3–10 tables.
|
||||
|
||||
**Self-check before adding a 2nd `.databases` entry:** "Am I adding a new module?" If no, you don't need a new db — add a migration to the existing one.
|
||||
|
||||
---
|
||||
|
||||
### Rule 3 — Concurrent queries: ONE step, MANY items (works across dbs too)
|
||||
|
||||
`query()` and `fetch()` items run in parallel. Two separate `query()` steps run **serially**. If items don't depend on each other, put them in ONE step.
|
||||
|
||||
```c
|
||||
✅ // Same db, concurrent
|
||||
query(
|
||||
{.set_key="todos", .db="todos_db", .query="select id, title from todos;"},
|
||||
{.set_key="count", .db="todos_db", .query="select count(*) as n from todos;"}
|
||||
)
|
||||
|
||||
✅ // Across dbs — STILL one step, still concurrent
|
||||
query(
|
||||
{.set_key="user", .db="users_db", .query="..."},
|
||||
{.set_key="orders", .db="commerce_db", .query="..."},
|
||||
{.set_key="activity", .db="activity_db", .query="..."}
|
||||
)
|
||||
|
||||
❌ // Three serial round-trips for independent queries
|
||||
query({.set_key="user", .db="users_db", ...}),
|
||||
query({.set_key="orders", .db="commerce_db", ...}),
|
||||
query({.set_key="activity", .db="activity_db", ...})
|
||||
```
|
||||
|
||||
Use separate steps **only** when a later query depends on a value the earlier one produced.
|
||||
|
||||
---
|
||||
|
||||
### Rule 4 — SQL `{{values}}` are prepared-statement parameters
|
||||
|
||||
Auto-bound, never spliced. SQL injection impossible. Don't pre-quote, don't pre-escape.
|
||||
|
||||
```c
|
||||
✅ query({.db="db", .query="insert into todos(title) values({{title}});"})
|
||||
❌ query({.db="db", .query="insert into todos(title) values('{{title}}');"}) // double-quoted
|
||||
```
|
||||
|
||||
For transactions: `BEGIN`/`COMMIT`/`ROLLBACK` directly in queries.
|
||||
|
||||
---
|
||||
|
||||
### Rule 5 — `query()`/`find()` item: asset name OR `.query`. Pick one. The asset must exist.
|
||||
|
||||
```c
|
||||
✅ query({.set_key="todos", .db="todos_db", .query="select ..."}) // inline; works anywhere
|
||||
|
||||
✅ query({"get_todos", .set_key="todos", .db="todos_db"}) // asset by name
|
||||
// requires: .context = {{"get_todos", (asset){#embed "get_todos.sql"}}}
|
||||
// WITHOUT that .context entry, this is a phantom reference — fails.
|
||||
|
||||
❌ query({"get_todos", .set_key="todos", .db="todos_db", .query="..."}) // both → boot rejection
|
||||
|
||||
❌ query({"get_todos", .set_key="todos", .db="todos_db"}) // when no .context defines "get_todos"
|
||||
```
|
||||
|
||||
**If your snippet has no `.context` block embedding SQL files, use inline `.query` only.** Don't reach for the positional name to avoid writing the SQL — that produces a phantom reference. The asset-name form is for when SQL grows large and you've extracted it; not for casual use.
|
||||
|
||||
Same rule applies to `find()` and `render()` (asset name in `.context` vs `.template` inline).
|
||||
|
||||
---
|
||||
|
||||
### Rule 6 — `find()` raises `http_not_found` on zero rows; `query()` does not
|
||||
|
||||
Otherwise identical. `find()` for "must exist" lookups (detail pages, by-id reads). `query()` for lists, counts, writes, anywhere zero rows is normal.
|
||||
|
||||
---
|
||||
|
||||
### Rule 7 — No `malloc`/`free`, no threads, no mutexes
|
||||
|
||||
Per-request arena handles memory. Reactors + thread pool handle concurrency. App code never calls these.
|
||||
|
||||
For a buffer: `char *buf = allocate(256);` (reclaimed at request end). For external-lib pointer cleanup: `defer_free(out);`.
|
||||
|
||||
---
|
||||
|
||||
### Rule 8 — Resource-based, not route-based
|
||||
|
||||
Resources referenced by **name**, never hard-coded path:
|
||||
|
||||
```c
|
||||
{{url:todos}} // → /todos
|
||||
{{url:todo:5}} // → /todos/5 (literal arg)
|
||||
{{url:todo:id}} // → /todos/{id from context}
|
||||
redirect("todo:{{id}}") // 302 to /todos/{id}
|
||||
reroute("todos") // re-enter pipeline server-side
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## C SYNTAX PATTERNS — required for compilation
|
||||
|
||||
Three patterns trip up small models even when MACH semantics are correct.
|
||||
|
||||
### Pattern 1 — Adjacent string literals concatenate. ONE quoted line per source line.
|
||||
|
||||
C joins adjacent strings at compile time. Use this for every multi-line SQL and every inline template. Never put a raw newline inside a single quoted string. Never mix quoted and bare text on one line.
|
||||
|
||||
```c
|
||||
✅ "CREATE TABLE projects (" // each line its own
|
||||
"id INTEGER PRIMARY KEY," // properly terminated string
|
||||
"name TEXT NOT NULL" // C concatenates them
|
||||
");"
|
||||
|
||||
❌ "CREATE TABLE projects ( // raw newline = compile error
|
||||
id INTEGER PRIMARY KEY, ...
|
||||
|
||||
❌ "<ul>"
|
||||
{{#tasks}}<li>{{title}}</li>{{/tasks}} // bare Mustache, not in string
|
||||
"</ul>"
|
||||
```
|
||||
|
||||
A MACH inline template is **not a heredoc** — it's a stack of small C string literals the compiler glues. Every fragment, every section marker, every HTML scrap must be inside its own `"..."`.
|
||||
|
||||
### Pattern 2 — Any `"` inside a C string ends it. Use `'` for HTML, no quotes for SQL identifiers.
|
||||
|
||||
#### 2a — SQL identifiers: don't quote them
|
||||
```c
|
||||
✅ "CREATE TABLE projects (id INTEGER PRIMARY KEY, name TEXT NOT NULL);"
|
||||
❌ "CREATE TABLE projects ("id" INTEGER ..., "name" TEXT ...);" // " ends C string
|
||||
```
|
||||
SQLite/Postgres/MySQL all accept unquoted identifiers for normal names. Never quote in MACH SQL unless reserved word (then escape with `\"`).
|
||||
|
||||
#### 2b — HTML attributes: USE SINGLE QUOTES EXCLUSIVELY. Never `\"`.
|
||||
|
||||
```c
|
||||
✅ "<li class='task' data-id='{{id}}'>{{title}}</li>" // single quotes — nothing to escape
|
||||
|
||||
⚠️ "<li class=\"task\" data-id=\"{{id}}\">{{title}}</li>"
|
||||
// works, but every \" is a slip waiting to happen.
|
||||
// Drop one backslash on one attribute → compile error.
|
||||
|
||||
❌ "<li class=\"task\" data-id="{{id}}">{{title}}</li>"
|
||||
// ↑ bare " — ends C string. The most-recurring HTML failure
|
||||
// is mixing escaped \" and bare " in one template. Don't escape; use ' everywhere.
|
||||
```
|
||||
|
||||
**Rule:** if your inline template contains any `\"`, you took the harder route. Switch every HTML attribute to `'...'` before emitting.
|
||||
|
||||
### Pattern 3 — All `.fields` go INSIDE one `(config){...}` block. Brace tracking is the most-regressed bug.
|
||||
|
||||
There is **no file-scope config in MACH.** `.databases`, `.modules`, `.resources` are NOT standalone declarations — they are fields of the single struct value `mach()` returns. Designated-initializer syntax (`.field = value`) is meaningful only **inside** a struct initializer.
|
||||
|
||||
> **Mechanical rule:** in the entire `mach()` body there must be **exactly one `};`** (closing `(config){...}` + return) and **exactly one `}`** after it (closing the function). No others.
|
||||
|
||||
#### Three failure modes (all close `(config){...}` at the wrong point)
|
||||
|
||||
```c
|
||||
✅ config mach(){
|
||||
return (config){ // depth 1 opens here
|
||||
.resources = {...}, // , separator
|
||||
.databases = {{...}}, // , separator
|
||||
.modules = {sqlite} // last field, no comma
|
||||
}; // ONE }; closes (config){...} + return
|
||||
} // ONE } closes function
|
||||
|
||||
❌ A: premature `};` mid-function
|
||||
.resources = {...}
|
||||
}; // ← ends return early
|
||||
.databases = {...}; // ← orphan inside function body
|
||||
.modules = {sqlite};
|
||||
|
||||
❌ B: fields placed AFTER mach() closes (file scope)
|
||||
};
|
||||
} // function ends here
|
||||
.databases = {...}; // ← floating at file scope = compile error
|
||||
.modules = {sqlite}
|
||||
// Giveaway: comment near here saying "global config" or "module-level" — there's no such thing.
|
||||
|
||||
❌ C: `},` closes (config){...} too early mid-function (most-recurring)
|
||||
.resources = {...
|
||||
}
|
||||
}, // ← `},` = `}` closed (config){...} + stray `,`
|
||||
.databases = {{...}}, // ← orphan in function body
|
||||
.modules = {sqlite}
|
||||
}; // orphan close
|
||||
```
|
||||
|
||||
#### Post-field-block boundary check
|
||||
|
||||
After every `.field = ...` block closes with `}`, the next non-whitespace token must be:
|
||||
- `,` → another `.field = ...` follows (still inside `(config){...}`). ✅
|
||||
- `};` → this was the last field, end of return. ✅
|
||||
- `},` → BUG: extra `}` closed `(config){...}` early plus stray `,`. **Failure mode C.** Delete one `}`.
|
||||
|
||||
**Trouble signals (any of these = bug):**
|
||||
|
||||
| You see | What broke |
|
||||
|---|---|
|
||||
| Two `};` lines in `mach()` | Failure mode A |
|
||||
| `};` `}` then `.field =` lines below | Failure mode B (file-scope orphans) |
|
||||
| `},` mid-function with `.field =` lines below | Failure mode C |
|
||||
| `.engine = sqlite_db` but no `.modules = {sqlite}` | Missing bundled module (see Modules) |
|
||||
| Indentation says fields are inside `(config){...}` but braces say otherwise | Trust braces, not indent |
|
||||
|
||||
### Self-check before emitting any snippet
|
||||
|
||||
1. **Strings:** every line of every multi-line SQL and template starts with `"` and ends with `"`. No raw newlines or bare HTML/SQL between strings.
|
||||
2. **Bare `"`:** no SQL identifiers in `"..."`. No HTML attributes in `attr="..."` — use `'...'` or `\"...\"`. **Shortcut: any `\"` in inline template → switch whole template to single quotes; faster than auditing.**
|
||||
3. **Brace count:** exactly one `};` (closes `(config){...}` + return) and one `}` (closes function). All `.fields` inside that one initializer, comma-separated.
|
||||
4. **No file-scope fields:** scan from closing `}` of `mach()` to end of file. Nothing between except other `config` functions or `#include`. If you see `.databases`/`.modules`/`.resources`/etc. floating, move them inside.
|
||||
5. **Modules registered:** every `.engine = X_db` requires `X` in `.modules`. Search snippet for `.modules` — zero matches = bug.
|
||||
|
||||
---
|
||||
|
||||
## CANONICAL WORKED SNIPPET — copy this shape for parent + children pipelines
|
||||
|
||||
This is the right shape for: project+tasks, blog+comments, order+line_items, user+sessions, playlist+tracks, anything with a parent-child relation. **Substitute names freely; do not change shape.**
|
||||
|
||||
```c
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
|
||||
config mach(){
|
||||
return (config){ // ← (config){ opens — depth 1
|
||||
.resources = { // .resources opens — depth 2
|
||||
{"project", "/projects/:id",
|
||||
.get = { // ← REQUIRED named field. Verb pipelines have NO
|
||||
// positional form — never put the pipeline
|
||||
// directly in {...} after the URL (that slot
|
||||
// is .steps middleware, not .get).
|
||||
validate({"id", .validation = validate_integer,
|
||||
.message = "must be an integer"}),
|
||||
|
||||
// Rule 3: ONE step, TWO items, SAME db (Rule 2: parent + child = one domain)
|
||||
query(
|
||||
{.set_key="project", .db="projects_db",
|
||||
.query="select id, name from projects where id = {{id}};"},
|
||||
{.set_key="tasks", .db="projects_db", // ← SAME db
|
||||
.query="select id, project_id, title from tasks where project_id = {{id}};"}
|
||||
),
|
||||
|
||||
join(.target_table_key="project", .target_field_key="id",
|
||||
.nested_table_key="tasks", .nested_field_key="project_id",
|
||||
.target_join_field_key="tasks"),
|
||||
|
||||
// Rule 1 Pattern C: enter {{#project}} for ALL parent fields,
|
||||
// including its own scalars (name, id) and nested children (tasks).
|
||||
render(.template =
|
||||
"{{#project}}"
|
||||
"<h1>{{name}}</h1>"
|
||||
"<p>Project ID: {{id}}</p>"
|
||||
"<ul class='tasks'>"
|
||||
"{{#tasks}}<li class='task'>{{title}}</li>{{/tasks}}"
|
||||
"</ul>"
|
||||
"{{/project}}")
|
||||
}
|
||||
}
|
||||
}, // ← close .resources (depth 2 → 1), comma to continue
|
||||
// ⚠️ depth IS 1 here. NEXT line must be another
|
||||
// `.field =` OR `};`. Writing `},` here = Failure mode C.
|
||||
|
||||
// Rule 2: ONE db, TWO migrations
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "projects_db",
|
||||
.connect = "file:projects.db?mode=rwc",
|
||||
.migrations = {
|
||||
"CREATE TABLE projects (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);",
|
||||
"CREATE TABLE tasks ("
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"project_id INTEGER NOT NULL REFERENCES projects(id),"
|
||||
"title TEXT NOT NULL"
|
||||
");"
|
||||
}
|
||||
}}, // ← close .databases (depth 2 → 1), comma to continue
|
||||
|
||||
.modules = {sqlite} // ← REQUIRED. `.engine = sqlite_db` above forces this.
|
||||
// Every .engine = X_db needs X in .modules.
|
||||
// Never omit "for brevity" — there is no elsewhere.
|
||||
}; // ← `};` closes (config){...} + return (depth 1 → 0).
|
||||
// This MUST be the only `};` in the function.
|
||||
} // ← `}` closes function body.
|
||||
```
|
||||
|
||||
**What the shape requires (all immutable regardless of names):**
|
||||
- ONE `.databases` entry containing migrations for parent AND child
|
||||
- ONE `query()` step with multiple items, all SAME `.db`
|
||||
- ONE `join()` between them
|
||||
- Template wraps everything in `{{#parent}}...{{/parent}}`, children iterated inside
|
||||
- `.modules = {<engine>}` listed once
|
||||
- Everything inside ONE `(config){...}` block, ONE `};`, ONE `}`
|
||||
|
||||
---
|
||||
|
||||
## MODULES — two different things called "module"
|
||||
|
||||
> **1. User-defined modules** = `config foo(){...}` functions you write to split an app into features. **Skip these for small/single-file snippets** — put everything in `config mach()`. Defining one without registering under `.modules = {foo, ...}` makes it dead code.
|
||||
>
|
||||
> **2. Bundled modules** = engine and feature modules shipped with MACH. **MUST be registered in `.modules` whenever you use what they provide.** `.modules` is never optional.
|
||||
|
||||
#### Module-registration trigger rule (mechanical)
|
||||
|
||||
Every `.engine = X_db` line requires `X` in `.modules`:
|
||||
|
||||
| `.engine =` | `.modules` must include |
|
||||
|---|---|
|
||||
| `sqlite_db` | `sqlite` |
|
||||
| `postgres_db` | `postgres` |
|
||||
| `mysql_db` | `mysql` |
|
||||
| `redis_db` | `redis` |
|
||||
| `duckdb_db` | `duckdb` |
|
||||
|
||||
Feature modules:
|
||||
|
||||
| You use | Add to `.modules` |
|
||||
|---|---|
|
||||
| `ds_sse(...)` | `datastar` |
|
||||
| `session()`, `logged_in()`, `login()`, `signup()` | `session_auth` |
|
||||
| `is_htmx` flag, HTMX-aware rendering | `htmx` |
|
||||
|
||||
**Pre-emit check:** search your output for the literal string `.modules`. Zero matches = bug. If `.modules` exists but missing an entry for an engine you used = bug. Apply at point of typing — memory-level reminders don't reliably fire mid-generation.
|
||||
|
||||
---
|
||||
|
||||
## REFERENCE
|
||||
|
||||
### Notation
|
||||
|
||||
`{}` = single value/struct. `{{}}` = array of structs. `query({...}, {...})` = multiple items in one step.
|
||||
|
||||
### Context
|
||||
|
||||
Per-request key-value store. Three scopes:
|
||||
- `input:xxx` — raw request params
|
||||
- `error:xxx` — validation/error data
|
||||
- (unprefixed) — app scope: query results, validated inputs, `.context` values
|
||||
|
||||
`validate()` promotes `input:` → app scope on success. `.context` seeds variables and assets at root, baked at compile time via `(asset){#embed "file"}`.
|
||||
|
||||
```c
|
||||
.context = {
|
||||
{"site_name", "MACH App"},
|
||||
{"layout", (asset){#embed "static/layout.mustache.html"}},
|
||||
{"get_todos", (asset){#embed "todos/get_todos.sql"}}
|
||||
}
|
||||
```
|
||||
|
||||
### Databases
|
||||
|
||||
Migrations forward-only, index-based, applied once each in array order, tracked in `mach_meta`. Seeds idempotent. Multi-tenant via `{{interpolation}}` in `.connect`; connections pooled with LRU.
|
||||
|
||||
**Engines:** `sqlite_db`, `postgres_db`, `mysql_db`, `redis_db`, `duckdb_db`
|
||||
|
||||
```c
|
||||
.databases = {{
|
||||
.engine = sqlite_db, .name = "blog_db",
|
||||
.connect = "file:{{user_id}}_blog.db?mode=rwc", // multi-tenant
|
||||
.migrations = {"CREATE TABLE blogs (...);", "CREATE TABLE comments (...);"},
|
||||
.seeds = {"INSERT OR IGNORE INTO blogs(...) VALUES(...);"}
|
||||
}}
|
||||
```
|
||||
|
||||
### Resource Pipelines
|
||||
|
||||
Each `.resources` entry is a named URL endpoint. Identified by name in `{{url:name}}`, `redirect()`, `reroute()` with colon-separated args (`name:arg1:arg2`).
|
||||
|
||||
URL helpers: `{{url:todos}}`, `{{url:todo:5}}` (literal), `{{url:todo:id}}` (context), `{{url:org_todo:acme:5}}` (multi-arg).
|
||||
|
||||
Path specificity automatic: `/todos/active` beats `/todos/:id`. Verb selection: HTTP method, or `?http_method=...` (lets HTML forms reach PATCH/DELETE/SSE).
|
||||
|
||||
**Fields:**
|
||||
- `.name` *(pos)*, `.url` *(pos)*, `.steps` *(pos)* — `.steps` is **middleware only**, runs before every verb
|
||||
- `.mime` — default response content type
|
||||
- `.get`, `.post`, `.put`, `.patch`, `.delete` — verb pipelines (**MUST be named fields, never positional**)
|
||||
- `.sse` — persistent SSE channel; first positional is channel name
|
||||
- `.errors` — terminal handlers keyed by error code
|
||||
- `.repairs` — resumable handlers keyed by error code
|
||||
|
||||
> **The third positional slot (`{...}` after the URL) is `.steps` middleware ONLY.** Do NOT put the GET/POST pipeline there expecting it to be inferred — verb pipelines have no positional form. They MUST use the named `.get =`, `.post =`, etc. fields. A resource with steps in the third slot but no `.get =` field has no GET handler and returns 404/405.
|
||||
>
|
||||
> ```c
|
||||
> ❌ {"project", "/projects/:id",
|
||||
> {validate(...), query(...), join(...), render(...)} // ← interpreted as
|
||||
> } // .steps middleware,
|
||||
> // no GET handler!
|
||||
>
|
||||
> ✅ {"project", "/projects/:id",
|
||||
> .get = { validate(...), query(...), join(...), render(...) } // ← named field
|
||||
> }
|
||||
>
|
||||
> ✅ {"project", "/projects/:id",
|
||||
> {session(), logged_in()}, // ← .steps middleware (small middleware steps)
|
||||
> .get = { ... }, // ← still need the named verb field
|
||||
> .post = { ... }
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> If your resource has only one verb (typical), use the named field directly and skip the `.steps` slot entirely — see the canonical worked snippet.
|
||||
|
||||
```c
|
||||
{"todo", "/todos/:id", {validate({"id", .validation="^\\d+$"})}, // ← {...} = .steps middleware
|
||||
.mime = mime_html,
|
||||
.get = { find({"get_todo", .set_key="todo", .db="todos_db"}), render("todo") }, // ← named .get
|
||||
.patch = { validate({"title", .validation=validate_not_empty}),
|
||||
query({.db="todos_db", .query="update todos set title={{title}} where id={{id}};"}),
|
||||
redirect("todo:{{id}}") },
|
||||
.delete = { query({.db="todos_db", .query="delete from todos where id={{id}};"}),
|
||||
redirect("todos") },
|
||||
.sse = {"todo/{{id}}", sse(.event="ready")},
|
||||
.errors = {{http_not_found, {render("404")}}}
|
||||
}
|
||||
```
|
||||
|
||||
**MIME:** `mime_html`, `mime_txt`, `mime_sse`, `mime_json`, `mime_js`
|
||||
|
||||
### Template Helpers
|
||||
|
||||
`{{helper:args}}` — colon-separated, never dot.
|
||||
|
||||
| Helper | Purpose |
|
||||
|---|---|
|
||||
| `{{raw:field}}` | unescaped output (default escapes) |
|
||||
| `{{precision:field:N}}` | numeric format, N decimals |
|
||||
| `{{input:field}}` | raw request param (form repop) |
|
||||
| `{{error:field}}` | truthy when field has error (use as section) |
|
||||
| `{{error_message:field}}`, `{{error_code:field}}` | error message / status code |
|
||||
| `{{url:name[:args]}}` | resource URL with positional args |
|
||||
| `{{asset:filename}}` | cache-busted URL for `public/` file |
|
||||
| `{{csrf:token}}`, `{{csrf:input}}` | CSRF token / hidden input (auto-verified) |
|
||||
|
||||
> **The table is exhaustive. Helpers not in it do not exist.** Other Mustache implementations have `{{length}}`, `{{count}}`, `{{size}}`, `{{first}}`, `{{last}}`, `{{index}}`, `{{#if}}`, `{{#each}}`, `{{else}}`, lambdas, partials. **None work in MACH.**
|
||||
>
|
||||
> **Supported template features:** field interpolation, `{{#name}}` (truthy/iteration), `{{^name}}` (falsy/empty), helpers above. HTML comments `<!--...-->` work; Mustache comments `{{!...}}` do NOT — use HTML.
|
||||
>
|
||||
> **Doing common things you might reach for missing helpers:**
|
||||
> - **Counts:** compute in SQL. `{.set_key="stats", .query="select count(*) as n from tasks where project_id = {{id}};"}` then `{{#stats}}{{n}} tasks{{/stats}}`.
|
||||
> - **Conditionals:** `{{#has_x}}...{{/has_x}}` where `has_x` is set by query/`exec()`. There is no `{{#if x}}`.
|
||||
> - **First/last:** different SQL — `limit 1`, `order by ... desc limit 1`.
|
||||
> - **Index in iteration:** `select row_number() over (...) as n` then `{{n}}`.
|
||||
>
|
||||
> **The template layer is a renderer, not a programming language.** If you need a feature not listed, do it in SQL or `exec()`, then render the result.
|
||||
|
||||
### Pipeline Steps
|
||||
|
||||
Every step accepts `.if_context` and `.unless_context`.
|
||||
|
||||
#### validate
|
||||
|
||||
Regex-checks request params. On success, promotes `input:name` → app scope. On failure, sets `error:name`, raises `http_bad_request`. All validations in one call complete before the error fires.
|
||||
|
||||
`.param_key` *(pos)*, `.validation` (regex/macro), `.message`, `.optional`, `.fallback`.
|
||||
|
||||
```c
|
||||
validate(
|
||||
{"email", .validation=validate_email, .message="must be email"},
|
||||
{"title", .validation=validate_not_empty, .message="cannot be empty"},
|
||||
{"page", .fallback="1", .validation="^\\d+$"},
|
||||
{"filter", .optional=true, .validation="^(active|done)$"}
|
||||
)
|
||||
```
|
||||
|
||||
**Built-ins:** `validate_not_empty`, `validate_alpha`, `validate_alphanumeric`, `validate_slug`, `validate_no_html`, `validate_integer`, `validate_positive`, `validate_float`, `validate_percentage`, `validate_email`, `validate_uuid`, `validate_username`, `validate_date`, `validate_time`, `validate_datetime`, `validate_url`, `validate_ipv4`, `validate_hex_color`, `validate_zipcode_us`, `validate_phone_e164`, `validate_cron`, `validate_no_sqli`, `validate_token`, `validate_base64`, `validate_boolean`, `validate_yes_no`, `validate_on_off`. Define your own: `#define validate_zipcode "^\\d{5}$"`.
|
||||
|
||||
#### find & query
|
||||
|
||||
Both run DB queries. `find()` raises `http_not_found` on zero rows; `query()` does not. Otherwise identical.
|
||||
|
||||
`.set_key` stores result as TABLE in context (always). SQL inlined with `.query` OR loaded by name from `.context` as positional (Rule 5 — asset must exist). Multiple items in one step run **concurrently** (Rule 3), even across dbs. `{{values}}` parameter-bound (Rule 4). Transactions: `BEGIN`/`COMMIT`/`ROLLBACK`.
|
||||
|
||||
`.template_key` *(pos)*, `.query`, `.set_key`, `.db`, `.if_context`/`.unless_context` *(per item)*.
|
||||
|
||||
```c
|
||||
query(
|
||||
{"get_todos", .set_key="todos", .db="todos_db"},
|
||||
{.set_key="count", .db="todos_db", .query="select count(*) as n from todos where user_id={{user_id}};"},
|
||||
{.if_context="show_urgent", .set_key="urgent", .db="todos_db",
|
||||
.query="select id, title from todos where priority='high';"}
|
||||
)
|
||||
```
|
||||
|
||||
#### join
|
||||
|
||||
Nests records from one context table into matching records of another, in memory. After `join()`, inner records live INSIDE outer records. Templates must enter outer section.
|
||||
|
||||
`.target_table_key`, `.target_field_key`, `.nested_table_key`, `.nested_field_key`, `.target_join_field_key` — see canonical snippet.
|
||||
|
||||
#### fetch
|
||||
|
||||
HTTP request → context. JSON parsed into tables/records; text stored as string. Multiple items run concurrently.
|
||||
|
||||
`.url` *(pos)*, `.set_key`, `.method` (default `http_get`), `.headers` (array of `{name, value}`), `.json`/`.text` (context key as body).
|
||||
|
||||
```c
|
||||
fetch("https://api.x.dev/charge",
|
||||
.set_key = "receipt", .method = http_post,
|
||||
.headers = {{"Authorization", "Bearer {{api_key}}"}, {"Idempotency-Key", "{{order_id}}"}},
|
||||
.json = "order")
|
||||
```
|
||||
|
||||
**Methods:** `http_get`, `http_post`, `http_put`, `http_patch`, `http_delete`, `http_sse_method`
|
||||
|
||||
#### exec
|
||||
|
||||
Calls C function/block with imperative context access. Dispatched to thread pool. Use for blocking I/O or CPU work. Trigger error pipeline via `error_set()`.
|
||||
|
||||
```c
|
||||
exec(^(){ auto t = get("challengers"); record_set(table_get(t,0), "x", record_get(table_get(t,1), "id")); })
|
||||
exec(.call = assign_opponents)
|
||||
```
|
||||
|
||||
**Imperative API:** `get/set/has/format`, `allocate/defer_free`, `error_set/get/has`, `table_new/count/get/add/remove/remove_at`, `record_new/set/get/remove`.
|
||||
|
||||
#### emit / task
|
||||
|
||||
```c
|
||||
emit("todo_created") // pub/sub event
|
||||
task("recount_todos") // enqueue async job
|
||||
```
|
||||
|
||||
#### sse / ds_sse
|
||||
|
||||
`sse(.channel="todos/{{user_id}}", .event="updated", .data={"id: {{id}}"})` — broadcasts on channel; without channel, sends only to requester.
|
||||
|
||||
`ds_sse(...)` — Datastar SSE (DOM updates + reactive state). Requires `.modules = {datastar}`. Fields: `.channel`, `.target`, `.mode` (`mode_outer`/`inner`/`replace`/`prepend`/`append`/`before`/`after`/`remove`), `.elements`, `.signals` (JSON), `.js`.
|
||||
|
||||
#### render
|
||||
|
||||
Outputs Mustache template. Auto-escapes (use `{{raw:field}}` to opt out). All field access follows Rule 1 — sections, never dot. Run Template Checklist on every `{{...}}` before emitting.
|
||||
|
||||
`.template_key` *(pos)*, `.template`, `.status`, `.mime`, `.engine` (`mustache` default, `mdm` for Markdown-with-Mustache), `.json_table_key` (serialize table as JSON).
|
||||
|
||||
```c
|
||||
render("todos")
|
||||
render(.template = "<h1>{{site_name}}</h1>")
|
||||
render("not_found", .status = http_not_found)
|
||||
render(.json_table_key = "todos")
|
||||
```
|
||||
|
||||
**Statuses:** `http_ok` (200), `http_created` (201), `http_redirect` (302), `http_bad_request` (400), `http_not_authorized` (401), `http_not_found` (404), `http_error` (500).
|
||||
|
||||
> Use either positional asset name (must exist in `.context`) OR inline `.template`. Don't reference phantom asset names.
|
||||
|
||||
#### headers / cookies / redirect / reroute / nest
|
||||
|
||||
```c
|
||||
headers({{"X-Request-Id", "{{request_id}}"}, {"Cache-Control", "no-store"}})
|
||||
cookies({{"session", "{{session_id}}"}})
|
||||
|
||||
redirect("todos") // 302 /todos
|
||||
redirect("todo:{{id}}") // 302 /todos/{id}
|
||||
reroute("todo:{{id}}") // server-side, in-process
|
||||
|
||||
nest({query({...}), render("urgent")}, .if_context="is_urgent") // shared conditional
|
||||
```
|
||||
|
||||
### Conditionals
|
||||
|
||||
Every step accepts `.if_context` (run when key present) / `.unless_context` (when absent). Works on validated inputs, query results, framework flags (`is_htmx`), or flags set by `exec()`.
|
||||
|
||||
```c
|
||||
render("fragment", .if_context="is_htmx")
|
||||
render("full_page", .unless_context="is_htmx")
|
||||
```
|
||||
|
||||
### Errors and Repairs
|
||||
|
||||
On failure, MACH searches handlers bottom-up: resource → module → root.
|
||||
- **Errors** terminal: send response, end request.
|
||||
- **Repairs** resumable: fix context, resume original pipeline at step after failure.
|
||||
|
||||
If no matching repair, falls through to errors. Error scope shared across `validate()` and `error_set()`: `{{error:name}}`, `{{error_code:name}}`, `{{error_message:name}}`. Raw input remains in `input:name`.
|
||||
|
||||
```c
|
||||
.errors = {
|
||||
{http_not_found, {render("404")}},
|
||||
{http_bad_request, {render("form")}},
|
||||
{http_error, {render("500")}}
|
||||
},
|
||||
.repairs = {{http_not_authorized, {exec(.call = refresh_session_token)}}}
|
||||
```
|
||||
|
||||
**Codes:** built-ins above; any int works. `#define err_quota_exceeded 723` for domain-specific.
|
||||
|
||||
### Events (cross-module pub/sub)
|
||||
|
||||
When `.publishes` exists anywhere, MACH creates `mach_events` to track delivery. Crashes don't drop events; replay on next boot.
|
||||
|
||||
```c
|
||||
// publisher
|
||||
.publishes = {{"todo_created", .with = {"user_id", "title"}}},
|
||||
.resources = {{"todos", "/todos", .post = {validate(...), query({"insert_todo", .db="todos_db"}),
|
||||
emit("todo_created"), redirect("todos")}}}
|
||||
|
||||
// subscriber (different module, OWN db per Rule 2)
|
||||
.events = {{"todo_created", {query({.db="activity_db",
|
||||
.query="insert into activities(kind, user_id) values('created', {{user_id}});"})}}}
|
||||
```
|
||||
|
||||
### Tasks
|
||||
|
||||
Async pipelines on task reactors. Triggered by `task("name")` or `.cron`. Durable: `mach_tasks` checkpoints context after each step; crash mid-task resumes at failed step.
|
||||
|
||||
```c
|
||||
.tasks = {
|
||||
{"recount_todos",
|
||||
{query({.db="todos_db", .query="update users set todo_count = ... where id = {{user_id}};"})},
|
||||
.accepts = {"user_id"}},
|
||||
{"daily_digest",
|
||||
{query({...}), emit("digest_ready")},
|
||||
.cron = "0 8 * * *"}
|
||||
}
|
||||
```
|
||||
|
||||
### Modules & Composition
|
||||
|
||||
Every app/module returns `config`. Root `main.c` defines `mach()`; modules define functions with any name. Module owns its resources, **its own db** (one per domain), migrations, templates, event contracts. Same-name conflicts: root wins. Modules communicate ONLY via pub/sub events.
|
||||
|
||||
`.name` — module identifier. `.modules` — modules to compose.
|
||||
|
||||
A module file:
|
||||
```c
|
||||
config blogs(){
|
||||
return (config){
|
||||
.name = "blogs",
|
||||
.resources = {{"blog", "/blogs/:id", .get = {/* validate→query→join→render */}}},
|
||||
.databases = {{
|
||||
.engine = sqlite_db, .name = "blog_db",
|
||||
.connect = "file:blogs.db?mode=rwc",
|
||||
.migrations = {
|
||||
"CREATE TABLE blogs (...);",
|
||||
"CREATE TABLE comments (... REFERENCES blogs(id) ...);"
|
||||
}
|
||||
}}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Bring in from `main.c`:
|
||||
```c
|
||||
#include "blogs/blogs.c"
|
||||
config mach(){ return (config){ .modules = {blogs, sqlite} }; }
|
||||
```
|
||||
|
||||
**Bundled modules (add to `.modules`):** `sqlite`, `postgres`, `mysql`, `redis`, `duckdb`, `htmx`, `datastar`, `tailwind`, `session_auth`.
|
||||
|
||||
**`session_auth` provides:** `session()` (attaches session, sets `user_id`), `logged_in()` (guard, raises `http_not_authorized`), `login()`, `logout()`, `signup()`. Common as resource middleware:
|
||||
```c
|
||||
{"dashboard", "/dashboard", {session(), logged_in()}, .get = {render("dashboard")}}
|
||||
```
|
||||
|
||||
### Static Files / External Deps
|
||||
|
||||
`public/` files served directly. Reference with `{{asset:filename}}` (content-checksummed URL, immutable cache).
|
||||
|
||||
External C libs: drop into `/vendor` (auto-discovered/linked), or use custom Dockerfile (`FROM mach:latest`, `apt-get install`).
|
||||
|
||||
Memory bridges: `allocate(N)` (per-request arena), `defer_free(ptr)` (cleanup at request end).
|
||||
|
||||
### Architecture (brief)
|
||||
|
||||
- **Boot once:** `mach()` runs at boot; config compiled into execution graph.
|
||||
- **Multi-reactor:** request reactors (one/CPU), task reactors (one/CPU), shared thread pool for `exec()` and blocking I/O.
|
||||
- **Memory:** per-request arena allocators; cleared at request end. Pipelines >5MB abort with 500.
|
||||
- **Safety:** SQL injection prevented by parameter binding (Rule 4). XSS prevented by `render()` auto-escape. CSRF prevented by `{{csrf:token}}`/`{{csrf:input}}`.
|
||||
|
||||
---
|
||||
|
||||
## PRE-EMIT SCAN — search your output for these strings before returning
|
||||
|
||||
| Search for | Failure |
|
||||
|---|---|
|
||||
| `.` between `{{` and `}}` (e.g. `{{x.y}}`) | Rule 1: dot notation |
|
||||
| Two `_db",` lines with different prefixes for ONE domain (e.g. `projects_db` + `tasks_db`) | Rule 2: parent+child split |
|
||||
| Multiple `query({` calls in one pipeline that don't depend on each other | Rule 3: serial when concurrent needed |
|
||||
| Positional asset name in `query()`/`find()` with no matching `.context` entry | Rule 5: phantom asset |
|
||||
| `config X()` not appearing in any `.modules = {...}` | Unregistered user module (dead code) |
|
||||
| `.engine = X_db` with no `X` in `.modules` | Missing bundled module (most-recurring regression) |
|
||||
| `{{#child}}` at root scope after a `join(child→parent)` | Pattern C: orphan section |
|
||||
| Bare `{{name}}` at root when `name` came from `set_key="X"` table | Pattern C: missing `{{#X}}` wrapper |
|
||||
| `\"` in inline template | Pattern 2b: switch whole template to `'...'` |
|
||||
| `{{length}}`, `{{count}}`, `{{#if}}`, `{{#each}}`, etc. | Improvised helpers — not in MACH |
|
||||
| `{{#a}}` opened twice in same scope | Overlapping sections (Check 4) |
|
||||
| `{{/x}` (one `}`), `{x}}`, `{{{x}}}`, `{ {x} }` | Malformed tag delimiters (Check 0) |
|
||||
| Two `};` in one `mach()` | Pattern 3 Failure A |
|
||||
| `};` then `}` then `.field =` lines | Pattern 3 Failure B (file-scope orphans) |
|
||||
| `},` between fields where `}` closed `(config){...}` | Pattern 3 Failure C |
|
||||
| `{{/x}}` with no matching `{{#x}}` earlier | Section balance (Check 4) |
|
||||
| `validate()` step missing when prompt asked for one | Prompt-following: re-read prompt |
|
||||
| Resource entry with `{...}` after URL containing the pipeline, but no `.get =` / `.post =` / etc. named field | Verb pipeline placed in `.steps` middleware slot — resource has no GET handler, returns 404. Use `.get = {...}` named field. |
|
||||
|
||||
If any match: stop and fix. When in doubt about parent + child: copy the **canonical worked snippet** above and rename.
|
||||