diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-14 21:55:59 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-14 21:55:59 +0100 |
| commit | 3f18f02d07802d1fc705a500e5978a9b3cb2e751 (patch) | |
| tree | 283970a2f5a693706456b853c550eeaa669b5d72 /src/routes/fallback.zig | |
| parent | 351ad457f0ff95e20301a146b8c88a8f0f659aa1 (diff) | |
implement login
Diffstat (limited to 'src/routes/fallback.zig')
| -rw-r--r-- | src/routes/fallback.zig | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/src/routes/fallback.zig b/src/routes/fallback.zig index 78d778b..25e1927 100644 --- a/src/routes/fallback.zig +++ b/src/routes/fallback.zig @@ -1,32 +1,37 @@ const std = @import("std"); -const http = @import("../http.zig"); +const mime = @import("../mime.zig"); +const Context = @import("context.zig"); const log = std.log.scoped(.fallback); -pub fn handler( - request: *std.http.Server.Request, - allocator: std.mem.Allocator, -) anyerror!void { +pub const needs_auth = false; +pub const method = .GET; + +pub fn handler(ctx: *Context) anyerror![]const u8 { var static = try std.fs.cwd().openDir("static", .{}); defer static.close(); - if (static.openFile(request.head.target[1..], .{})) |file| { + if (static.openFile(ctx.request.head.target[1..], .{})) |file| { defer file.close(); - try http.respond_file(request, file, request.head.target[1..], allocator); + const content = file.readToEndAlloc(ctx.allocator, std.math.maxInt(usize)); + const mime_type = mime.get_type(ctx.request.head.target); + ctx.response.headers.content_type = mime_type; + return content; } else |_| { - var subdir = if (request.head.target.len == 1) static - else (static.openDir(request.head.target[1..], .{}) catch { - try request.respond("Not Found", .{ .status = .not_found }); - return; + var subdir = if (ctx.request.head.target.len == 1) static + else (static.openDir(ctx.request.head.target[1..], .{}) catch { + return error.NotFound; }); - defer if (request.head.target.len > 1) subdir.close(); + defer if (ctx.request.head.target.len > 1) subdir.close(); if (subdir.openFile("index.html", .{})) |file| { defer file.close(); - try http.respond_file(request, file, "index.html", allocator); + const content = file.readToEndAlloc(ctx.allocator, std.math.maxInt(usize)); + ctx.response.headers.content_type = "text/html"; + return content; } else |_| { - log.warn("File '{s}' Not Found", .{ request.head.target }); - try request.respond("Not Found", .{ .status = .not_found }); + log.warn("File '{s}' Not Found", .{ ctx.request.head.target }); + return error.NotFound; } } } |