const std = @import("std"); const httpz = @import("httpz"); const Allocator = std.mem.Allocator; const PORT = 8802; // This example demonstrates using a custom Handler. It shows how to have // global state (here we show a counter, but it could be a more complex struct // including things such as a DB pool) and how to define not found and error // handlers. pub fn main(init: std.process.Init) !void { const allocator = init.gpa; // We specify our "Handler" and, as the last parameter to init, pass an // instance of it. var handler = Handler{}; var server = try httpz.Server(*Handler).init(init.io, allocator, .{ .address = .localhost(PORT) }, &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(.{}); // Register routes. router.get("/", index, .{}); router.get("/hits", hits, .{}); router.get("/error", @"error", .{}); std.debug.print("listening http://localhost:{d}/\n", .{PORT}); // Starts the server, this is blocking. try server.listen(); } const Handler = struct { _hits: usize = 0, // If the handler defines a special "notFound" function, it'll be called // when a request is made and no route matches. pub fn notFound(_: *Handler, _: *httpz.Request, res: *httpz.Response) !void { res.status = 404; res.body = "NOPE!"; } // If the handler defines the special "uncaughtError" function, it'll be // called when an action returns an error. // Note that this function takes an additional parameter (the error) and // returns a `void` rather than a `!void`. pub fn uncaughtError(_: *Handler, req: *httpz.Request, res: *httpz.Response, err: anyerror) void { std.debug.print("uncaught http error at {s}: {}\n", .{ req.url.path, err }); // Alternative to res.content_type = .TYPE // useful for dynamic content types, or content types not defined in // httpz.ContentType res.headers.add("content-type", "text/html; charset=utf-8"); res.status = 505; res.body = "(╯°□°)╯︵ ┻━┻"; } }; fn index(_: *Handler, _: *httpz.Request, res: *httpz.Response) !void { res.body = \\ \\
Except in very simple cases, you'll want to use a custom Handler. \\
A custom Handler is how you share app-specific data with your actions (like a DB pool) \\ and define a custom not found and error function. \\
Other examples show more advanced things you can do with a custom Handler. \\