diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-05-20 10:16:50 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-05-20 10:16:50 +0200 |
| commit | 44445734ba4a0f05564f808c2fd34a7f23c6fafb (patch) | |
| tree | 3bd22add718620cbf931286f55137bcc666100be /src/routes/root.zig | |
| parent | 95ae9f15e1db335a7f8358d811ea37e2c89dd10f (diff) | |
Let's do a rewrite!
Created project structure and changed `build.zig` and added licence.
Diffstat (limited to 'src/routes/root.zig')
| -rw-r--r-- | src/routes/root.zig | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/src/routes/root.zig b/src/routes/root.zig deleted file mode 100644 index c438893..0000000 --- a/src/routes/root.zig +++ /dev/null @@ -1,49 +0,0 @@ -const std = @import("std"); - -pub const HandlerInfo = @import("handler-info.zig"); - -pub const api = @import("api/root.zig"); -pub const static: HandlerInfo = .from_type(@import("static.zig")); - -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; - } -}; |