diff --git a/examples/endpoints/endpoints.zig b/examples/endpoints/endpoints.zig index 2885389..cc364e3 100644 --- a/examples/endpoints/endpoints.zig +++ b/examples/endpoints/endpoints.zig @@ -1,6 +1,7 @@ const std = @import("std"); const zap = @import("zap"); const Users = @import("users.zig"); +const User = Users.User; // the Endpoints @@ -21,7 +22,7 @@ pub fn init( endpoint = zap.SimpleEndpoint.init(.{ .path = user_path, .get = getUser, - .post = null, + .post = postUser, .put = null, .delete = null, }); @@ -72,11 +73,11 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; - var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc); + var l: std.ArrayList(User) = std.ArrayList(User).init(alloc); if (users.list(&l)) {} else |_| { return; } - if (zap.stringifyArrayList(Users.User, &l, .{})) |maybe_json| { + if (zap.stringifyArrayList(User, &l, .{})) |maybe_json| { if (maybe_json) |json| { _ = r.sendJson(json); } @@ -87,12 +88,21 @@ fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; - if (r.path) |path| { - if (userIdFromPath(path)) |id| { - if (users.get(id)) |user| { - if (zap.stringify(user, .{})) |json| { + if (r.body) |body| { + var stream = std.json.TokenStream.init(body); + var maybe_user: ?User = std.json.parse( + User, + &stream, + .{ .allocator = alloc }, + ) catch null; + if (maybe_user) |u| { + defer std.json.parseFree(User, u, .{ .allocator = alloc }); + if (users.addByName(u.first_name, u.last_name)) |id| { + if (zap.stringify(.{ .status = "OK", .id = id }, .{})) |json| { _ = r.sendJson(json); } + } else |_| { + return; } } } diff --git a/examples/endpoints/html/index.html b/examples/endpoints/html/index.html index 270c2c0..2379773 100644 --- a/examples/endpoints/html/index.html +++ b/examples/endpoints/html/index.html @@ -1,7 +1,120 @@ + + + + + NIM Sensor Sync + -

Show example user 1

-

Show example user 2

-

Show ALL users

+
+

Show example user 1

+

Show example user 2

+

Show ALL users

+
+
+
+
+ +
+
+
+ +
+
+ +
+
+
+ + (hide) +
+ +
+
+ diff --git a/src/util.zig b/src/util.zig index ab865b0..81ab270 100644 --- a/src/util.zig +++ b/src/util.zig @@ -3,7 +3,9 @@ const std = @import("std"); // // JSON helpers // -var jsonbuf: [100 * 1024]u8 = undefined; + +// 1MB JSON buffer +var jsonbuf: [1024 * 1024]u8 = undefined; pub fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 { var fba = std.heap.FixedBufferAllocator.init(&jsonbuf); diff --git a/src/zap.zig b/src/zap.zig index 65b122a..d722f6e 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -99,18 +99,18 @@ pub const SimpleRequest = struct { // C.fiobj_free(new_fiobj_str); } - // 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 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;