mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
endpoints progress
This commit is contained in:
parent
b0ca939082
commit
4d70181701
3 changed files with 39 additions and 32 deletions
|
@ -326,7 +326,7 @@ pub fn main() !void {
|
||||||
|
|
||||||
### [endpoints](examples/endpoints/)
|
### [endpoints](examples/endpoints/)
|
||||||
|
|
||||||
Only showing `main.zig` here:
|
[`main.zig`](examples/endpoints/main.zig):
|
||||||
|
|
||||||
```zig
|
```zig
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
@ -369,3 +369,6 @@ pub fn main() !void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
[`endpoints.zig`](examples/endpoints/endpoints.zig):
|
||||||
|
|
|
@ -57,23 +57,12 @@ fn userIdFromPath(path: []const u8) ?usize {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonbuf: [100 * 1024]u8 = undefined;
|
|
||||||
fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 {
|
|
||||||
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
|
|
||||||
var string = std.ArrayList(u8).init(fba.allocator());
|
|
||||||
if (std.json.stringify(value, options, string.writer())) {
|
|
||||||
return string.items;
|
|
||||||
} else |_| { // error
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
_ = e;
|
||||||
if (r.path) |path| {
|
if (r.path) |path| {
|
||||||
if (userIdFromPath(path)) |id| {
|
if (userIdFromPath(path)) |id| {
|
||||||
if (users.get(id)) |user| {
|
if (users.get(id)) |user| {
|
||||||
if (stringify(user, .{})) |json| {
|
if (Users.stringify(user, .{})) |json| {
|
||||||
_ = r.sendJson(json);
|
_ = r.sendJson(json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,31 +70,13 @@ pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stringifyUserList(
|
|
||||||
userlist: *std.ArrayList(Users.User),
|
|
||||||
options: std.json.StringifyOptions,
|
|
||||||
) !?[]const u8 {
|
|
||||||
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
|
|
||||||
var string = std.ArrayList(u8).init(fba.allocator());
|
|
||||||
var writer = string.writer();
|
|
||||||
try writer.writeByte('[');
|
|
||||||
var first: bool = true;
|
|
||||||
for (userlist.items) |user| {
|
|
||||||
if (!first) try writer.writeByte(',');
|
|
||||||
first = false;
|
|
||||||
try std.json.stringify(user, options, string.writer());
|
|
||||||
}
|
|
||||||
try writer.writeByte(']');
|
|
||||||
return string.items;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
_ = e;
|
||||||
var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc);
|
var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc);
|
||||||
if (users.list(&l)) {} else |_| {
|
if (users.list(&l)) {} else |_| {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (stringifyUserList(&l, .{})) |maybe_json| {
|
if (Users.stringifyUserList(&l, .{})) |maybe_json| {
|
||||||
if (maybe_json) |json| {
|
if (maybe_json) |json| {
|
||||||
_ = r.sendJson(json);
|
_ = r.sendJson(json);
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,3 +111,36 @@ const JsonUserIterator = struct {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// JSON helpers
|
||||||
|
//
|
||||||
|
var jsonbuf: [100 * 1024]u8 = undefined;
|
||||||
|
|
||||||
|
pub fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 {
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
|
||||||
|
var string = std.ArrayList(u8).init(fba.allocator());
|
||||||
|
if (std.json.stringify(value, options, string.writer())) {
|
||||||
|
return string.items;
|
||||||
|
} else |_| { // error
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stringifyUserList(
|
||||||
|
userlist: *std.ArrayList(User),
|
||||||
|
options: std.json.StringifyOptions,
|
||||||
|
) !?[]const u8 {
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
|
||||||
|
var string = std.ArrayList(u8).init(fba.allocator());
|
||||||
|
var writer = string.writer();
|
||||||
|
try writer.writeByte('[');
|
||||||
|
var first: bool = true;
|
||||||
|
for (userlist.items) |user| {
|
||||||
|
if (!first) try writer.writeByte(',');
|
||||||
|
first = false;
|
||||||
|
try std.json.stringify(user, options, string.writer());
|
||||||
|
}
|
||||||
|
try writer.writeByte(']');
|
||||||
|
return string.items;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue