mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 07:34: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>
|
\\ </body></html>
|
||||||
;
|
;
|
||||||
|
|
||||||
// authenticated requests go here
|
const Endpoint = struct {
|
||||||
fn endpoint_http_get(e: *zap.Endpoint, r: zap.Request) void {
|
// the slug
|
||||||
_ = e;
|
path: []const u8,
|
||||||
r.sendBody(HTTP_RESPONSE) catch return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// just for fun, we also catch the unauthorized callback
|
// authenticated requests go here
|
||||||
fn endpoint_http_unauthorized(e: *zap.Endpoint, r: zap.Request) void {
|
pub fn get(_: *Endpoint, r: zap.Request) void {
|
||||||
_ = e;
|
r.sendBody(HTTP_RESPONSE) catch return;
|
||||||
r.setStatus(.unauthorized);
|
}
|
||||||
r.sendBody("UNAUTHORIZED ACCESS") 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 {
|
pub fn main() !void {
|
||||||
// setup listener
|
// setup listener
|
||||||
|
@ -38,11 +48,9 @@ pub fn main() !void {
|
||||||
defer listener.deinit();
|
defer listener.deinit();
|
||||||
|
|
||||||
// create mini endpoint
|
// create mini endpoint
|
||||||
var ep = zap.Endpoint.init(.{
|
var ep: Endpoint = .{
|
||||||
.path = "/test",
|
.path = "/test",
|
||||||
.get = endpoint_http_get,
|
};
|
||||||
.unauthorized = endpoint_http_unauthorized,
|
|
||||||
});
|
|
||||||
|
|
||||||
// create authenticator
|
// create authenticator
|
||||||
const Authenticator = zap.Auth.BearerSingle;
|
const Authenticator = zap.Auth.BearerSingle;
|
||||||
|
@ -50,10 +58,10 @@ pub fn main() !void {
|
||||||
defer authenticator.deinit();
|
defer authenticator.deinit();
|
||||||
|
|
||||||
// create authenticating endpoint
|
// create authenticating endpoint
|
||||||
const BearerAuthEndpoint = zap.Endpoint.Authenticating(Authenticator);
|
const BearerAuthEndpoint = zap.Endpoint.Authenticating(Endpoint, Authenticator);
|
||||||
var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator);
|
var auth_ep = BearerAuthEndpoint.init(&ep, &authenticator);
|
||||||
|
|
||||||
try listener.register(auth_ep.endpoint());
|
try listener.register(&auth_ep);
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
|
|
Loading…
Add table
Reference in a new issue