···11# An HTTP/1.1 server for Zig.
2233+## Zig Version
44+This is for Zig 0.16.0. Use the [zig-0.15.2](https://github.com/karlseguin/http.zig/tree/zig-0.15) branch for Zig 0.15 or the [dev](https://github.com/karlseguin/http.zig/tree/dev) which may or may not be up to date with zig dev.
55+36```zig
47const std = @import("std");
58const httpz = @import("httpz");
6977-pub fn main() !void {
88- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
99- const allocator = gpa.allocator();
1010+pub fn main(init: std.process.Init) !void {
1111+ const allocator = init.gpa;
10121113 // More advance cases will use a custom "Handler" instead of "void".
1214 // The last parameter is our handler instance, since we have a "void"
1315 // handler, we passed a void ({}) value.
1414- var server = try httpz.Server(void).init(allocator, .{
1616+ var server = try httpz.Server(void).init(init.io, allocator, .{
1517 // use .all(5882) to bind to all interfaces, i.e. 0.0.0.0
1618 .address = .localhost(5882),
1719 }, {});
···115117const std = @import("std");
116118const httpz = @import("httpz");
117119118118-pub fn main() !void {
119119- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
120120- const allocator = gpa.allocator();
120120+pub fn main(init: std.process.Init) !void {
121121+ const allocator = init.gpa;
121122122122- var db = try pg.Pool.init(allocator, .{
123123+ var db = try pg.Pool.init(init.io, allocator, .{
123124 .connect = .{ .port = 5432, .host = "localhost"},
124125 .auth = .{.username = "user", .database = "db", .password = "pass"}
125126 });
···129130 .db = db,
130131 };
131132132132- var server = try httpz.Server(*App).init(allocator, .{.address = .localhost(5882)}, &app);
133133+ var server = try httpz.Server(*App).init(init.io, allocator, .{.address = .localhost(5882)}, &app);
133134 var router = try server.router(.{});
134135 router.get("/api/user/:id", getUser, .{});
135136 try server.listen();
···265266const std = @import("std");
266267const httpz = @import("httpz");
267268268268-pub fn main() !void {
269269- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
270270- const allocator = gpa.allocator();
269269+pub fn main(init: std.process.Init) !void {
270270+ const allocator = init.gpa;
271271272272- var server = try httpz.Server(void).init(allocator, .{.address = .localhost(5882)}, {});
272272+ var server = try httpz.Server(void).init(init.io, allocator, .{.address = .localhost(5882)}, {});
273273274274 // overwrite the default notFound handler
275275 server.notFound(notFound);
···700700You can specify a separate configuration for each route. To change the configuration for a group of routes, you have two options. The first, is to directly change the router's `handler`, `dispatcher` and `middlewares` field. Any subsequent routes will use these values:
701701702702```zig
703703-var server = try httpz.Server(Handler).init(allocator, .{.address = .localhost(5882)}, &handler);
703703+var server = try httpz.Server(Handler).init(io, allocator, .{.address = .localhost(5882)}, &handler);
704704705705var router = try server.router(.{});
706706···803803A middleware instance is created using `server.middleware()` and can then be used with the router:
804804805805```zig
806806-var server = try httpz.Server(void).init(allocator, .{.address = .localhost(5882)}, {});
806806+var server = try httpz.Server(void).init(io, allocator, .{.address = .localhost(5882)}, {});
807807808808// the middleware method takes the struct name and its configuration
809809const cors = try server.middleware(httpz.middleware.Cors, .{