mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
fix endpoint_auth example for new Endpoint stuff
This commit is contained in:
parent
0123e60059
commit
d0cb7520ac
1 changed files with 25 additions and 17 deletions
|
@ -10,18 +10,28 @@ const HTTP_RESPONSE: []const u8 =
|
|||
\\ </body></html>
|
||||
;
|
||||
|
||||
// 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(
|
||||
|
|
Loading…
Add table
Reference in a new issue