mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 07:34:08 +00:00
fixed zap.Middleware + example for endpoint handlers to new zap.Endpoint
This commit is contained in:
parent
7aae77b034
commit
7f82a6b350
2 changed files with 24 additions and 19 deletions
|
@ -136,26 +136,23 @@ const SessionMiddleWare = struct {
|
||||||
// `breakOnFinish` parameter.
|
// `breakOnFinish` parameter.
|
||||||
//
|
//
|
||||||
const HtmlEndpoint = struct {
|
const HtmlEndpoint = struct {
|
||||||
ep: zap.Endpoint = undefined,
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
path: []const u8 = "(undefined)",
|
||||||
|
|
||||||
pub fn init() Self {
|
pub fn init() Self {
|
||||||
return .{
|
return .{
|
||||||
.ep = zap.Endpoint.init(.{
|
.path = "/doesn't+matter",
|
||||||
.path = "/doesn't+matter",
|
|
||||||
.get = get,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn endpoint(self: *Self) *zap.Endpoint {
|
pub fn post(_: *HtmlEndpoint, _: zap.Request) void {}
|
||||||
return &self.ep;
|
pub fn put(_: *HtmlEndpoint, _: zap.Request) void {}
|
||||||
}
|
pub fn delete(_: *HtmlEndpoint, _: zap.Request) void {}
|
||||||
|
pub fn patch(_: *HtmlEndpoint, _: zap.Request) void {}
|
||||||
pub fn get(ep: *zap.Endpoint, r: zap.Request) void {
|
pub fn options(_: *HtmlEndpoint, _: zap.Request) void {}
|
||||||
const self: *Self = @fieldParentPtr("ep", ep);
|
|
||||||
_ = self;
|
|
||||||
|
|
||||||
|
pub fn get(_: *Self, r: zap.Request) void {
|
||||||
var buf: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
var userFound: bool = false;
|
var userFound: bool = false;
|
||||||
var sessionFound: bool = false;
|
var sessionFound: bool = false;
|
||||||
|
@ -205,8 +202,8 @@ pub fn main() !void {
|
||||||
var htmlEndpoint = HtmlEndpoint.init();
|
var htmlEndpoint = HtmlEndpoint.init();
|
||||||
|
|
||||||
// we wrap the endpoint with a middleware handler
|
// we wrap the endpoint with a middleware handler
|
||||||
var htmlHandler = zap.Middleware.EndpointHandler(Handler, Context).init(
|
var htmlHandler = zap.Middleware.EndpointHandler(Handler, HtmlEndpoint, Context).init(
|
||||||
htmlEndpoint.endpoint(), // the endpoint
|
&htmlEndpoint, // the endpoint
|
||||||
null, // no other handler (we are the last in the chain)
|
null, // no other handler (we are the last in the chain)
|
||||||
.{}, // We can set custom EndpointHandlerOptions here
|
.{}, // We can set custom EndpointHandlerOptions here
|
||||||
);
|
);
|
||||||
|
|
|
@ -63,10 +63,10 @@ pub const EndpointHandlerOptions = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A convenience handler for artibrary zap.Endpoint
|
/// 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 {
|
return struct {
|
||||||
handler: HandlerType,
|
handler: HandlerType,
|
||||||
endpoint: *zap.Endpoint,
|
endpoint: *EndpointType,
|
||||||
options: EndpointHandlerOptions,
|
options: EndpointHandlerOptions,
|
||||||
|
|
||||||
const Self = @This();
|
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 `breakOnFinish` option is `true`, the handler will stop handing requests down the chain
|
||||||
/// if the endpoint processed the request.
|
/// 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 .{
|
return .{
|
||||||
.handler = HandlerType.init(onRequest, other),
|
.handler = HandlerType.init(onRequest, other),
|
||||||
.endpoint = endpoint,
|
.endpoint = endpoint,
|
||||||
|
@ -100,9 +100,17 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime ContextType: anyt
|
||||||
const self: *Self = @fieldParentPtr("handler", handler);
|
const self: *Self = @fieldParentPtr("handler", handler);
|
||||||
r.setUserContext(context);
|
r.setUserContext(context);
|
||||||
if (!self.options.checkPath 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.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
|
// if the request was handled by the endpoint, we may break the chain here
|
||||||
|
|
Loading…
Add table
Reference in a new issue