aboutsummaryrefslogtreecommitdiff
path: root/zig-comments/src/main.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-08-14 21:45:25 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-08-22 10:10:02 +0100
commit6a8a204020449fee3d8ef5e6175932e3731389f0 (patch)
tree35af7c5a1c19fa95c3c85d106cbc748fc20fc982 /zig-comments/src/main.zig
parentff092976a9fdeeba96a0de13d013b9d838640c40 (diff)
downloadmfashby.net-6a8a204020449fee3d8ef5e6175932e3731389f0.tar.gz
mfashby.net-6a8a204020449fee3d8ef5e6175932e3731389f0.tar.bz2
mfashby.net-6a8a204020449fee3d8ef5e6175932e3731389f0.tar.xz
mfashby.net-6a8a204020449fee3d8ef5e6175932e3731389f0.zip
Misc fixes:
- fix template in comments.html: whitespace before tag names is not allowed - update mustache-zig version - null-terminate field names in zig struts, for C interop with PQfnumber function
Diffstat (limited to 'zig-comments/src/main.zig')
-rw-r--r--zig-comments/src/main.zig40
1 files changed, 33 insertions, 7 deletions
diff --git a/zig-comments/src/main.zig b/zig-comments/src/main.zig
index 951f5f0..0a7bca1 100644
--- a/zig-comments/src/main.zig
+++ b/zig-comments/src/main.zig
@@ -75,17 +75,43 @@ fn constresponse(res: *std.http.Server.Response, rr: []const u8, status: std.htt
try res.finish();
}
-fn get_comments(res: *std.http.Server.Response, ctx: Ctx, _: Params) Err!void {
- _ = ctx;
- // Run SQL
- // Render comments template
- //zws.parse
+fn get_comments(res: *std.http.Server.Response, ctx: Ctx, params: Params) Err!void {
+ _ = params;
+ var p = try zws.Path.parse(res.allocator, res.request.target);
+ defer p.deinit();
+ const url: []const u8 = try p.get_query_param("url") orelse {
+ try badrequest(res, ctx);
+ return;
+ };
+
+ const Comment = struct {
+ author: []const u8,
+ comment: []const u8,
+ ts: []const u8,
+ };
+ var comments = std.ArrayList(Comment).init(res.allocator);
+ var stmt = try ctx.db.prepare_statement(res.allocator,
+ \\ select author,comment,ts from comments where url = $1 order by ts
+ );
+ defer stmt.deinit();
+ try stmt.bind(0, url);
+ while (try stmt.step()) {
+ const cmt = try stmt.read_struct(Comment);
+ try comments.append(cmt);
+ }
const rr = @embedFile("templates/comments.html");
- res.transfer_encoding = .{ .content_length = rr.len };
+ const tt = comptime mustache.parseComptime(rr, .{}, .{});
+ res.transfer_encoding = .chunked;
try res.headers.append("content-type", "text/html");
try res.do();
- try res.writeAll(rr);
+
+ const data = struct {
+ comments: []const Comment,
+ };
+ try mustache.render(tt, data{
+ .comments = comments.items,
+ }, res.writer());
try res.finish();
}