From 7f82a6b35045f4da526c70ccc232d103ffe5555e Mon Sep 17 00:00:00 2001 From: renerocksai Date: Sun, 16 Mar 2025 14:13:54 +0100 Subject: [PATCH] fixed zap.Middleware + example for endpoint handlers to new zap.Endpoint --- .../middleware_with_endpoint.zig | 25 ++++++++----------- src/middleware.zig | 18 +++++++++---- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/examples/middleware_with_endpoint/middleware_with_endpoint.zig b/examples/middleware_with_endpoint/middleware_with_endpoint.zig index 3e94450..3bf9551 100644 --- a/examples/middleware_with_endpoint/middleware_with_endpoint.zig +++ b/examples/middleware_with_endpoint/middleware_with_endpoint.zig @@ -136,26 +136,23 @@ const SessionMiddleWare = struct { // `breakOnFinish` parameter. // const HtmlEndpoint = struct { - ep: zap.Endpoint = undefined, const Self = @This(); + path: []const u8 = "(undefined)", + pub fn init() Self { return .{ - .ep = zap.Endpoint.init(.{ - .path = "/doesn't+matter", - .get = get, - }), + .path = "/doesn't+matter", }; } - pub fn endpoint(self: *Self) *zap.Endpoint { - return &self.ep; - } - - pub fn get(ep: *zap.Endpoint, r: zap.Request) void { - const self: *Self = @fieldParentPtr("ep", ep); - _ = self; + pub fn post(_: *HtmlEndpoint, _: zap.Request) void {} + pub fn put(_: *HtmlEndpoint, _: zap.Request) void {} + pub fn delete(_: *HtmlEndpoint, _: zap.Request) void {} + pub fn patch(_: *HtmlEndpoint, _: zap.Request) void {} + pub fn options(_: *HtmlEndpoint, _: zap.Request) void {} + pub fn get(_: *Self, r: zap.Request) void { var buf: [1024]u8 = undefined; var userFound: bool = false; var sessionFound: bool = false; @@ -205,8 +202,8 @@ pub fn main() !void { var htmlEndpoint = HtmlEndpoint.init(); // we wrap the endpoint with a middleware handler - var htmlHandler = zap.Middleware.EndpointHandler(Handler, Context).init( - htmlEndpoint.endpoint(), // the endpoint + var htmlHandler = zap.Middleware.EndpointHandler(Handler, HtmlEndpoint, Context).init( + &htmlEndpoint, // the endpoint null, // no other handler (we are the last in the chain) .{}, // We can set custom EndpointHandlerOptions here ); diff --git a/src/middleware.zig b/src/middleware.zig index c094fee..0373280 100644 --- a/src/middleware.zig +++ b/src/middleware.zig @@ -63,10 +63,10 @@ pub const EndpointHandlerOptions = struct { }; /// A convenience handler for artibrary zap.Endpoint -pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anytype) type { +pub fn EndpointHandler(comptime HandlerType: anytype, comptime EndpointType: anytype, comptime ContextType: anytype) type { return struct { handler: HandlerType, - endpoint: *zap.Endpoint, + endpoint: *EndpointType, options: EndpointHandlerOptions, const Self = @This(); @@ -78,7 +78,7 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt /// /// If the `breakOnFinish` option is `true`, the handler will stop handing requests down the chain /// if the endpoint processed the request. - pub fn init(endpoint: *zap.Endpoint, other: ?*HandlerType, options: EndpointHandlerOptions) Self { + pub fn init(endpoint: *EndpointType, other: ?*HandlerType, options: EndpointHandlerOptions) Self { return .{ .handler = HandlerType.init(onRequest, other), .endpoint = endpoint, @@ -100,9 +100,17 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt const self: *Self = @fieldParentPtr("handler", handler); r.setUserContext(context); if (!self.options.checkPath or - std.mem.startsWith(u8, r.path orelse "", self.endpoint.settings.path)) + std.mem.startsWith(u8, r.path orelse "", self.endpoint.path)) { - self.endpoint.onRequest(r); + switch (r.methodAsEnum()) { + .GET => self.endpoint.*.get(r), + .POST => self.endpoint.*.post(r), + .PUT => self.endpoint.*.put(r), + .DELETE => self.endpoint.*.delete(r), + .PATCH => self.endpoint.*.patch(r), + .OPTIONS => self.endpoint.*.options(r), + else => {}, + } } // if the request was handled by the endpoint, we may break the chain here