const std = @import("std"); const zap = @import("zap"); var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, }){}; fn on_request_verbose(r: zap.Request) void { const allocator = gpa.allocator(); const content_type: zap.ContentType = content_type: { var accept_list = std.ArrayList(zap.Request.AcceptItem).init(allocator); defer accept_list.deinit(); r.parseAccept(&accept_list) catch break :content_type .HTML; for (accept_list.items) |accept| { break :content_type accept.toContentType() orelse continue; } break :content_type .HTML; }; r.setContentType(content_type) catch return; switch (content_type) { .TEXT => { r.sendBody("Hello from ZAP!!!") catch return; }, .HTML => { r.sendBody("

Hello from ZAP!!!

") catch return; }, .XML => { r.sendBody( \\ \\ \\ \\ Hello from ZAP!!! \\ \\ ) catch return; }, .JSON => { var buffer: [128]u8 = undefined; const json = zap.stringifyBuf(&buffer, .{ .message = "Hello from ZAP!!!" }, .{}) orelse return; r.sendJson(json) catch return; }, .XHTML => { r.sendBody( \\ \\ \\ \\

Hello from ZAP!!!

\\ \\ ) catch return; }, } } pub fn main() !void { var listener = zap.HttpListener.init(.{ .port = 3000, .on_request = on_request_verbose, .log = true, .max_clients = 100000, }); try listener.listen(); std.debug.print("Listening on 0.0.0.0:3000\n", .{}); // start worker threads zap.start(.{ .threads = 2, .workers = 2, }); }