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 | |
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
-rw-r--r-- | comments/src/main.rs | 2 | ||||
-rwxr-xr-x | deploy.sh | 20 | ||||
-rw-r--r-- | zig-comments/src/main.zig | 20 | ||||
-rw-r--r-- | zig-comments/src/templates/form.html | 6 |
4 files changed, 30 insertions, 18 deletions
diff --git a/comments/src/main.rs b/comments/src/main.rs index 7064bca..50f568f 100644 --- a/comments/src/main.rs +++ b/comments/src/main.rs @@ -51,7 +51,7 @@ async fn main() { .unwrap_or_else(|_| "/api".to_string()); let db_connection_str = std::env::var("DATABASE_URL") - .unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string()); + .unwrap_or_else(|_| "postgres://comments@localhost/comments".to_string()); let notification_address = std::env::var("NOTIFICATION_ADDRESS").ok(); let mail_opts = notification_address.map(|na| { @@ -6,12 +6,20 @@ set -e hugo # Build comments app -pushd comments +#pushd comments +#if [ $(uname -m) != "aarch64" ] +#then +# RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" +#fi +#cargo build --target=aarch64-unknown-linux-gnu --release +#popd +pushd zig-comments if [ $(uname -m) != "aarch64" ] then - RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" + echo "must build on aarch64 for now" + exit(1) fi -cargo build --target=aarch64-unknown-linux-gnu --release +zig build -Doptimize=ReleaseSafe popd # TODO update caddy with offline message while site being updated @@ -21,8 +29,10 @@ rsync -rz public/* root@rpi3:/var/www/mfashby.net # Copy comments app and reboot ssh root@rpi3 -C systemctl stop comments -rsync comments/target/aarch64-unknown-linux-gnu/release/comments root@rpi3:/usr/local/bin/comments -rsync comments/comments.service root@rpi3:/etc/systemd/system/comments.service +#rsync comments/target/aarch64-unknown-linux-gnu/release/comments root@rpi3:/usr/local/bin/comments +#rsync comments/comments.service root@rpi3:/etc/systemd/system/comments.service +rsync zig-comments/zig-out/bin/comments root@rpi3:/usr/local/bin/comments +rsync zig-comments/comments.service root@rpi3:/etc/systemd/system/comments.service ssh root@rpi3 -C systemctl daemon-reload ssh root@rpi3 -C systemctl restart comments 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> |