1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

fixed zap.Middleware + example for endpoint handlers to new zap.Endpoint

This commit is contained in:
renerocksai 2025-03-16 14:13:54 +01:00
parent 7aae77b034
commit 7f82a6b350
No known key found for this signature in database
2 changed files with 24 additions and 19 deletions

View file

@ -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
);

View file

@ -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