From 89aae265bc3cfd03f4081ef1a8f034e538fef702 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 14 Aug 2023 23:03:57 +0100 Subject: align zig and rust implementations of comments switch deploy script to zig version --- zig-comments/src/main.zig | 20 +++++++++++--------- zig-comments/src/templates/form.html | 6 +++--- 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'zig-comments/src') diff --git a/zig-comments/src/main.zig b/zig-comments/src/main.zig index 0a7bca1..b8b820c 100644 --- a/zig-comments/src/main.zig +++ b/zig-comments/src/main.zig @@ -24,17 +24,17 @@ const router = Rtr{ .handlers = &[_]Rtr.Handler{ .{ .method = .GET, - .pattern = "/api/comments", - .handle_fn = get_comments, + .pattern = "/api/comment", + .handle_fn = get_comment, }, .{ .method = .POST, - .pattern = "/api/comments", - .handle_fn = post_comments, + .pattern = "/api/comment", + .handle_fn = post_comment, }, .{ .method = .GET, - .pattern = "/api/form/*", + .pattern = "/api/form", .handle_fn = get_form, }, }, @@ -42,13 +42,14 @@ const router = Rtr{ }; pub fn main() !void { - var db = try pq.Db.init("postgresql://comments@localhost/comments"); + const db_url = std.os.getenv("DATABASE_URL") orelse "postgresql://comments@localhost/comments"; + var db = try pq.Db.init(db_url); try db.exec(@embedFile("migrations/0_init.sql")); try db.exec(@embedFile("migrations/1_capcha.sql")); defer db.deinit(); const server = zws.Server(Ctx, Rtr){ .allocator = gpa.allocator(), - .address = std.net.Address{ .in = std.net.Ip4Address.init(.{ 127, 0, 0, 1 }, 8080) }, + .address = std.net.Address{ .in = std.net.Ip4Address.init(.{ 127, 0, 0, 1 }, 5678) }, .context = Ctx{ .db = db }, .handler = router, }; @@ -75,7 +76,7 @@ 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: Params) Err!void { +fn get_comment(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(); @@ -99,6 +100,7 @@ fn get_comments(res: *std.http.Server.Response, ctx: Ctx, params: Params) Err!vo const cmt = try stmt.read_struct(Comment); try comments.append(cmt); } + std.log.debug("found {} comments for url {s}", .{ comments.items.len, url }); const rr = @embedFile("templates/comments.html"); const tt = comptime mustache.parseComptime(rr, .{}, .{}); @@ -115,7 +117,7 @@ fn get_comments(res: *std.http.Server.Response, ctx: Ctx, params: Params) Err!vo try res.finish(); } -fn post_comments(res: *std.http.Server.Response, ctx: Ctx, _: Params) Err!void { +fn post_comment(res: *std.http.Server.Response, ctx: Ctx, _: Params) Err!void { var body_aa = std.ArrayList(u8).init(res.allocator); try res.reader().readAllArrayList(&body_aa, 1_000_000); var body = try body_aa.toOwnedSlice(); diff --git a/zig-comments/src/templates/form.html b/zig-comments/src/templates/form.html index 8c492ea..3ec8eeb 100644 --- a/zig-comments/src/templates/form.html +++ b/zig-comments/src/templates/form.html @@ -1,6 +1,6 @@ -
-
-
+ +
+



-- cgit v1.2.3-ZIG