mirror of
https://github.com/zigzap/zap.git
synced 2025-10-22 08:04:08 +00:00
improv
This commit is contained in:
parent
aa31604b18
commit
102c1ac12b
4 changed files with 25 additions and 1 deletions
|
@ -46,7 +46,7 @@ $ # open http://localhost:3000 in your browser
|
||||||
You build and run the examples via:
|
You build and run the examples via:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ zig build hello [EXAMPLE]
|
$ zig build [EXAMPLE]
|
||||||
$ ./zig-out/bin/[EXAMPLE]
|
$ ./zig-out/bin/[EXAMPLE]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
src: []const u8,
|
src: []const u8,
|
||||||
}{
|
}{
|
||||||
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
.{ .name = "hello", .src = "examples/hello/hello.zig" },
|
||||||
|
.{ .name = "hello2", .src = "examples/hello2/hello2.zig" },
|
||||||
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
.{ .name = "routes", .src = "examples/routes/routes.zig" },
|
||||||
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
.{ .name = "serve", .src = "examples/serve/serve.zig" },
|
||||||
}) |excfg| {
|
}) |excfg| {
|
||||||
|
|
|
@ -6,6 +6,7 @@ fn dispatch_routes(r: zap.SimpleRequest) void {
|
||||||
if (r.path) |the_path| {
|
if (r.path) |the_path| {
|
||||||
if (routes.get(the_path)) |foo| {
|
if (routes.get(the_path)) |foo| {
|
||||||
foo(r);
|
foo(r);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// or default: present menu
|
// or default: present menu
|
||||||
|
|
|
@ -23,9 +23,16 @@ const ListenError = error{
|
||||||
ListenError,
|
ListenError,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const HttpParam = struct {
|
||||||
|
key: []const u8,
|
||||||
|
value: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub const SimpleRequest = struct {
|
pub const SimpleRequest = struct {
|
||||||
path: ?[]const u8,
|
path: ?[]const u8,
|
||||||
query: ?[]const u8,
|
query: ?[]const u8,
|
||||||
|
body: ?[]const u8,
|
||||||
|
method: ?[]const u8,
|
||||||
h: [*c]C.http_s,
|
h: [*c]C.http_s,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
@ -36,6 +43,19 @@ pub const SimpleRequest = struct {
|
||||||
@ptrToInt(body.ptr),
|
@ptrToInt(body.ptr),
|
||||||
), body.len);
|
), 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;
|
pub const HttpRequestFn = *const fn (r: [*c]C.http_s) callconv(.C) void;
|
||||||
|
@ -69,6 +89,8 @@ pub const SimpleHttpListener = struct {
|
||||||
var req: SimpleRequest = .{
|
var req: SimpleRequest = .{
|
||||||
.path = fio2str(r.*.path),
|
.path = fio2str(r.*.path),
|
||||||
.query = fio2str(r.*.query),
|
.query = fio2str(r.*.query),
|
||||||
|
.body = fio2str(r.*.body),
|
||||||
|
.method = fio2str(r.*.method),
|
||||||
.h = r,
|
.h = r,
|
||||||
};
|
};
|
||||||
l.settings.on_request.?(req);
|
l.settings.on_request.?(req);
|
||||||
|
|
Loading…
Add table
Reference in a new issue