1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00
This commit is contained in:
Rene Schallner 2023-01-13 04:38:58 +01:00
parent aa31604b18
commit 102c1ac12b
4 changed files with 25 additions and 1 deletions

View file

@ -46,7 +46,7 @@ $ # open http://localhost:3000 in your browser
You build and run the examples via:
```shell
$ zig build hello [EXAMPLE]
$ zig build [EXAMPLE]
$ ./zig-out/bin/[EXAMPLE]
```

View file

@ -19,6 +19,7 @@ pub fn build(b: *std.build.Builder) !void {
src: []const u8,
}{
.{ .name = "hello", .src = "examples/hello/hello.zig" },
.{ .name = "hello2", .src = "examples/hello2/hello2.zig" },
.{ .name = "routes", .src = "examples/routes/routes.zig" },
.{ .name = "serve", .src = "examples/serve/serve.zig" },
}) |excfg| {

View file

@ -6,6 +6,7 @@ fn dispatch_routes(r: zap.SimpleRequest) void {
if (r.path) |the_path| {
if (routes.get(the_path)) |foo| {
foo(r);
return;
}
}
// or default: present menu

View file

@ -23,9 +23,16 @@ const ListenError = error{
ListenError,
};
const HttpParam = struct {
key: []const u8,
value: []const u8,
};
pub const SimpleRequest = struct {
path: ?[]const u8,
query: ?[]const u8,
body: ?[]const u8,
method: ?[]const u8,
h: [*c]C.http_s,
const Self = @This();
@ -36,6 +43,19 @@ pub const SimpleRequest = struct {
@ptrToInt(body.ptr),
), body.len);
}
pub fn nextParam(self: *const Self) ?HttpParam {
if (self.h.*.params == 0) return null;
var key: C.FIOBJ = undefined;
const value = C.fiobj_hash_pop(self.h.*.params, &key);
if (value == C.FIOBJ_INVALID) {
return null;
}
return HttpParam{
.key = fio2str(key).?,
.value = fio2str(value).?,
};
}
};
pub const HttpRequestFn = *const fn (r: [*c]C.http_s) callconv(.C) void;
@ -69,6 +89,8 @@ pub const SimpleHttpListener = struct {
var req: SimpleRequest = .{
.path = fio2str(r.*.path),
.query = fio2str(r.*.query),
.body = fio2str(r.*.body),
.method = fio2str(r.*.method),
.h = r,
};
l.settings.on_request.?(req);