An HTTP/1.1 server for zig
0

Configure Feed

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

http.zig / examples / 05_request_takeover.zig
1.5 kB 41 lines
1const std = @import("std"); 2const httpz = @import("httpz"); 3const Allocator = std.mem.Allocator; 4 5const PORT = 8805; 6 7// This example uses the Handler's "handle" function to completely takeover 8// request processing from httpz. 9 10pub fn main(init: std.process.Init) !void { 11 const allocator = init.gpa; 12 13 var handler = Handler{}; 14 var server = try httpz.Server(*Handler).init(init.io, allocator, .{ .address = .localhost(PORT) }, &handler); 15 16 defer server.deinit(); 17 18 // ensures a clean shutdown, finishing off any existing requests 19 // see 09_shutdown.zig for how to to break server.listen with an interrupt 20 defer server.stop(); 21 22 // Routes aren't used in this mode 23 24 std.debug.print("listening http://localhost:{d}/\n", .{PORT}); 25 26 // Starts the server, this is blocking. 27 try server.listen(); 28} 29 30const Handler = struct { 31 pub fn handle(_: *Handler, _: *httpz.Request, res: *httpz.Response) void { 32 res.body = 33 \\ If defined, the "handle" function is called early in httpz' request 34 \\ processing. Routing, middlewares, not found and error handling are all skipped. 35 \\ This is an advanced option and is used by frameworks like JetZig to provide 36 \\ their own flavor and enhancement ontop of httpz. 37 \\ If you define this, the special "dispatch", "notFound" and "uncaughtError" 38 \\ functions have no meaning as far as httpz is concerned. 39 ; 40 } 41};