diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-11 10:34:59 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2025-11-11 10:34:59 +0100 |
| commit | 402888423de9764c22175df4cc41d79157895f3d (patch) | |
| tree | b954cac6454bb00ed39cc87ffc3d23c8da948ef4 /src/routes/fallback.zig | |
| parent | ce06aafe385f217bb0756089a88255f31f093018 (diff) | |
add basic server
Diffstat (limited to 'src/routes/fallback.zig')
| -rw-r--r-- | src/routes/fallback.zig | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/routes/fallback.zig b/src/routes/fallback.zig new file mode 100644 index 0000000..78d778b --- /dev/null +++ b/src/routes/fallback.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const http = @import("../http.zig"); + +const log = std.log.scoped(.fallback); + +pub fn handler( + request: *std.http.Server.Request, + allocator: std.mem.Allocator, +) anyerror!void { + var static = try std.fs.cwd().openDir("static", .{}); + defer static.close(); + + if (static.openFile(request.head.target[1..], .{})) |file| { + defer file.close(); + try http.respond_file(request, file, request.head.target[1..], allocator); + } 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; + }); + defer if (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); + } else |_| { + log.warn("File '{s}' Not Found", .{ request.head.target }); + try request.respond("Not Found", .{ .status = .not_found }); + } + } +} |