const std = @import("std"); const httpz = @import("httpz"); const Io = std.Io; const Allocator = std.mem.Allocator; const PORT = 8807; // This example shows more advanced routing example, namely route groups // and route configuration. (The previous example, with middleware, also showed // per-route configuration for middleware specifically). pub fn main(init: std.process.Init) !void { const allocator = init.gpa; var default_handler = Handler{ .io = init.io, .log = true, }; var nolog_handler = Handler{ .io = init.io, .log = false, }; var server = try httpz.Server(*Handler).init(init.io, allocator, .{ .address = .localhost(PORT) }, &default_handler); defer server.deinit(); // ensures a clean shutdown, finishing off any existing requests // see 09_shutdown.zig for how to to break server.listen with an interrupt defer server.stop(); var router = try server.router(.{}); router.get("/", index, .{}); // We can define a dispatch function per-route. This will be used instead of Handler.dispatch // But, sadly, every dispatch method must have the same signature (they must all accept the same type of action) router.get("/page1", page1, .{ .dispatcher = Handler.infoDispatch }); // We can define a handler instance per-route. This will be used instead of the // handler instance passed to the init method above. router.get("/page2", page2, .{ .handler = &nolog_handler }); std.debug.print("listening http://localhost:{d}/\n", .{PORT}); // Starts the server, this is blocking. try server.listen(); } const Handler = struct { io: Io, log: bool, // special dispatch set in the info route pub fn infoDispatch(h: *Handler, action: httpz.Action(*Handler), req: *httpz.Request, res: *httpz.Response) !void { return action(h, req, res); } pub fn dispatch(h: *Handler, action: httpz.Action(*Handler), req: *httpz.Request, res: *httpz.Response) !void { try action(h, req, res); if (h.log) { std.debug.print("ts={d} path={s} status={d}\n", .{ Io.Timestamp.now(h.io, .real), req.url.path, res.status }); } } }; fn index(_: *Handler, _: *httpz.Request, res: *httpz.Response) !void { res.content_type = .HTML; res.body = \\ \\

It's possible to define a custom dispatch method, custom handler instance and/or custom middleware per-route. \\

It's also possible to create a route group, which is a group of routes who share a common prefix and/or a custom configration. \\