diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-08-14 23:03:57 +0100 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-08-22 10:10:02 +0100 |
commit | 89aae265bc3cfd03f4081ef1a8f034e538fef702 (patch) | |
tree | a507bf8b33eb10bf3a3143f1f5483b339eb0da9b /zig-comments | |
parent | 6a8a204020449fee3d8ef5e6175932e3731389f0 (diff) | |
download | mfashby.net-89aae265bc3cfd03f4081ef1a8f034e538fef702.tar.gz mfashby.net-89aae265bc3cfd03f4081ef1a8f034e538fef702.tar.bz2 mfashby.net-89aae265bc3cfd03f4081ef1a8f034e538fef702.tar.xz mfashby.net-89aae265bc3cfd03f4081ef1a8f034e538fef702.zip |
align zig and rust implementations of comments
switch deploy script to zig version
Diffstat (limited to 'zig-comments')
-rw-r--r-- | zig-comments/src/main.zig | 20 | ||||
-rw-r--r-- | zig-comments/src/templates/form.html | 6 |
2 files changed, 14 insertions, 12 deletions
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 @@ -<form action="/api/comments" method="post"> - <input type="hidden" name="url" value="{{ url }}"><br> - <input type="hidden" name="capcha_id" value="{{ capcha_id }}"><br> +<form action="/api/comment" method="post"> + <input type="hidden" name="url" value="{{url}}"><br> + <input type="hidden" name="capcha_id" value="{{capcha_id`}}"><br> <label for="author">Name:</label><br> <input type="text" id="author" name="author"><br> <label for="comment">Comment:</label><br> |