commit a9455f14e4a7038aee8c64a286e166ef7ee42e6c
parent 723eb8a0abe9c89d60afa4518f91a4a161fa17b9
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date: Thu, 19 Nov 2026 15:09:13 -0300
Take listen address and port as event service flags.
-address and -port (also --flag and -flag=value), in the same style as
the auth service, defaulting to localhost:16000. The address accepts
localhost or an IPv4/IPv6 literal. Invalid arguments print the usage
and exit with status 2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat:
4 files changed, 151 insertions(+), 10 deletions(-)
diff --git a/event_registration/build.zig b/event_registration/build.zig
@@ -50,6 +50,16 @@ pub fn build(b: *std.Build) void {
}),
});
+ const options = b.addLibrary(.{
+ .name = "options",
+ .root_module = b.createModule(.{
+ .root_source_file = b.path("src/options/options.zig"),
+ .target = target,
+ .optimize = optimize,
+ }),
+ });
+
+ exe.root_module.addImport("options", options.root_module);
exe.root_module.addImport("repository", repo.root_module);
exe.root_module.addImport("server", server.root_module);
exe.root_module.addImport("auth", auth.root_module);
@@ -67,4 +77,6 @@ pub fn build(b: *std.Build) void {
const auth_tests = b.addTest(.{ .root_module = auth.root_module });
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&b.addRunArtifact(auth_tests).step);
+ const options_tests = b.addTest(.{ .root_module = options.root_module });
+ test_step.dependOn(&b.addRunArtifact(options_tests).step);
}
diff --git a/event_registration/src/main.zig b/event_registration/src/main.zig
@@ -1,23 +1,37 @@
const std = @import("std");
const Io = std.Io;
-const repository = @import("repository");
const server = @import("server");
const auth = @import("auth");
+const options = @import("options");
pub fn main(init: std.process.Init) !void {
const io = init.io;
const allocator = init.gpa;
- // var db = try repository.start();
- // std.debug.print("db: {any}\n", .{db});
- // defer db.deinit();
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ var diag: options.Diagnostic = .{};
+ const opts = options.parse(args[1..], &diag) catch |err| {
+ switch (err) {
+ error.HelpRequested => {
+ std.debug.print("{s}", .{options.usage});
+ return;
+ },
+ error.UnknownFlag => std.debug.print("unknown argument: {s}\n", .{diag.arg}),
+ error.MissingValue => std.debug.print("missing value for {s}\n", .{diag.arg}),
+ error.InvalidPort => std.debug.print("invalid port: {s}\n", .{diag.arg}),
+ }
+ std.debug.print("{s}", .{options.usage});
+ std.process.exit(2);
+ };
+
+ const address = options.resolveAddress(io, opts.address, opts.port) catch {
+ std.debug.print("invalid address: {s} (use localhost or an IP)\n", .{opts.address});
+ std.process.exit(2);
+ };
// Base URL of the Go auth service.
const auth_url = init.environ_map.get("SIGA_AUTH_URL") orelse auth.default_url;
- var server_future = Io.async(io, server.start, .{ io, allocator, auth_url });
-
- // const event = try db.find(57);
- // std.debug.print("found event => id: '{}', name: '{s}'\n", .{event.id, event.name});
+ var server_future = Io.async(io, server.start, .{ io, allocator, address, auth_url });
_ = try server_future.await(io);
}
diff --git a/event_registration/src/options/options.zig b/event_registration/src/options/options.zig
@@ -0,0 +1,109 @@
+//! Command line options, in the same style as the Go auth service:
+//! `-address <host>` and `-port <port>`. `--flag` and `-flag=value` also work.
+
+const std = @import("std");
+
+pub const default_address = "localhost";
+pub const default_port: u16 = 16000;
+
+pub const usage =
+ \\usage: registration [-address <ip>] [-port <port>]
+ \\
+ \\ -address address to listen to (default "localhost")
+ \\ -port port to listen to (default 16000)
+ \\
+;
+
+pub const Options = struct {
+ address: []const u8 = default_address,
+ port: u16 = default_port,
+};
+
+pub const ParseError = error{ HelpRequested, UnknownFlag, MissingValue, InvalidPort };
+
+/// On error, `diag` holds the offending argument.
+pub const Diagnostic = struct {
+ arg: []const u8 = "",
+};
+
+/// Parses `args`, without the program name.
+pub fn parse(args: []const [:0]const u8, diag: *Diagnostic) ParseError!Options {
+ var opts: Options = .{};
+ var i: usize = 0;
+ while (i < args.len) : (i += 1) {
+ const arg: []const u8 = args[i];
+ diag.arg = arg;
+
+ if (arg.len < 2 or arg[0] != '-') return error.UnknownFlag;
+ const flag = if (std.mem.startsWith(u8, arg, "--")) arg[2..] else arg[1..];
+
+ var name = flag;
+ var inline_value: ?[]const u8 = null;
+ if (std.mem.indexOfScalar(u8, flag, '=')) |eq| {
+ name = flag[0..eq];
+ inline_value = flag[eq + 1 ..];
+ }
+
+ if (std.mem.eql(u8, name, "h") or std.mem.eql(u8, name, "help")) {
+ return error.HelpRequested;
+ }
+
+ const is_address = std.mem.eql(u8, name, "address");
+ const is_port = std.mem.eql(u8, name, "port");
+ if (!is_address and !is_port) return error.UnknownFlag;
+
+ const value = inline_value orelse blk: {
+ i += 1;
+ if (i >= args.len) return error.MissingValue;
+ break :blk args[i];
+ };
+
+ if (is_address) {
+ if (value.len == 0) return error.MissingValue;
+ opts.address = value;
+ } else {
+ diag.arg = value;
+ opts.port = std.fmt.parseInt(u16, value, 10) catch return error.InvalidPort;
+ }
+ }
+ return opts;
+}
+
+/// Turns the `-address` value into an IP address. Accepts `localhost` and
+/// IPv4/IPv6 literals; other host names are not resolved.
+pub fn resolveAddress(io: std.Io, address: []const u8, port: u16) !std.Io.net.IpAddress {
+ if (std.ascii.eqlIgnoreCase(address, "localhost")) {
+ return .{ .ip4 = .{ .bytes = .{ 127, 0, 0, 1 }, .port = port } };
+ }
+ return std.Io.net.IpAddress.resolve(io, address, port);
+}
+
+test "defaults" {
+ var diag: Diagnostic = .{};
+ const opts = try parse(&.{}, &diag);
+ try std.testing.expectEqualStrings("localhost", opts.address);
+ try std.testing.expectEqual(16000, opts.port);
+}
+
+test "flag forms" {
+ var diag: Diagnostic = .{};
+ const a = try parse(&.{ "-address", "0.0.0.0", "-port", "8000" }, &diag);
+ try std.testing.expectEqualStrings("0.0.0.0", a.address);
+ try std.testing.expectEqual(8000, a.port);
+
+ const b = try parse(&.{ "--address=::1", "--port=9" }, &diag);
+ try std.testing.expectEqualStrings("::1", b.address);
+ try std.testing.expectEqual(9, b.port);
+}
+
+test "errors" {
+ var diag: Diagnostic = .{};
+ try std.testing.expectError(error.MissingValue, parse(&.{"-port"}, &diag));
+ try std.testing.expectError(error.InvalidPort, parse(&.{ "-port", "70000" }, &diag));
+ try std.testing.expectEqualStrings("70000", diag.arg);
+ try std.testing.expectError(error.InvalidPort, parse(&.{"-port=abc"}, &diag));
+ try std.testing.expectError(error.UnknownFlag, parse(&.{"-verbose"}, &diag));
+ try std.testing.expectEqualStrings("-verbose", diag.arg);
+ try std.testing.expectError(error.UnknownFlag, parse(&.{"solto"}, &diag));
+ try std.testing.expectError(error.HelpRequested, parse(&.{"-h"}, &diag));
+}
diff --git a/event_registration/src/server/server.zig b/event_registration/src/server/server.zig
@@ -16,7 +16,12 @@ const Response = struct {
var db: repository.Repository = undefined;
var authenticator: auth.Auth = undefined;
-pub fn start(io: std.Io, allocator: std.mem.Allocator, auth_url: []const u8) !void {
+pub fn start(
+ io: std.Io,
+ allocator: std.mem.Allocator,
+ address: std.Io.net.IpAddress,
+ auth_url: []const u8,
+) !void {
db = try repository.init();
defer db.deinit();
@@ -27,7 +32,7 @@ pub fn start(io: std.Io, allocator: std.mem.Allocator, auth_url: []const u8) !vo
var server = try httpz.Server(void).init(
io,
allocator,
- .{ .address = .localhost(16000) },
+ .{ .address = .{ .ip = address } },
{},
);
defer {
@@ -39,6 +44,7 @@ pub fn start(io: std.Io, allocator: std.mem.Allocator, auth_url: []const u8) !vo
router.get("/event/:id", handleGetEvent, .{});
router.post("/event", handlePostEvent, .{});
+ std.log.info("listening on {f}", .{address});
try server.listen();
}