aboutsummaryrefslogtreecommitdiff
path: root/src/routes/static.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-11-17 13:09:02 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2025-11-17 13:09:02 +0100
commit6201307fecf8398a1b53bf276bc08bfbb3524899 (patch)
tree2e623f4779b310a81b49dbb146146f8a694d9ec8 /src/routes/static.zig
parent9c979a6fefdfc6709b3576014520d219e02c3649 (diff)
implement memora.Stream
Diffstat (limited to 'src/routes/static.zig')
-rw-r--r--src/routes/static.zig10
1 files changed, 3 insertions, 7 deletions
diff --git a/src/routes/static.zig b/src/routes/static.zig
index ef7d493..f52d178 100644
--- a/src/routes/static.zig
+++ b/src/routes/static.zig
@@ -8,16 +8,14 @@ const log = std.log.scoped(.fallback);
pub const access = .everyone;
-pub fn get(ctx: *Context) anyerror![]const u8 {
+pub fn get(ctx: *Context) anyerror!memora.Stream {
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;
+ return .from_file(file);
} else |_| {
var subdir = if (ctx.request.head.target.len == 1) static
else (static.openDir(ctx.request.head.target[1..], .{}) catch {
@@ -26,10 +24,8 @@ pub fn get(ctx: *Context) anyerror![]const u8 {
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;
+ return .from_file(file);
} else |_| {
log.warn("File '{s}' Not Found", .{ ctx.request.head.target });
return error.NotFound;