1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

endpoints example progress

This commit is contained in:
Rene Schallner 2023-01-14 16:40:54 +01:00
parent 3d1b9adf00
commit cd18bffc62
2 changed files with 27 additions and 17 deletions

View file

@ -49,17 +49,15 @@ pub fn getUserListEndpoint() *zap.SimpleEndpoint {
fn userIdFromPath(path: []const u8) ?usize { fn userIdFromPath(path: []const u8) ?usize {
if (path.len >= endpoint.settings.path.len + 2) { if (path.len >= endpoint.settings.path.len + 2) {
if (path[endpoint.settings.path.len] != '/') { if (path[endpoint.settings.path.len] != '/') {
std.debug.print("no slash\n", .{});
return null; return null;
} }
const idstr = path[endpoint.settings.path.len + 1 ..]; const idstr = path[endpoint.settings.path.len + 1 ..];
std.debug.print("idstr={s}\n", .{idstr});
return std.fmt.parseUnsigned(usize, idstr, 10) catch null; return std.fmt.parseUnsigned(usize, idstr, 10) catch null;
} }
return null; return null;
} }
var jsonbuf: [1024]u8 = undefined; var jsonbuf: [100 * 1024]u8 = undefined;
fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 { fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 {
var fba = std.heap.FixedBufferAllocator.init(&jsonbuf); var fba = std.heap.FixedBufferAllocator.init(&jsonbuf);
var string = std.ArrayList(u8).init(fba.allocator()); var string = std.ArrayList(u8).init(fba.allocator());
@ -72,32 +70,46 @@ fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 {
pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
_ = e; _ = e;
std.debug.print("getUser()\n", .{});
if (r.path) |path| { if (r.path) |path| {
std.debug.print("getUser({s})\n", .{path});
if (userIdFromPath(path)) |id| { if (userIdFromPath(path)) |id| {
std.debug.print("getUser({})\n", .{id});
if (users.get(id)) |user| { if (users.get(id)) |user| {
std.debug.print("getUser(): {}\n", .{user});
if (stringify(user, .{})) |json| { if (stringify(user, .{})) |json| {
_ = r.sendJson(json); _ = r.sendJson(json);
} }
} }
} else {
std.debug.print("User not found\n", .{});
} }
} }
} }
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 {
std.debug.print("listUsers()\n", .{});
_ = r;
_ = e; _ = e;
var l = 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 (stringify(l, .{})) |json| { if (stringifyUserList(&l, .{})) |maybe_json| {
// _ = r.sendJson(json); if (maybe_json) |json| {
// } _ = r.sendJson(json);
}
} else |_| {
return;
}
} }

View file

@ -23,9 +23,7 @@ pub fn main() !void {
// fake some users // fake some users
var uid: usize = undefined; var uid: usize = undefined;
uid = try Endpoints.getUsers().addByName("renerocksai", null); uid = try Endpoints.getUsers().addByName("renerocksai", null);
std.debug.print("Added user {}\n", .{uid});
uid = try Endpoints.getUsers().addByName("renerocksai", "your mom"); uid = try Endpoints.getUsers().addByName("renerocksai", "your mom");
std.debug.print("Added user {}\n", .{uid});
// listen // listen
try listener.listen(); try listener.listen();