From d0cb7520ac7638eac94ab0446195846ac141cc99 Mon Sep 17 00:00:00 2001 From: renerocksai Date: Sun, 16 Mar 2025 13:39:23 +0100 Subject: [PATCH] fix endpoint_auth example for new Endpoint stuff --- examples/endpoint_auth/endpoint_auth.zig | 42 ++++++++++++++---------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/endpoint_auth/endpoint_auth.zig b/examples/endpoint_auth/endpoint_auth.zig index 4b69d7e..f6f1e8a 100644 --- a/examples/endpoint_auth/endpoint_auth.zig +++ b/examples/endpoint_auth/endpoint_auth.zig @@ -10,18 +10,28 @@ const HTTP_RESPONSE: []const u8 = \\ ; -// authenticated requests go here -fn endpoint_http_get(e: *zap.Endpoint, r: zap.Request) void { - _ = e; - r.sendBody(HTTP_RESPONSE) catch return; -} +const Endpoint = struct { + // the slug + path: []const u8, -// just for fun, we also catch the unauthorized callback -fn endpoint_http_unauthorized(e: *zap.Endpoint, r: zap.Request) void { - _ = e; - r.setStatus(.unauthorized); - r.sendBody("UNAUTHORIZED ACCESS") catch return; -} + // authenticated requests go here + pub fn get(_: *Endpoint, r: zap.Request) void { + r.sendBody(HTTP_RESPONSE) catch return; + } + + // just for fun, we also catch the unauthorized callback + pub fn unauthorized(_: *Endpoint, r: zap.Request) void { + r.setStatus(.unauthorized); + r.sendBody("UNAUTHORIZED ACCESS") catch return; + } + + // not implemented, don't care + pub fn post(_: *Endpoint, _: zap.Request) void {} + pub fn put(_: *Endpoint, _: zap.Request) void {} + pub fn delete(_: *Endpoint, _: zap.Request) void {} + pub fn patch(_: *Endpoint, _: zap.Request) void {} + pub fn options(_: *Endpoint, _: zap.Request) void {} +}; pub fn main() !void { // setup listener @@ -38,11 +48,9 @@ pub fn main() !void { defer listener.deinit(); // create mini endpoint - var ep = zap.Endpoint.init(.{ + var ep: Endpoint = .{ .path = "/test", - .get = endpoint_http_get, - .unauthorized = endpoint_http_unauthorized, - }); + }; // create authenticator const Authenticator = zap.Auth.BearerSingle; @@ -50,10 +58,10 @@ pub fn main() !void { defer authenticator.deinit(); // create authenticating endpoint - const BearerAuthEndpoint = zap.Endpoint.Authenticating(Authenticator); + const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator); var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator); - try listener.register(auth_ep.endpoint()); + try listener.register(&auth_ep); listener.listen() catch {}; std.debug.print(