diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-13 17:23:58 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-13 17:23:58 +0100 |
| commit | 464dc81c4214960ed7eb3ddf9c3238755cf148a1 (patch) | |
| tree | 9a80214ff625cc57fe17ee80a750266f66f66fe6 /src/routes | |
| parent | c7b02f02ad0a7e2888f2d7d3599719e59bbd1ee2 (diff) | |
add HandlerInfo
Diffstat (limited to 'src/routes')
| -rw-r--r-- | src/routes/login.zig | 17 | ||||
| -rw-r--r-- | src/routes/root.zig | 57 |
2 files changed, 67 insertions, 7 deletions
diff --git a/src/routes/login.zig b/src/routes/login.zig new file mode 100644 index 0000000..29bf370 --- /dev/null +++ b/src/routes/login.zig @@ -0,0 +1,17 @@ +const std = @import("std"); + +pub fn handler( + request: *std.http.Server.Request, + allocator: std.mem.Allocator, +) anyerror!void { + var output = std.Io.Writer.Allocating.init(allocator); + var stringify = std.json.Stringify { .writer = &output.writer }; + + try stringify.write(.{ + .user = "nathan.reiner", + .name = "Nathan Reiner", + }); + + try output.writer.flush(); + try request.respond(output.written(), .{}); +} diff --git a/src/routes/root.zig b/src/routes/root.zig index 0330404..01952bb 100644 --- a/src/routes/root.zig +++ b/src/routes/root.zig @@ -1,15 +1,58 @@ const std = @import("std"); pub const fallback = @import("fallback.zig"); +pub const login = @import("login.zig"); -pub const Handler = *const fn ( - request: *std.http.Server.Request, - allocator: std.mem.Allocator, -) anyerror!void; +pub const HandlerInfo = struct { + handler: *const fn ( + request: *std.http.Server.Request, + allocator: std.mem.Allocator, + ) anyerror!void, + needs_auth: bool, + method: std.http.Method, -pub const handlers = std.StaticStringMap(Handler).initComptime(.{ + pub fn handle( + self: *const @This(), + request: *std.http.Server.Request, + allocator: std.mem.Allocator, + ) !void { + if (request.head.method != self.method) { + try request.respond("{ \"error\": \"Bad Request\" }", .{ .status = .bad_request }); + } + + self.handler(request, allocator) catch |err| { + const response, const status_code: std.http.Status = switch (err) { + error.BadRequest => .{ "{ \"error\": \"Bad Request\" }", .bad_request }, + error.Unauthorized => .{ "{ \"error\": \"Unauthorized\" }", .unauthorized }, + error.Forbidden => .{ "{ \"error\": \"Forbidden\" }", .forbidden }, + error.NotFound => .{ "{ \"error\": \"Not Found\" }", .not_found }, + else => .{ "{ \"error\": \"Internal Server Error\" }", .internal_server_error }, + }; + + try request.respond(response, .{ .status = status_code }); + }; + } +}; + +pub const handlers = std.StaticStringMap(HandlerInfo).initComptime(.{ + .{ + "", + HandlerInfo { + .handler = fallback.handler, + .needs_auth = false, + .method = .GET, + } + }, + .{ + "/api/login", + HandlerInfo { + .handler = login.handler, + .needs_auth = false, + .method = .POST, + } + }, }); -pub fn get(path: []const u8) Handler { - return handlers.get(path) orelse fallback.handler; +pub fn get(path: []const u8) HandlerInfo { + return (handlers.getLongestPrefix(std.mem.trimEnd(u8, path, "/")) orelse unreachable).value; } |