An HTTP/1.1 server for zig
0

Configure Feed

Select the types of activity you want to include in your feed.

update readme

+15 -15
+15 -15
readme.md
··· 1 1 # An HTTP/1.1 server for Zig. 2 2 3 + ## Zig Version 4 + 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. 5 + 3 6 ```zig 4 7 const std = @import("std"); 5 8 const httpz = @import("httpz"); 6 9 7 - pub fn main() !void { 8 - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 9 - const allocator = gpa.allocator(); 10 + pub fn main(init: std.process.Init) !void { 11 + const allocator = init.gpa; 10 12 11 13 // More advance cases will use a custom "Handler" instead of "void". 12 14 // The last parameter is our handler instance, since we have a "void" 13 15 // handler, we passed a void ({}) value. 14 - var server = try httpz.Server(void).init(allocator, .{ 16 + var server = try httpz.Server(void).init(init.io, allocator, .{ 15 17 // use .all(5882) to bind to all interfaces, i.e. 0.0.0.0 16 18 .address = .localhost(5882), 17 19 }, {}); ··· 115 117 const std = @import("std"); 116 118 const httpz = @import("httpz"); 117 119 118 - pub fn main() !void { 119 - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 120 - const allocator = gpa.allocator(); 120 + pub fn main(init: std.process.Init) !void { 121 + const allocator = init.gpa; 121 122 122 - var db = try pg.Pool.init(allocator, .{ 123 + var db = try pg.Pool.init(init.io, allocator, .{ 123 124 .connect = .{ .port = 5432, .host = "localhost"}, 124 125 .auth = .{.username = "user", .database = "db", .password = "pass"} 125 126 }); ··· 129 130 .db = db, 130 131 }; 131 132 132 - var server = try httpz.Server(*App).init(allocator, .{.address = .localhost(5882)}, &app); 133 + var server = try httpz.Server(*App).init(init.io, allocator, .{.address = .localhost(5882)}, &app); 133 134 var router = try server.router(.{}); 134 135 router.get("/api/user/:id", getUser, .{}); 135 136 try server.listen(); ··· 265 266 const std = @import("std"); 266 267 const httpz = @import("httpz"); 267 268 268 - pub fn main() !void { 269 - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 270 - const allocator = gpa.allocator(); 269 + pub fn main(init: std.process.Init) !void { 270 + const allocator = init.gpa; 271 271 272 - var server = try httpz.Server(void).init(allocator, .{.address = .localhost(5882)}, {}); 272 + var server = try httpz.Server(void).init(init.io, allocator, .{.address = .localhost(5882)}, {}); 273 273 274 274 // overwrite the default notFound handler 275 275 server.notFound(notFound); ··· 700 700 You 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: 701 701 702 702 ```zig 703 - var server = try httpz.Server(Handler).init(allocator, .{.address = .localhost(5882)}, &handler); 703 + var server = try httpz.Server(Handler).init(io, allocator, .{.address = .localhost(5882)}, &handler); 704 704 705 705 var router = try server.router(.{}); 706 706 ··· 803 803 A middleware instance is created using `server.middleware()` and can then be used with the router: 804 804 805 805 ```zig 806 - var server = try httpz.Server(void).init(allocator, .{.address = .localhost(5882)}, {}); 806 + var server = try httpz.Server(void).init(io, allocator, .{.address = .localhost(5882)}, {}); 807 807 808 808 // the middleware method takes the struct name and its configuration 809 809 const cors = try server.middleware(httpz.middleware.Cors, .{