1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-21 07:34:08 +00:00

update docs and examples for endpoint middleware

This commit is contained in:
Thom Dickson 2024-07-25 20:38:26 -04:00
parent c5325337e1
commit 903716bcf5
No known key found for this signature in database
2 changed files with 18 additions and 15 deletions

View file

@ -123,19 +123,17 @@ const SessionMiddleWare = struct {
// //
// !!!! ENDPOINT !!! // !!!! ENDPOINT !!!
// //
// We define an endpoint as we usually would. // We define an endpoint as we usually would; however,
// NO ROUTING IS PERFORMED // NO ROUTING IS PERFORMED BY DEFAULT!
// as we are just going to wrap it in a bunch of Middleware components
// and therefore NOT using an endpoint listener that would the routing for us
// //
// Hence, the endpoint should check r.path in its on_request to check wether // This can be changed using the EndpointHandlerOptions passed into the
// it is adressed or not. // EndpointHandler.init() method.
// //
// N.B. the EndpointHandler checks if the endpoint turned the request into // N.B. the EndpointHandler checks if the endpoint turned the request into
// "finished" state, e.g. by sending anything. If the endpoint didn't finish the // "finished" state, e.g. by sending anything. If the endpoint didn't finish the
// request, the EndpointHandler will pass the request on to the next handler in // request, the EndpointHandler will pass the request on to the next handler in
// the chain if there is one. See also the EndpointHandler's `breakOnFinish` // the chain if there is one. See also the EndpointHandlerOptions's
// parameter. // `breakOnFinish` parameter.
// //
const HtmlEndpoint = struct { const HtmlEndpoint = struct {
ep: zap.Endpoint = undefined, ep: zap.Endpoint = undefined,
@ -210,7 +208,7 @@ pub fn main() !void {
var htmlHandler = zap.Middleware.EndpointHandler(Handler, Context).init( var htmlHandler = zap.Middleware.EndpointHandler(Handler, Context).init(
htmlEndpoint.endpoint(), // the endpoint htmlEndpoint.endpoint(), // the endpoint
null, // no other handler (we are the last in the chain) null, // no other handler (we are the last in the chain)
true, // break on finish. See EndpointHandler for this. Not applicable here. .{}, // We can set custom EndpointHandlerOptions here
); );
// we wrap it in the session Middleware component // we wrap it in the session Middleware component

View file

@ -51,14 +51,15 @@ pub fn Handler(comptime ContextType: anytype) type {
}; };
} }
/// Options used to change the behavior of an `EndpointHandler`
pub const EndpointHandlerOptions = struct { pub const EndpointHandlerOptions = struct {
/// If `true`, the handler will stop handing requests down the chain if the /// If `true`, the handler will stop handing requests down the chain if the
/// endpoint processed the request. /// endpoint processed the request.
breakOnFinish: bool = true, breakOnFinish: bool = true,
/// If `true`, the handler will only execute against request that match the /// If `true`, the handler will only execute against requests that match
/// endpoint's path setting. /// the endpoint's `path` setting.
useRoutes: bool = false, checkPath: bool = false,
}; };
/// A convenience handler for artibrary zap.Endpoint /// A convenience handler for artibrary zap.Endpoint
@ -71,8 +72,12 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt
const Self = @This(); const Self = @This();
/// Create an endpointhandler from an endpoint and pass in the next (other) handler in the chain. /// Create an endpointhandler from an endpoint and pass in the next (other) handler in the chain.
/// If `breakOnFinish` is `true`, the handler will stop handing requests down the chain if ///
/// the endpoint processed the request. /// By default no routing is performed on requests. This behavior can be changed by setting
/// `checkPath` in the provided options.
///
/// 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: *zap.Endpoint, other: ?*HandlerType, options: EndpointHandlerOptions) Self {
return .{ return .{
.handler = HandlerType.init(onRequest, other), .handler = HandlerType.init(onRequest, other),
@ -94,7 +99,7 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt
pub fn onRequest(handler: *HandlerType, r: zap.Request, context: *ContextType) bool { pub fn onRequest(handler: *HandlerType, r: zap.Request, context: *ContextType) bool {
const self: *Self = @fieldParentPtr("handler", handler); const self: *Self = @fieldParentPtr("handler", handler);
r.setUserContext(context); r.setUserContext(context);
if (!self.options.useRoutes or 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.settings.path))
{ {
self.endpoint.onRequest(r); self.endpoint.onRequest(r);