diff --git a/build.zig.zon b/build.zig.zon index 4837601..85033e0 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = "zap", - .version = "0.0.19", + .version = "0.0.20", .dependencies = .{ .@"facil.io" = .{ diff --git a/examples/endpoint/main.zig b/examples/endpoint/main.zig index 586cfea..a86a1f8 100644 --- a/examples/endpoint/main.zig +++ b/examples/endpoint/main.zig @@ -2,6 +2,15 @@ const std = @import("std"); const zap = @import("zap"); const Endpoint = @import("endpoint.zig"); +// this is just to demo that we can catch arbitrary slugs +fn on_request(r: zap.SimpleRequest) void { + if (r.path) |the_path| { + std.debug.print("REQUESTED PATH: {s}\n", .{the_path}); + } + + r.sendBody("

Hello from ZAP!!!

") catch return; +} + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true, @@ -13,7 +22,7 @@ pub fn main() !void { allocator, .{ .port = 3000, - .on_request = null, + .on_request = on_request, .log = true, .public_folder = "examples/endpoint/html", .max_clients = 100000, diff --git a/src/endpoint.zig b/src/endpoint.zig index a53c6c3..f84937a 100644 --- a/src/endpoint.zig +++ b/src/endpoint.zig @@ -178,13 +178,14 @@ pub const SimpleEndpointListener = struct { /// static struct member endpoints var endpoints: std.ArrayList(*SimpleEndpoint) = undefined; + var on_request: ?zap.SimpleHttpRequestFn = null; pub fn init(a: std.mem.Allocator, l: ListenerSettings) Self { endpoints = std.ArrayList(*SimpleEndpoint).init(a); var ls = l; // take copy of listener settings ls.on_request = onRequest; - + on_request = l.on_request; return .{ .listener = Listener.init(ls), .allocator = a, @@ -227,5 +228,8 @@ pub const SimpleEndpointListener = struct { } } } + if (on_request) |foo| { + foo(r); + } } };