//! //! Part of the Zap examples. //! //! Build me with `zig build hello2`. //! Run me with `zig build run-hello2`. //! const std = @import("std"); const zap = @import("zap"); fn on_request(r: zap.Request) !void { const m = r.methodAsEnum(); const m_str = r.method orelse ""; const p = r.path orelse "/"; const qm = if (r.query) |_| "?" else ""; const qq = r.query orelse ""; // curl -H "special-header: hello" http://localhost:3000 if (r.getHeader("special-header")) |clstr| { std.debug.print(">> Special Header: {s}\n", .{clstr}); } else { std.debug.print(">> Special Header: \n", .{}); } std.debug.print(">> {s}({}) {s}{s}{s}\n", .{ m_str, m, p, qm, qq }); if (r.body) |the_body| { std.debug.print(">> BODY: {s}\n", .{the_body}); } try r.setContentTypeFromPath(); try r.sendBody( \\ \\

Hello from ZAP!!!

\\
\\
\\
\\
\\ \\ \\
\\ ); } pub fn main() !void { var listener = zap.HttpListener.init(.{ .port = 3000, .on_request = on_request, .log = false, }); try listener.listen(); std.debug.print( \\ Listening on 0.0.0.0:3000 \\ \\ Test me with: \\ curl http://localhost:3000 \\ curl --header "special-header: test" localhost:3000 \\ \\ ... or open http://localhost:3000 in the browser \\ and watch the log output here \\ \\ , .{}); // start worker threads zap.start(.{ .threads = 2, .workers = 1, }); }