mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 15:14:08 +00:00
Compare commits
5 commits
905b376c21
...
66a7967aca
Author | SHA1 | Date | |
---|---|---|---|
![]() |
66a7967aca | ||
![]() |
e67b672977 | ||
![]() |
dabd0637f9 | ||
![]() |
baaa71d0e0 | ||
![]() |
6bdd5a41e4 |
16 changed files with 91 additions and 125 deletions
|
@ -25,7 +25,7 @@ proved to be:
|
||||||
## FAQ:
|
## FAQ:
|
||||||
|
|
||||||
- Q: **What version of Zig does Zap support?**
|
- Q: **What version of Zig does Zap support?**
|
||||||
- Zap uses the latest stable zig release (0.14.0), so you don't have to keep
|
- Zap uses the latest stable zig release (0.14.1), so you don't have to keep
|
||||||
up with frequent breaking changes. It's an "LTS feature".
|
up with frequent breaking changes. It's an "LTS feature".
|
||||||
- Q: **Can Zap build with Zig's master branch?**
|
- Q: **Can Zap build with Zig's master branch?**
|
||||||
- See the `zig-master` branch. Please note that the zig-master branch is not
|
- See the `zig-master` branch. Please note that the zig-master branch is not
|
||||||
|
@ -266,7 +266,7 @@ code leaks memory.
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
Make sure you have **zig 0.14.0** installed. Fetch it from
|
Make sure you have **zig 0.14.1** installed. Fetch it from
|
||||||
[here](https://ziglang.org/download).
|
[here](https://ziglang.org/download).
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
@ -279,7 +279,7 @@ $ # open http://localhost:3000 in your browser
|
||||||
|
|
||||||
## Using ⚡zap⚡ in your own projects
|
## Using ⚡zap⚡ in your own projects
|
||||||
|
|
||||||
Make sure you have **the latest zig release (0.14.0)** installed. Fetch it from
|
Make sure you have **the latest zig release (0.14.1)** installed. Fetch it from
|
||||||
[here](https://ziglang.org/download).
|
[here](https://ziglang.org/download).
|
||||||
|
|
||||||
If you don't have an existing zig project, create one like this:
|
If you don't have an existing zig project, create one like this:
|
||||||
|
@ -298,7 +298,7 @@ In your zig project folder (where `build.zig` is located), run:
|
||||||
|
|
||||||
<!-- INSERT_DEP_BEGIN -->
|
<!-- INSERT_DEP_BEGIN -->
|
||||||
```
|
```
|
||||||
zig fetch --save "git+https://github.com/zigzap/zap#v0.10.4"
|
zig fetch --save "git+https://github.com/zigzap/zap#v0.10.5"
|
||||||
```
|
```
|
||||||
<!-- INSERT_DEP_END -->
|
<!-- INSERT_DEP_END -->
|
||||||
|
|
||||||
|
@ -412,3 +412,4 @@ pub fn main() !void {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
.{
|
.{
|
||||||
.name = .zap,
|
.name = .zap,
|
||||||
.version = "0.10.4",
|
.version = "0.10.5",
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"build.zig",
|
"build.zig",
|
||||||
"build.zig.zon",
|
"build.zig.zon",
|
||||||
|
|
|
@ -59,7 +59,7 @@ const SimpleEndpoint = struct {
|
||||||
try r.sendBody(response_text);
|
try r.sendBody(response_text);
|
||||||
std.time.sleep(std.time.ns_per_ms * 300);
|
std.time.sleep(std.time.ns_per_ms * 300);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const StopEndpoint = struct {
|
const StopEndpoint = struct {
|
||||||
path: []const u8,
|
path: []const u8,
|
||||||
|
@ -69,7 +69,7 @@ const StopEndpoint = struct {
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\Before I stop, let me dump the app context:
|
\\Before I stop, let me dump the app context:
|
||||||
\\db_connection='{s}'
|
\\db_connection='{s}'
|
||||||
\\
|
\\
|
||||||
\\
|
\\
|
||||||
, .{context.*.db_connection});
|
, .{context.*.db_connection});
|
||||||
zap.stop();
|
zap.stop();
|
||||||
|
|
|
@ -14,7 +14,7 @@ const MyContext = struct {
|
||||||
db_connection: []const u8,
|
db_connection: []const u8,
|
||||||
|
|
||||||
// we don't use this
|
// we don't use this
|
||||||
pub fn unhandledRequest(_: *MyContext, _: Allocator, _: zap.Request) anyerror!void {}
|
// pub fn unhandledRequest(_: *MyContext, _: Allocator, _: zap.Request) anyerror!void {}
|
||||||
|
|
||||||
pub fn unhandledError(_: *MyContext, _: zap.Request, err: anyerror) void {
|
pub fn unhandledError(_: *MyContext, _: zap.Request, err: anyerror) void {
|
||||||
std.debug.print("\n\n\nUNHANDLED ERROR: {} !!! \n\n\n", .{err});
|
std.debug.print("\n\n\nUNHANDLED ERROR: {} !!! \n\n\n", .{err});
|
||||||
|
@ -46,14 +46,6 @@ const ErrorEndpoint = struct {
|
||||||
// -> error will be raised and dispatched to MyContext.unhandledError
|
// -> error will be raised and dispatched to MyContext.unhandledError
|
||||||
return error.@"Oh-No!";
|
return error.@"Oh-No!";
|
||||||
}
|
}
|
||||||
|
|
||||||
// empty stubs for all other request methods
|
|
||||||
pub fn post(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn put(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn delete(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn patch(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn options(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn head(_: *ErrorEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const StopEndpoint = struct {
|
const StopEndpoint = struct {
|
||||||
|
@ -64,17 +56,11 @@ const StopEndpoint = struct {
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\Before I stop, let me dump the app context:
|
\\Before I stop, let me dump the app context:
|
||||||
\\db_connection='{s}'
|
\\db_connection='{s}'
|
||||||
\\
|
\\
|
||||||
\\
|
\\
|
||||||
, .{context.*.db_connection});
|
, .{context.*.db_connection});
|
||||||
zap.stop();
|
zap.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(_: *StopEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn put(_: *StopEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn delete(_: *StopEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn patch(_: *StopEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
pub fn options(_: *StopEndpoint, _: Allocator, _: *MyContext, _: zap.Request) !void {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
|
|
|
@ -13,11 +13,3 @@ pub fn get(_: *ErrorEndpoint, _: zap.Request) !void {
|
||||||
// --> this error will be shown in the browser, with a nice error trace
|
// --> this error will be shown in the browser, with a nice error trace
|
||||||
return error.@"Oh-no!";
|
return error.@"Oh-no!";
|
||||||
}
|
}
|
||||||
|
|
||||||
// unused:
|
|
||||||
pub fn post(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn put(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn delete(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn patch(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn options(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn head(_: *ErrorEndpoint, _: zap.Request) !void {}
|
|
||||||
|
|
|
@ -17,10 +17,3 @@ pub fn init(path: []const u8) StopEndpoint {
|
||||||
pub fn get(_: *StopEndpoint, _: zap.Request) !void {
|
pub fn get(_: *StopEndpoint, _: zap.Request) !void {
|
||||||
zap.stop();
|
zap.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn put(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn delete(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn patch(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn options(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
pub fn head(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
|
|
|
@ -43,7 +43,8 @@ fn userIdFromPath(self: *UserWeb, path: []const u8) ?usize {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put(_: *UserWeb, _: zap.Request) !void {}
|
// not implemented
|
||||||
|
// pub fn put(_: *UserWeb, _: zap.Request) !void {}
|
||||||
|
|
||||||
pub fn get(self: *UserWeb, r: zap.Request) !void {
|
pub fn get(self: *UserWeb, r: zap.Request) !void {
|
||||||
if (r.path) |path| {
|
if (r.path) |path| {
|
||||||
|
|
|
@ -31,14 +31,6 @@ const Endpoint = struct {
|
||||||
r.setStatus(.unauthorized);
|
r.setStatus(.unauthorized);
|
||||||
r.sendBody("UNAUTHORIZED ACCESS") catch return;
|
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 head(_: *Endpoint, _: zap.Request) !void {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
|
@ -74,7 +66,7 @@ pub fn main() !void {
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
std.debug.print(
|
std.debug.print(
|
||||||
\\ Run the following:
|
\\ Run the following:
|
||||||
\\
|
\\
|
||||||
\\ curl http://localhost:3000/test -i -H "Authorization: Bearer ABCDEFG" -v
|
\\ curl http://localhost:3000/test -i -H "Authorization: Bearer ABCDEFG" -v
|
||||||
\\ curl http://localhost:3000/test -i -H "Authorization: Bearer invalid" -v
|
\\ curl http://localhost:3000/test -i -H "Authorization: Bearer invalid" -v
|
||||||
\\
|
\\
|
||||||
|
|
|
@ -152,13 +152,6 @@ const HtmlEndpoint = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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 head(_: *HtmlEndpoint, _: zap.Request) !void {}
|
|
||||||
|
|
||||||
pub fn get(_: *HtmlEndpoint, r: zap.Request) !void {
|
pub fn get(_: *HtmlEndpoint, r: zap.Request) !void {
|
||||||
var buf: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
var userFound: bool = false;
|
var userFound: bool = false;
|
||||||
|
|
|
@ -5,7 +5,11 @@
|
||||||
//! Pass an instance of an Endpoint struct to zap.Endpoint.Listener.register()
|
//! Pass an instance of an Endpoint struct to zap.Endpoint.Listener.register()
|
||||||
//! function to register with the listener.
|
//! function to register with the listener.
|
||||||
//!
|
//!
|
||||||
//! **NOTE**: Endpoints must implement the following "interface":
|
//! **NOTE**: Endpoints can implement the following "interface":
|
||||||
|
//!
|
||||||
|
//! Any method handler that's not implemented will be handled automatically:
|
||||||
|
//! - zap will log it
|
||||||
|
//! - a response with status code 405 (method not allowed) is sent to the client
|
||||||
//!
|
//!
|
||||||
//! ```zig
|
//! ```zig
|
||||||
//! /// The http request path / slug of the endpoint
|
//! /// The http request path / slug of the endpoint
|
||||||
|
@ -13,6 +17,7 @@
|
||||||
//! error_strategy: zap.Endpoint.ErrorStrategy,
|
//! error_strategy: zap.Endpoint.ErrorStrategy,
|
||||||
//!
|
//!
|
||||||
//! /// Handlers by request method:
|
//! /// Handlers by request method:
|
||||||
|
//! /// implement any of the following
|
||||||
//! pub fn get(_: *Self, _: zap.Request) !void {}
|
//! pub fn get(_: *Self, _: zap.Request) !void {}
|
||||||
//! pub fn post(_: *Self, _: zap.Request) !void {}
|
//! pub fn post(_: *Self, _: zap.Request) !void {}
|
||||||
//! pub fn put(_: *Self, _: zap.Request) !void {}
|
//! pub fn put(_: *Self, _: zap.Request) !void {}
|
||||||
|
@ -44,13 +49,6 @@
|
||||||
//! pub fn get(_: *StopEndpoint, _: zap.Request) !void {
|
//! pub fn get(_: *StopEndpoint, _: zap.Request) !void {
|
||||||
//! zap.stop();
|
//! zap.stop();
|
||||||
//! }
|
//! }
|
||||||
//!
|
|
||||||
//! pub fn post(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
//! pub fn put(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
//! pub fn delete(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
//! pub fn patch(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
//! pub fn options(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
//! pub fn head(_: *StopEndpoint, _: zap.Request) !void {}
|
|
||||||
//! };
|
//! };
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
@ -154,10 +152,25 @@ pub fn checkEndpointType(T: type) void {
|
||||||
@compileError("Expected return type of method `" ++ @typeName(T) ++ "." ++ method ++ "` to be !void, got: !" ++ @typeName(ret_info.error_union.payload));
|
@compileError("Expected return type of method `" ++ @typeName(T) ++ "." ++ method ++ "` to be !void, got: !" ++ @typeName(ret_info.error_union.payload));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@compileError(@typeName(T) ++ " has no method named `" ++ method ++ "`");
|
// it is ok not to implement a method handler
|
||||||
|
// pass
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// This can be resolved at comptime so *perhaps it does affect optimiazation
|
||||||
|
pub fn callHandlerIfExist(comptime fn_name: []const u8, e: anytype, r: Request) anyerror!void {
|
||||||
|
const EndPointType = @TypeOf(e.*);
|
||||||
|
if (@hasDecl(EndPointType, fn_name)) {
|
||||||
|
return @field(EndPointType, fn_name)(e, r);
|
||||||
|
}
|
||||||
|
zap.log.debug(
|
||||||
|
"Unhandled `{s}` {s} request ({s} not implemented in {s})",
|
||||||
|
.{ r.method orelse "<unknown>", r.path orelse "", fn_name, @typeName(EndPointType) },
|
||||||
|
);
|
||||||
|
r.setStatus(.method_not_allowed);
|
||||||
|
try r.sendBody("405 - method not allowed\r\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
pub const Binder = struct {
|
pub const Binder = struct {
|
||||||
pub const Interface = struct {
|
pub const Interface = struct {
|
||||||
|
@ -189,13 +202,13 @@ pub const Binder = struct {
|
||||||
|
|
||||||
pub fn onRequest(self: *Bound, r: zap.Request) !void {
|
pub fn onRequest(self: *Bound, r: zap.Request) !void {
|
||||||
const ret = switch (r.methodAsEnum()) {
|
const ret = switch (r.methodAsEnum()) {
|
||||||
.GET => self.endpoint.*.get(r),
|
.GET => callHandlerIfExist("get", self.endpoint, r),
|
||||||
.POST => self.endpoint.*.post(r),
|
.POST => callHandlerIfExist("post", self.endpoint, r),
|
||||||
.PUT => self.endpoint.*.put(r),
|
.PUT => callHandlerIfExist("put", self.endpoint, r),
|
||||||
.DELETE => self.endpoint.*.delete(r),
|
.DELETE => callHandlerIfExist("delete", self.endpoint, r),
|
||||||
.PATCH => self.endpoint.*.patch(r),
|
.PATCH => callHandlerIfExist("patch", self.endpoint, r),
|
||||||
.OPTIONS => self.endpoint.*.options(r),
|
.OPTIONS => callHandlerIfExist("options", self.endpoint, r),
|
||||||
.HEAD => self.endpoint.*.head(r),
|
.HEAD => callHandlerIfExist("head", self.endpoint, r),
|
||||||
else => error.UnsupportedHtmlRequestMethod,
|
else => error.UnsupportedHtmlRequestMethod,
|
||||||
};
|
};
|
||||||
if (ret) {
|
if (ret) {
|
||||||
|
@ -249,8 +262,8 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type {
|
||||||
/// Authenticates GET requests using the Authenticator.
|
/// Authenticates GET requests using the Authenticator.
|
||||||
pub fn get(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn get(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.get(r),
|
.AuthOK => callHandlerIfExist("get", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -258,8 +271,8 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type {
|
||||||
/// Authenticates POST requests using the Authenticator.
|
/// Authenticates POST requests using the Authenticator.
|
||||||
pub fn post(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn post(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.post(r),
|
.AuthOK => callHandlerIfExist("post", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -267,8 +280,8 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type {
|
||||||
/// Authenticates PUT requests using the Authenticator.
|
/// Authenticates PUT requests using the Authenticator.
|
||||||
pub fn put(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn put(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.put(r),
|
.AuthOK => callHandlerIfExist("put", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -276,8 +289,8 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type {
|
||||||
/// Authenticates DELETE requests using the Authenticator.
|
/// Authenticates DELETE requests using the Authenticator.
|
||||||
pub fn delete(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn delete(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.delete(r),
|
.AuthOK => callHandlerIfExist("delete", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -285,8 +298,8 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type {
|
||||||
/// Authenticates PATCH requests using the Authenticator.
|
/// Authenticates PATCH requests using the Authenticator.
|
||||||
pub fn patch(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn patch(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.patch(r),
|
.AuthOK => callHandlerIfExist("patch", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -294,17 +307,17 @@ pub fn Authenticating(EndpointType: type, Authenticator: type) type {
|
||||||
/// Authenticates OPTIONS requests using the Authenticator.
|
/// Authenticates OPTIONS requests using the Authenticator.
|
||||||
pub fn options(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn options(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.put(r),
|
.AuthOK => callHandlerIfExist("options", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Authenticates HEAD requests using the Authenticator.
|
/// Authenticates HEAD requests using the Authenticator.
|
||||||
pub fn head(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
pub fn head(self: *AuthenticatingEndpoint, r: zap.Request) anyerror!void {
|
||||||
try switch (self.authenticator.authenticateRequest(&r)) {
|
try switch (self.authenticator.authenticateRequest(&r)) {
|
||||||
.AuthFailed => return self.ep.*.unauthorized(r),
|
.AuthFailed => callHandlerIfExist("unauthorized", self.ep, r),
|
||||||
.AuthOK => self.ep.*.head(r),
|
.AuthOK => callHandlerIfExist("head", self.ep, r),
|
||||||
.Handled => {},
|
.Handled => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const zap = @import("zap.zig");
|
const zap = @import("zap.zig");
|
||||||
|
const callHandlerIfExist = @import("endpoint.zig").callHandlerIfExist;
|
||||||
|
|
||||||
/// Your middleware components need to contain a handler.
|
/// Your middleware components need to contain a handler.
|
||||||
///
|
///
|
||||||
|
@ -102,16 +103,16 @@ pub fn EndpointHandler(comptime HandlerType: anytype, comptime EndpointType: any
|
||||||
if (!self.options.checkPath or
|
if (!self.options.checkPath or
|
||||||
std.mem.startsWith(u8, r.path orelse "", self.endpoint.path))
|
std.mem.startsWith(u8, r.path orelse "", self.endpoint.path))
|
||||||
{
|
{
|
||||||
switch (r.methodAsEnum()) {
|
try switch (r.methodAsEnum()) {
|
||||||
.GET => try self.endpoint.*.get(r),
|
.GET => callHandlerIfExist("get", self.endpoint, r),
|
||||||
.POST => try self.endpoint.*.post(r),
|
.POST => callHandlerIfExist("post", self.endpoint, r),
|
||||||
.PUT => try self.endpoint.*.put(r),
|
.PUT => callHandlerIfExist("put", self.endpoint, r),
|
||||||
.DELETE => try self.endpoint.*.delete(r),
|
.DELETE => callHandlerIfExist("delete", self.endpoint, r),
|
||||||
.PATCH => try self.endpoint.*.patch(r),
|
.PATCH => callHandlerIfExist("patch", self.endpoint, r),
|
||||||
.OPTIONS => try self.endpoint.*.options(r),
|
.OPTIONS => callHandlerIfExist("options", self.endpoint, r),
|
||||||
.HEAD => try self.endpoint.*.head(r),
|
.HEAD => callHandlerIfExist("head", self.endpoint, r),
|
||||||
else => {},
|
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
|
||||||
|
|
|
@ -165,12 +165,6 @@ pub const Endpoint = struct {
|
||||||
std.time.sleep(1 * std.time.ns_per_s);
|
std.time.sleep(1 * std.time.ns_per_s);
|
||||||
zap.stop();
|
zap.stop();
|
||||||
}
|
}
|
||||||
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 head(_: *Endpoint, _: zap.Request) !void {}
|
|
||||||
};
|
};
|
||||||
//
|
//
|
||||||
// end of http client code
|
// end of http client code
|
||||||
|
@ -184,7 +178,7 @@ test "BearerAuthSingle authenticateRequest OK" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3001,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -211,7 +205,7 @@ test "BearerAuthSingle authenticateRequest OK" {
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = token });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3001/test", .{ .auth = .Bearer, .token = token });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -231,7 +225,7 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3002,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -264,7 +258,7 @@ test "BearerAuthSingle authenticateRequest test-unauthorized" {
|
||||||
|
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = "invalid" });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3002/test", .{ .auth = .Bearer, .token = "invalid" });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -284,7 +278,7 @@ test "BearerAuthMulti authenticateRequest OK" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3003,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -311,7 +305,7 @@ test "BearerAuthMulti authenticateRequest OK" {
|
||||||
|
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = token });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3003/test", .{ .auth = .Bearer, .token = token });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -331,7 +325,7 @@ test "BearerAuthMulti authenticateRequest test-unauthorized" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3004,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -358,7 +352,7 @@ test "BearerAuthMulti authenticateRequest test-unauthorized" {
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Bearer, .token = "invalid" });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3004/test", .{ .auth = .Bearer, .token = "invalid" });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -378,7 +372,7 @@ test "BasicAuth Token68 authenticateRequest" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3005,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -410,7 +404,7 @@ test "BasicAuth Token68 authenticateRequest" {
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = token });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3005/test", .{ .auth = .Basic, .token = token });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -430,7 +424,7 @@ test "BasicAuth Token68 authenticateRequest test-unauthorized" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3006,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -462,7 +456,7 @@ test "BasicAuth Token68 authenticateRequest test-unauthorized" {
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = "invalid" });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3006/test", .{ .auth = .Basic, .token = "invalid" });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -481,7 +475,7 @@ test "BasicAuth UserPass authenticateRequest" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3007,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -524,7 +518,7 @@ test "BasicAuth UserPass authenticateRequest" {
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = encoded });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3007/test", .{ .auth = .Basic, .token = encoded });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
@ -543,7 +537,7 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" {
|
||||||
var listener = zap.Endpoint.Listener.init(
|
var listener = zap.Endpoint.Listener.init(
|
||||||
a,
|
a,
|
||||||
.{
|
.{
|
||||||
.port = 3000,
|
.port = 3008,
|
||||||
.on_request = null,
|
.on_request = null,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -589,7 +583,7 @@ test "BasicAuth UserPass authenticateRequest test-unauthorized" {
|
||||||
|
|
||||||
listener.listen() catch {};
|
listener.listen() catch {};
|
||||||
|
|
||||||
const thread = try makeRequestThread(a, "http://127.0.0.1:3000/test", .{ .auth = .Basic, .token = "invalid" });
|
const thread = try makeRequestThread(a, "http://127.0.0.1:3008/test", .{ .auth = .Basic, .token = "invalid" });
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
|
|
||||||
// start worker threads
|
// start worker threads
|
||||||
|
|
|
@ -65,7 +65,7 @@ test "http parameters" {
|
||||||
// setup listener
|
// setup listener
|
||||||
var listener = zap.HttpListener.init(
|
var listener = zap.HttpListener.init(
|
||||||
.{
|
.{
|
||||||
.port = 3001,
|
.port = 3010,
|
||||||
.on_request = Handler.on_request,
|
.on_request = Handler.on_request,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -74,7 +74,7 @@ test "http parameters" {
|
||||||
);
|
);
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3001/?one=1&two=2&string=hello+world&float=6.28&bool=true");
|
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3010/?one=1&two=2&string=hello+world&float=6.28&bool=true");
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
.threads = 1,
|
.threads = 1,
|
||||||
|
|
|
@ -110,7 +110,7 @@ test "recv file" {
|
||||||
|
|
||||||
var listener = zap.HttpListener.init(
|
var listener = zap.HttpListener.init(
|
||||||
.{
|
.{
|
||||||
.port = 3003,
|
.port = 3020,
|
||||||
.on_request = on_request,
|
.on_request = on_request,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -119,7 +119,7 @@ test "recv file" {
|
||||||
);
|
);
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const t1 = try std.Thread.spawn(.{}, makeRequest, .{ allocator, "http://127.0.0.1:3003" });
|
const t1 = try std.Thread.spawn(.{}, makeRequest, .{ allocator, "http://127.0.0.1:3020" });
|
||||||
defer t1.join();
|
defer t1.join();
|
||||||
|
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
|
|
|
@ -108,7 +108,7 @@ test "recv file" {
|
||||||
|
|
||||||
var listener = zap.HttpListener.init(
|
var listener = zap.HttpListener.init(
|
||||||
.{
|
.{
|
||||||
.port = 3003,
|
.port = 3030,
|
||||||
.on_request = on_request,
|
.on_request = on_request,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -117,7 +117,7 @@ test "recv file" {
|
||||||
);
|
);
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const t1 = try std.Thread.spawn(.{}, makeRequest, .{ allocator, "http://127.0.0.1:3003" });
|
const t1 = try std.Thread.spawn(.{}, makeRequest, .{ allocator, "http://127.0.0.1:3030" });
|
||||||
defer t1.join();
|
defer t1.join();
|
||||||
|
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
|
|
|
@ -42,7 +42,7 @@ test "send file" {
|
||||||
// setup listener
|
// setup listener
|
||||||
var listener = zap.HttpListener.init(
|
var listener = zap.HttpListener.init(
|
||||||
.{
|
.{
|
||||||
.port = 3002,
|
.port = 3040,
|
||||||
.on_request = on_request,
|
.on_request = on_request,
|
||||||
.log = false,
|
.log = false,
|
||||||
.max_clients = 10,
|
.max_clients = 10,
|
||||||
|
@ -51,7 +51,7 @@ test "send file" {
|
||||||
);
|
);
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
||||||
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3002/?file=src/tests/testfile.txt");
|
const thread = try makeRequestThread(allocator, "http://127.0.0.1:3040/?file=src/tests/testfile.txt");
|
||||||
defer thread.join();
|
defer thread.join();
|
||||||
zap.start(.{
|
zap.start(.{
|
||||||
.threads = 1,
|
.threads = 1,
|
||||||
|
|
Loading…
Add table
Reference in a new issue