From 102c1ac12bb9995a9949e790edabfea44cc4a9c7 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 13 Jan 2023 04:38:58 +0100 Subject: [PATCH] improv --- README.md | 2 +- build.zig | 1 + examples/routes/routes.zig | 1 + src/deps/zap.zig | 22 ++++++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 12db370..ac9652b 100644 --- a/README.md +++ b/README.md @@ -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] ``` diff --git a/build.zig b/build.zig index a486a18..53132c7 100644 --- a/build.zig +++ b/build.zig @@ -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| { diff --git a/examples/routes/routes.zig b/examples/routes/routes.zig index dc62312..cd90680 100644 --- a/examples/routes/routes.zig +++ b/examples/routes/routes.zig @@ -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 diff --git a/src/deps/zap.zig b/src/deps/zap.zig index 8da4199..2535f92 100644 --- a/src/deps/zap.zig +++ b/src/deps/zap.zig @@ -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);