diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-08-14 21:45:25 +0100 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-08-22 10:10:02 +0100 |
commit | 6a8a204020449fee3d8ef5e6175932e3731389f0 (patch) | |
tree | 35af7c5a1c19fa95c3c85d106cbc748fc20fc982 /zig-comments | |
parent | ff092976a9fdeeba96a0de13d013b9d838640c40 (diff) | |
download | mfashby.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')
-rw-r--r-- | zig-comments/.vscode/launch.json | 16 | ||||
-rw-r--r-- | zig-comments/build.zig | 1 | ||||
m--------- | zig-comments/lib/mustache-zig | 0 | ||||
-rw-r--r-- | zig-comments/src/main.zig | 40 | ||||
-rw-r--r-- | zig-comments/src/pq.zig | 10 | ||||
-rw-r--r-- | zig-comments/src/templates/comments.html | 10 |
6 files changed, 61 insertions, 16 deletions
diff --git a/zig-comments/.vscode/launch.json b/zig-comments/.vscode/launch.json new file mode 100644 index 0000000..08ebd2a --- /dev/null +++ b/zig-comments/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/zig-out/bin/comments", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +}
\ No newline at end of file diff --git a/zig-comments/build.zig b/zig-comments/build.zig index 21102a7..b7f3bcc 100644 --- a/zig-comments/build.zig +++ b/zig-comments/build.zig @@ -69,6 +69,7 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + unit_tests.addModule("mustache", mustache); const run_unit_tests = b.addRunArtifact(unit_tests); diff --git a/zig-comments/lib/mustache-zig b/zig-comments/lib/mustache-zig -Subproject cfcd483639458793d7d7818cf1c89cc37bb6344 +Subproject a228c9e46d1e06c28e419c0656cfc43f553086a 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(); } diff --git a/zig-comments/src/pq.zig b/zig-comments/src/pq.zig index 52220fa..a4fc382 100644 --- a/zig-comments/src/pq.zig +++ b/zig-comments/src/pq.zig @@ -60,7 +60,7 @@ pub const Stmt = struct { db: Db, query: [:0]const u8, aa: std.heap.ArenaAllocator, - + n_params: usize = 0, param_values: [MAX_PARAMS][*c]const u8 = undefined, did_exec: bool = false, @@ -71,7 +71,9 @@ pub const Stmt = struct { pub fn deinit(self: *Stmt) void { self.aa.deinit(); - pq.PQclear(self.c_res); + if (self.c_res != null) { + pq.PQclear(self.c_res); + } } pub fn step(self: *Stmt) !bool { @@ -132,7 +134,7 @@ pub const Stmt = struct { const ti = @typeInfo(T); var t: T = undefined; inline for (ti.Struct.fields) |field| { - const name: [:0]const u8 = &addZ(field.name.len,field.name[0..].*); + const name: [:0]const u8 = &addZ(field.name.len, field.name[0..].*); const val = try self.read_columnN(name, field.type); @field(t, field.name) = val; } @@ -143,7 +145,7 @@ pub const Stmt = struct { // https://github.com/ziglang/zig/issues/16116 pub fn addZ(comptime length: usize, value: [length]u8) [length:0]u8 { var terminated_value: [length:0]u8 = undefined; - terminated_value[length] = 0; + terminated_value[length] = 0; @memcpy(&terminated_value, &value); return terminated_value; } diff --git a/zig-comments/src/templates/comments.html b/zig-comments/src/templates/comments.html index a1d8b76..e2fc6da 100644 --- a/zig-comments/src/templates/comments.html +++ b/zig-comments/src/templates/comments.html @@ -1,9 +1,9 @@ <ul class="comments"> -{{ #comments }} +{{#comments}} <li class="comment"> - <span class="comment author">{{ author }}</span> - <p class="comment content">{{ comment }}</p> - <span class="comment timestamp">{{ ts }}</span> + <span class="comment author">{{author}}</span> + <p class="comment content">{{comment}}</p> + <span class="comment timestamp">{{ts}}</span> </li> -{{ /comments }} +{{/comments}} </ul> |