From e8ff326f0e5bd61fb0a8c22c48f296fcf0249830 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 15 Nov 2025 11:54:00 +0100 Subject: backend implement auth-service --- src/routes/static.zig | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/routes/static.zig (limited to 'src/routes/static.zig') diff --git a/src/routes/static.zig b/src/routes/static.zig new file mode 100644 index 0000000..8719cc9 --- /dev/null +++ b/src/routes/static.zig @@ -0,0 +1,36 @@ +const std = @import("std"); +const mime = @import("../mime.zig"); +const Context = @import("context.zig"); + +const log = std.log.scoped(.fallback); + +pub const access = .everyone; + +pub fn get(ctx: *Context) anyerror![]const u8 { + var static = try std.fs.cwd().openDir("static", .{}); + defer static.close(); + + if (static.openFile(ctx.request.head.target[1..], .{})) |file| { + defer file.close(); + 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 (ctx.request.head.target.len == 1) static + else (static.openDir(ctx.request.head.target[1..], .{}) catch { + return error.NotFound; + }); + defer if (ctx.request.head.target.len > 1) subdir.close(); + + if (subdir.openFile("index.html", .{})) |file| { + defer file.close(); + 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", .{ ctx.request.head.target }); + return error.NotFound; + } + } +} -- cgit v1.2.3-70-g09d2