diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-15 11:54:00 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-15 11:56:24 +0100 |
| commit | e8ff326f0e5bd61fb0a8c22c48f296fcf0249830 (patch) | |
| tree | 19d8c026f8460ec577f446a7d61e68c38acddde0 /src/routes/root.zig | |
| parent | 007bc3fe0615f38cb31d5116759ed3500f6fd3e6 (diff) | |
backend implement auth-service
Diffstat (limited to 'src/routes/root.zig')
| -rw-r--r-- | src/routes/root.zig | 45 |
1 files changed, 39 insertions, 6 deletions
diff --git a/src/routes/root.zig b/src/routes/root.zig index dbfce32..878b9f2 100644 --- a/src/routes/root.zig +++ b/src/routes/root.zig @@ -4,14 +4,47 @@ pub const HandlerInfo = @import("handler-info.zig"); pub const Context = @import("context.zig"); pub const api = @import("api/root.zig"); -pub const fallback: HandlerInfo = .from_type(@import("fallback.zig")); +pub const static: HandlerInfo = .from_type(@import("static.zig")); -pub const handlers = std.StaticStringMap(HandlerInfo).initComptime(.{ - .{ "", fallback }, - .{ "/api/auth/login", api.auth.login }, - .{ "/api/auth/first-login", api.auth.first_login }, -}); +const routes = (Routes {}) + .with("", static) + .with_module("/api", api); + +pub const handlers = std.StaticStringMap(HandlerInfo).initComptime(routes.items); pub fn get(path: []const u8) HandlerInfo { return (handlers.getLongestPrefix(std.mem.trimEnd(u8, path, "/")) orelse unreachable).value; } + +const Routes = struct { + const Self = @This(); + + const Route = struct{ []const u8, HandlerInfo }; + + items: []const Route = &[0]Route{}, + + pub fn with(self: Self, comptime route: []const u8, handler: HandlerInfo) Self { + var next = self; + const tail: []const Route = &[1]Route { .{ route, handler } }; + next.items = self.items ++ tail; + return next; + } + + pub fn with_module(self: Self, comptime prefix: []const u8, module: type) Self { + var next = self; + + if (@typeInfo(module) != .@"struct") return self; + + inline for (@typeInfo(module).@"struct".decls) |decl| { + const field = @field(module, decl.name); + const route = prefix ++ "/" ++ decl.name; + switch (@TypeOf(field)) { + type => next = next.with_module(route, field), + HandlerInfo => next = next.with(route, field), + else => {}, + } + } + + return next; + } +}; |