diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-26 15:45:33 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-26 15:45:33 +0100 |
| commit | feeedb0bf31314b182e7bbc135182f6e0bc859b2 (patch) | |
| tree | 163948cd874729925c2bf57d0927619065d2322d /src/main.zig | |
| parent | 91d9b6992ac0ebee13704a3faefa8fd41a00075e (diff) | |
update zilch
Diffstat (limited to 'src/main.zig')
| -rw-r--r-- | src/main.zig | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig index ec3b2fd..308d308 100644 --- a/src/main.zig +++ b/src/main.zig @@ -14,8 +14,32 @@ const routes: http.RouteSet = .init(&([_]http.handler.Interface{ http.handler.Static(.index, site.app.html, "text/html").interface(), http.handler.Static(.{"index.wasm"}, wasm, "application/wasm").interface(), http.handler.Static(.{"icon.png"}, @embedFile("favicon.png"), "image/png").interface(), + images.ctx.interface(), }) ++ api.interfaces); +const images = struct { + const Ctx = http.handler.Context(.{ "image", .id }); + const ctx: Ctx = .{ + .get = get, + }; + + pub fn get(request: Ctx.Request) http.Response { + const id = request.params.id.as(usize) catch return .badRequest(.string("invalid id")); + + const storage = std.Io.Dir.cwd().openDir(request.io, "storage", .{}) catch return .with(.internal_server_error, .void); + defer storage.close(request.io); + + var allocating: std.Io.Writer.Allocating = .init(request.gpa); + defer allocating.deinit(); + + allocating.writer.print("{}.jpg", .{id}) catch return .with(.internal_server_error, .void); + + const file = storage.openFile(request.io, allocating.written(), .{}) catch return .badRequest(.string("invalid id")); + + return .ok(.file(file)); + } +}; + pub fn main(init: std.process.Init) !void { try http.serve(init.gpa, routes, .{ .address = try .parseLiteral("0.0.0.0:8080"), |