const std = @import("std"); const httpz = @import("httpz"); const Allocator = std.mem.Allocator; const PORT = 8801; // This example demonstrates basic httpz usage, with focus on using the // httpz.Request and httpz.Response objects. pub fn main(init: std.process.Init) !void { const allocator = init.gpa; // We pass a "void" handler. This is the simplest, but limits what we can do // The last parameter is an instance of our handler. Since we have // a void handler, we pass a void value: i.e. {}. var server = try httpz.Server(void).init(init.io, allocator, .{ .address = .localhost(PORT), .request = .{ // httpz has a number of tweakable configuration settings (see readme) // by default, it won't read form data. We need to configure a max // field count (since one of our examples reads form data) .max_form_count = 20, }, }, {}); 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. The last parameter is a Route Config. For these basic // examples, we aren't using it. // Other support methods: post, put, delete, head, trace, options and all router.get("/", index, .{}); router.get("/hello", hello, .{}); router.get("/json/hello/:name", json, .{}); router.get("/writer/hello/:name", writer, .{}); router.get("/metrics", metrics, .{}); router.get("/form_data", formShow, .{}); router.post("/form_data", formPost, .{}); router.get("/explicit_write", explicitWrite, .{}); std.debug.print("listening http://localhost:{d}/\n", .{PORT}); // Starts the server, this is blocking. try server.listen(); } fn index(_: *httpz.Request, res: *httpz.Response) !void { res.body = \\ \\