aboutsummaryrefslogtreecommitdiff
path: root/src/routes/root.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-11-14 21:55:59 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2025-11-14 21:55:59 +0100
commit3f18f02d07802d1fc705a500e5978a9b3cb2e751 (patch)
tree283970a2f5a693706456b853c550eeaa669b5d72 /src/routes/root.zig
parent351ad457f0ff95e20301a146b8c88a8f0f659aa1 (diff)
implement login
Diffstat (limited to 'src/routes/root.zig')
-rw-r--r--src/routes/root.zig55
1 files changed, 7 insertions, 48 deletions
diff --git a/src/routes/root.zig b/src/routes/root.zig
index 01952bb..dbfce32 100644
--- a/src/routes/root.zig
+++ b/src/routes/root.zig
@@ -1,56 +1,15 @@
const std = @import("std");
-pub const fallback = @import("fallback.zig");
-pub const login = @import("login.zig");
+pub const HandlerInfo = @import("handler-info.zig");
-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 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 Context = @import("context.zig");
+pub const api = @import("api/root.zig");
+pub const fallback: HandlerInfo = .from_type(@import("fallback.zig"));
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,
- }
- },
+ .{ "", fallback },
+ .{ "/api/auth/login", api.auth.login },
+ .{ "/api/auth/first-login", api.auth.first_login },
});
pub fn get(path: []const u8) HandlerInfo {