Skip to content

Commit

Permalink
http/router: asynchronous fs serving
Browse files Browse the repository at this point in the history
  • Loading branch information
mookums committed Oct 22, 2024
1 parent fb57b4e commit ff2de71
Show file tree
Hide file tree
Showing 11 changed files with 1,306 additions and 1,277 deletions.
3 changes: 2 additions & 1 deletion examples/http/basic/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ pub fn main() !void {
}.handler_fn));

var server = http.Server(.plain, .auto).init(.{
.router = &router,
.allocator = allocator,
.threading = .single,
});
defer server.deinit();

try server.bind(host, port);
try server.listen(.{ .router = &router });
try server.listen();
}
28 changes: 24 additions & 4 deletions examples/http/fs/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ pub fn main() !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;

const allocator = std.heap.page_allocator;
var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){ .backing_allocator = std.heap.c_allocator };
const allocator = gpa.allocator();
defer _ = gpa.deinit();

var router = http.Router.init(allocator);
defer router.deinit();

try router.serve_route("/", http.Route.init().get(struct {
pub fn handler_fn(_: http.Request, response: *http.Response, _: http.Context) void {
pub fn handler_fn(ctx: *http.Context) void {
const body =
\\ <!DOCTYPE html>
\\ <html>
Expand All @@ -23,17 +25,35 @@ pub fn main() !void {
\\ </html>
;

response.set(.{
ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
.body = body[0..],
});
}
}.handler_fn));

try router.serve_route("/kill", http.Route.init().get(struct {
pub fn handler_fn(ctx: *http.Context) void {
ctx.runtime.stop();

ctx.respond(.{
.status = .OK,
.mime = http.Mime.HTML,
.body = "",
});
}
}.handler_fn));

try router.serve_fs_dir("/static", "./examples/http/fs/static");

var server = http.Server(.plain, .auto).init(.{ .allocator = allocator });
var server = http.Server(.plain, .auto).init(.{
.allocator = allocator,
.threading = .auto,
.size_connections_max = 256,
});
defer server.deinit();

try server.bind(host, port);
try server.listen(.{ .router = &router });
}
1 change: 0 additions & 1 deletion src/core/lib.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub const Job = @import("job.zig").Job;
pub const Pseudoslice = @import("pseudoslice.zig").Pseudoslice;
pub const Server = @import("server.zig").Server;
Loading

0 comments on commit ff2de71

Please sign in to comment.