aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-08-28 20:22:21 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-08-28 20:22:21 +0100
commit9075971696cae25228779e9deb9dba113b3d1b43 (patch)
tree57fc964986570483f624269f31ac6476e8c9749b
parent570d9a2503cfd06303c96f40253f80d8cee84766 (diff)
downloadmfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.tar.gz
mfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.tar.bz2
mfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.tar.xz
mfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.zip
Switch to zig package manager instead of using submodules
Add smtp library Send a notification on comment.
-rw-r--r--.gitmodules6
-rw-r--r--Caddyfile2
-rw-r--r--comments/build.zig18
-rw-r--r--comments/build.zig.zon18
m---------comments/lib/mustache-zig0
m---------comments/lib/zigwebserver0
-rw-r--r--comments/src/main.zig18
-rw-r--r--comments/src/templates/notification.txt4
8 files changed, 54 insertions, 12 deletions
diff --git a/.gitmodules b/.gitmodules
index e6a03aa..a38e1fb 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,9 +1,3 @@
[submodule "hugo-xmin"]
path = hugo-xmin
url = git@github.com:yihui/hugo-xmin
-[submodule "zig-comments/lib/zigwebserver"]
- path = comments/lib/zigwebserver
- url = git://code.mfashby.net:/zigwebserver
-[submodule "zig-comments/lib/mustache-zig"]
- path = comments/lib/mustache-zig
- url = https://github.com/batiati/mustache-zig
diff --git a/Caddyfile b/Caddyfile
index 03411cb..4106cea 100644
--- a/Caddyfile
+++ b/Caddyfile
@@ -6,7 +6,7 @@ http://localhost:8080 {
root * public
# API server
- cgi /api/* zig-comments/zig-out/bin/comments
+ cgi /api/* comments/zig-out/bin/comments
# Redirects for old assets
redir /10-11-21-longboard-slides.mp4 /assets/10-11-21-longboard-slides.mp4 permanent
diff --git a/comments/build.zig b/comments/build.zig
index b7f3bcc..a678527 100644
--- a/comments/build.zig
+++ b/comments/build.zig
@@ -15,6 +15,8 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
+ const opts = .{ .target = target, .optimize = optimize };
+
const exe = b.addExecutable(.{
.name = "comments",
// In this case the main source file is merely a path, however, in more
@@ -24,15 +26,21 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
- const zws = b.addModule("zws", .{ .source_file = .{ .path = "lib/zigwebserver/src/zigwebserver.zig" } });
- exe.addModule("zws", zws);
+ const zws = b.dependency("zigwebserver", opts);
+ exe.addModule("zws", zws.module("zigwebserver"));
+ exe.linkLibrary(zws.artifact("zigwebserver"));
exe.linkLibC();
exe.linkSystemLibrary("libpq");
exe.addIncludePath(.{ .path = "/usr/include" });
- const mustache = b.addModule("mustache", .{ .source_file = .{ .path = "lib/mustache-zig/src/mustache.zig" } });
- exe.addModule("mustache", mustache);
+ const mustache = b.dependency("mustache", opts);
+ exe.addModule("mustache", mustache.module("mustache"));
+ exe.linkLibrary(mustache.artifact("mustache-static"));
+
+ const smtp = b.dependency("smtp", opts);
+ exe.addModule("smtp", smtp.module("smtp"));
+ exe.linkLibrary(smtp.artifact("smtp"));
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
@@ -69,7 +77,7 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
- unit_tests.addModule("mustache", mustache);
+ unit_tests.addModule("mustache", mustache.module("mustache"));
const run_unit_tests = b.addRunArtifact(unit_tests);
diff --git a/comments/build.zig.zon b/comments/build.zig.zon
new file mode 100644
index 0000000..2acd629
--- /dev/null
+++ b/comments/build.zig.zon
@@ -0,0 +1,18 @@
+.{
+ .name = "comments",
+ .version = "0.0.1",
+ .dependencies = .{
+ .smtp = .{
+ .url = "https://code.mfashby.net/smtp-zig/snapshot/smtp-zig-main.tar.xz",
+ .hash = "12205e7087360185eea859ae9265cc4be54c519d5f8d205f66f655d4f8e7e0045dc1",
+ },
+ .mustache = .{
+ .url = "https://github.com/MFAshby/mustache-zig/archive/refs/heads/stage3.tar.gz",
+ .hash = "1220fdf239b6ece687324bb600c72d5ad7a278df2ece86599aee6adf932064686cef",
+ },
+ .zigwebserver = .{
+ .url = "https://code.mfashby.net/zigwebserver/snapshot/zigwebserver-main.tar.xz",
+ .hash = "122066ea438ccbe3e006e84563746705cd46f3c7d1389c8fa8cccf2cdf681959495e",
+ }
+ }
+} \ No newline at end of file
diff --git a/comments/lib/mustache-zig b/comments/lib/mustache-zig
deleted file mode 160000
-Subproject da5aabb77b8ae6669b0436f09c3305c8f227caa
diff --git a/comments/lib/zigwebserver b/comments/lib/zigwebserver
deleted file mode 160000
-Subproject 0736c8a26dbd970670117290baf4a68e430aeca
diff --git a/comments/src/main.zig b/comments/src/main.zig
index 898b689..882202d 100644
--- a/comments/src/main.zig
+++ b/comments/src/main.zig
@@ -2,6 +2,7 @@ const std = @import("std");
const zws = @import("zws");
const pq = @import("pq.zig");
const mustache = @import("mustache");
+const smtp = @import("smtp");
const Params = zws.Params;
@@ -244,6 +245,23 @@ fn post_comment(res: *Response, ctx: Ctx, _: Params) Err!void {
_ = try stmt.step();
}
+ // Send me an email
+ const rr = @embedFile("templates/notification.txt");
+ const tt = comptime mustache.parseComptime(rr, .{}, .{});
+ const Data = struct { url: []const u8, author: []const u8, comment: []const u8 };
+ const notification = try mustache.allocRender(res.allocator, tt, Data{
+ .url = form_val.url,
+ .author = form_val.author,
+ .comment = form_val.comment,
+ });
+ const smtp_username = std.os.getenv("SMTP_USERNAME") orelse "comments@mfashby.net";
+ const smtp_password = std.os.getenv("SMTP_PASSWORD") orelse "foobar";
+ const notification_address = std.os.getenv("NOTIFICATION_ADDRESS") orelse "martin@mfashby.net";
+ const smtp_server = std.os.getenv("SMTP_SERVER") orelse "mail.mfashby.net:587";
+ smtp.send_mail(res.allocator, smtp_server, .{ .user = smtp_username, .pass = smtp_password }, smtp_username, &[_][]const u8{notification_address}, notification) catch |err| {
+ std.log.err("failed to send notification email {}", .{err});
+ };
+
// And redirect!
res.transfer_encoding = .none;
res.status = .found;
diff --git a/comments/src/templates/notification.txt b/comments/src/templates/notification.txt
index ba85056..3824f52 100644
--- a/comments/src/templates/notification.txt
+++ b/comments/src/templates/notification.txt
@@ -1,3 +1,7 @@
+subject: New comment on {{ url }}
+from: comments@mfashby.net
+to: martin@mfashby.net
+
New comment on {{ url }}:
Author: {{ author }}