Skip to content

Commit

Permalink
feat(embed_file): add option to serve pre-encoded file
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Dec 25, 2024
1 parent c185704 commit eced8cb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/benchmark/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn main() !void {
defer t.deinit();

var router = try Router.init(allocator, &.{
Route.init("/").serve_embedded_file(http.Mime.HTML, @embedFile("index.html")).layer(),
Route.init("/").embed_file(.{ .mime = http.Mime.HTML }, @embedFile("index.html")).layer(),
Route.init("/hi/%s").get({}, hi_handler).layer(),
}, .{});
defer router.deinit(allocator);
Expand Down
2 changes: 1 addition & 1 deletion examples/multithread/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn main() !void {
defer t.deinit();

var router = try Router.init(allocator, &.{
Route.init("/").serve_embedded_file(http.Mime.HTML, @embedFile("index.html")).layer(),
Route.init("/").embed_file(.{ .mime = http.Mime.HTML }, @embedFile("index.html")).layer(),
Route.init("/hi/%s").get({}, hi_handler).layer(),
Route.init("/redirect").get({}, redir_handler).layer(),
Route.init("/post").post({}, post_handler).layer(),
Expand Down
2 changes: 1 addition & 1 deletion examples/sse/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn main() !void {
defer broadcast.deinit();

var router = try Router.init(allocator, &.{
Route.init("/").serve_embedded_file(http.Mime.HTML, @embedFile("index.html")).layer(),
Route.init("/").embed_file(.{ .mime = http.Mime.HTML }, @embedFile("index.html")).layer(),
Route.init("/kill").get({}, kill_handler).layer(),
Route.init("/stream").get({}, sse_handler).layer(),
Route.init("/message").post(&broadcast, msg_handler).layer(),
Expand Down
4 changes: 2 additions & 2 deletions examples/tls/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub fn main() !void {
defer t.deinit();

var router = try Router.init(allocator, &.{
Route.init("/embed/pico.min.css").serve_embedded_file(
http.Mime.CSS,
Route.init("/embed/pico.min.css").embed_file(
.{ .mime = http.Mime.CSS },
@embedFile("embed/pico.min.css"),
).layer(),

Expand Down
7 changes: 7 additions & 0 deletions src/http/encoding.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub const Encoding = enum {
gzip,
compress,
deflate,
br,
zstd,
};
1 change: 1 addition & 0 deletions src/http/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub const Method = @import("method.zig").Method;
pub const Request = @import("request.zig").Request;
pub const Response = @import("response.zig").Response;
pub const Mime = @import("mime.zig").Mime;
pub const Encoding = @import("encoding.zig").Encoding;
pub const Date = @import("date.zig").Date;
pub const Headers = @import("../core/case_string_map.zig").CaseStringMap([]const u8);

Expand Down
21 changes: 18 additions & 3 deletions src/http/router/route.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Method = @import("../method.zig").Method;
const Request = @import("../request.zig").Request;
const Response = @import("../response.zig").Response;
const Mime = @import("../mime.zig").Mime;
const Encoding = @import("../encoding.zig").Encoding;

const FsDir = @import("fs_dir.zig").FsDir;
const Context = @import("../context.zig").Context;
Expand Down Expand Up @@ -166,10 +167,17 @@ pub const Route = struct {
return inner_route(.PATCH, self, data, handler_fn);
}

const ServeEmbeddedOptions = struct {
/// If you are serving a compressed file, please
/// set the correct encoding type.
encoding: ?Encoding = null,
mime: ?Mime = null,
};

/// Define a GET handler to serve an embedded file.
pub fn serve_embedded_file(
pub fn embed_file(
self: *const Self,
comptime mime: ?Mime,
comptime opts: ServeEmbeddedOptions,
comptime bytes: []const u8,
) Self {
return self.get({}, struct {
Expand Down Expand Up @@ -205,9 +213,16 @@ pub const Route = struct {
}
}

if (opts.encoding) |encoding| {
ctx.response.headers.put_assume_capacity(
"Content-Encoding",
@tagName(encoding),
);
}

return try ctx.respond(.{
.status = .OK,
.mime = mime,
.mime = opts.mime,
.body = bytes,
});
}
Expand Down

0 comments on commit eced8cb

Please sign in to comment.