-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.awk
More file actions
161 lines (154 loc) · 4.09 KB
/
Copy pathapp.awk
File metadata and controls
161 lines (154 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# SPDX-License-Identifier: MIT
# app.awk -- H-awk todo demo (DSL style)
#
# Routes:
# GET / landing page (static shell)
# GET /todos todo list HTML fragment (HTMX) — <tr> rows
# POST /todos add todo, return new <tr> (HTMX)
# DELETE /todos/:id delete todo, return empty (HTMX outerHTML swap)
# GET /todos.json JSON API
BEGIN {
hawk.app.get("/", "todo_index")
hawk.app.get("/todos", "todo_list_html")
hawk.app.post("/todos", "todo_add")
hawk.app.del("/todos/:id", "todo_delete")
hawk.app.get("/todos.json", "todo_list_json")
hawk.app.listen(env.get("PORT") ?? 8080)
}
function todo_index() -> Response {
ctx.res.render("views/index.html")
}
function todo_list_html() -> Response {
when cache.get("todos:html") of
ok r:
when r of
some v:
return ctx.res.html(safe.html.raw(v))
none:
# fall through to rebuild
end
ng _:
# fall through to rebuild on cache error
end
let rows = []
let n: Int = read_tsv("data/todos.tsv", rows)
let mut out: Str = ""
let mut i: Int
for (i = 1; i <= n; i++) {
out = out _todo_tr(rows[i, "id"], rows[i, "title"])
}
when cache.set("todos:html", out, 30) of
ok _:
# noop
ng _:
# noop on cache error
end
return ctx.res.html(safe.html.raw(out))
}
function todo_add() -> Response {
let raw_title = ctx.req.form("title")
when raw_title of
ok raw:
if (raw == "") {
let raw_q ?= ctx.req.query("title")
raw = raw_q
}
if (raw == "") {
ctx.res.status(400)
return ctx.res.text("title required")
}
let row: Dict<Str, Str> = {}
row["id"] = "#{systime()}_#{int(rand() * 100000)}"
row["title"] = safe.str.trust(raw)
append_tsv("data/todos.tsv", row)
when cache.del("todos:html") of
ok _:
# noop
ng _:
return ctx.res.status(500)
end
when cache.del("todos:json") of
ok _:
# noop
ng _:
return ctx.res.status(500)
end
ctx.res.status(201)
return ctx.res.html(_todo_tr(row["id"], row["title"]))
ng err:
ctx.res.status(400)
return ctx.res.text("bad request")
end
}
function todo_delete() -> Response {
let raw_id ?= ctx.req.param("id")
let deleted: Int = delete_tsv("data/todos.tsv", "id", raw_id)
if (deleted == 0) {
ctx.res.status(404)
return ctx.res.text("not found")
}
when cache.del("todos:html") of
ok _:
# noop
ng _:
return ctx.res.status(500)
end
when cache.del("todos:json") of
ok _:
# noop
ng _:
return ctx.res.status(500)
end
ctx.res.status(200)
return ctx.res.html(safe.html.raw(""))
}
function todo_list_json() -> Response {
when cache.get("todos:json") of
ok r:
when r of
some v:
return ctx.res.json_raw(v)
none:
# fall through to rebuild
end
ng _:
# fall through to rebuild on cache error
end
let rows = []
let n: Int = read_tsv("data/todos.tsv", rows)
let item: Dict<Str, Str> = {}
let mut items_json: Str = ""
let mut i: Int
for (i = 1; i <= n; i++) {
item["id"] = rows[i, "id"]
item["title"] = rows[i, "title"]
let sep: Str = (i > 1 ? "," : "")
items_json = "#{items_json}#{sep}#{json_encode(item)}"
}
let result: Str = "{\"count\":#{n},\"items\":[#{items_json}]}"
when cache.set("todos:json", result, 30) of
ok _:
# noop
ng _:
# noop on cache error
end
return ctx.res.json_raw(result)
}
function _todo_tr(id: Str, title: Str) -> HtmlFragment {
let safe_title = safe.html.escape(title)
let safe_id = safe.attr.escape(id)
let html: Str =
"<tr class=\"task-row\">"
"<td class=\"task-bullet\">◆</td>"
"<td class=\"task-title\">#{safe_title}</td>"
"<td class=\"task-del\">"
"<button"
" hx-delete=\"/todos/#{safe_id}\""
" hx-target=\"closest tr\""
" hx-swap=\"outerHTML\""
" class=\"btn-del\""
">[ rm ]</button>"
"</td>"
"</tr>"
return safe.html.raw(html)
}