mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
Enpoint example: from singleton to @fieldParentPtr
This commit is contained in:
parent
9a9decbdd4
commit
dfaadf3140
2 changed files with 48 additions and 47 deletions
|
@ -3,57 +3,60 @@ const zap = @import("zap");
|
||||||
const Users = @import("users.zig");
|
const Users = @import("users.zig");
|
||||||
const User = Users.User;
|
const User = Users.User;
|
||||||
|
|
||||||
// the Endpoint
|
// an Endpoint
|
||||||
|
|
||||||
pub const Self = @This();
|
pub const Self = @This();
|
||||||
|
|
||||||
var alloc: std.mem.Allocator = undefined;
|
alloc: std.mem.Allocator = undefined,
|
||||||
var endpoint: zap.SimpleEndpoint = undefined;
|
endpoint: zap.SimpleEndpoint = undefined,
|
||||||
var users: Users = undefined;
|
users: Users = undefined,
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
a: std.mem.Allocator,
|
a: std.mem.Allocator,
|
||||||
user_path: []const u8,
|
user_path: []const u8,
|
||||||
) void {
|
) Self {
|
||||||
users = Users.init(a);
|
return .{
|
||||||
alloc = a;
|
.alloc = a,
|
||||||
endpoint = zap.SimpleEndpoint.init(.{
|
.users = Users.init(a),
|
||||||
|
.endpoint = zap.SimpleEndpoint.init(.{
|
||||||
.path = user_path,
|
.path = user_path,
|
||||||
.get = getUser,
|
.get = getUser,
|
||||||
.post = postUser,
|
.post = postUser,
|
||||||
.put = putUser,
|
.put = putUser,
|
||||||
.delete = deleteUser,
|
.delete = deleteUser,
|
||||||
});
|
}),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getUsers() *Users {
|
pub fn getUsers(self: *Self) *Users {
|
||||||
return &users;
|
return &self.users;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getUserEndpoint() *zap.SimpleEndpoint {
|
pub fn getUserEndpoint(self: *Self) *zap.SimpleEndpoint {
|
||||||
return &endpoint;
|
return &self.endpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn userIdFromPath(path: []const u8) ?usize {
|
fn userIdFromPath(self: *Self, path: []const u8) ?usize {
|
||||||
if (path.len >= endpoint.settings.path.len + 2) {
|
if (path.len >= self.endpoint.settings.path.len + 2) {
|
||||||
if (path[endpoint.settings.path.len] != '/') {
|
if (path[self.endpoint.settings.path.len] != '/') {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const idstr = path[endpoint.settings.path.len + 1 ..];
|
const idstr = path[self.endpoint.settings.path.len + 1 ..];
|
||||||
return std.fmt.parseUnsigned(usize, idstr, 10) catch null;
|
return std.fmt.parseUnsigned(usize, idstr, 10) catch null;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
|
const self = @fieldParentPtr(Self, "endpoint", e);
|
||||||
if (r.path) |path| {
|
if (r.path) |path| {
|
||||||
// /users
|
// /users
|
||||||
if (path.len == e.settings.path.len) {
|
if (path.len == e.settings.path.len) {
|
||||||
return listUsers(e, r);
|
return self.listUsers(r);
|
||||||
}
|
}
|
||||||
var jsonbuf: [256]u8 = undefined;
|
var jsonbuf: [256]u8 = undefined;
|
||||||
if (userIdFromPath(path)) |id| {
|
if (self.userIdFromPath(path)) |id| {
|
||||||
if (users.get(id)) |user| {
|
if (self.users.get(id)) |user| {
|
||||||
if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| {
|
if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| {
|
||||||
r.sendJson(json) catch return;
|
r.sendJson(json) catch return;
|
||||||
}
|
}
|
||||||
|
@ -62,11 +65,9 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
fn listUsers(self: *Self, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
if (self.users.toJSON()) |json| {
|
||||||
|
defer self.alloc.free(json);
|
||||||
if (users.toJSON()) |json| {
|
|
||||||
defer alloc.free(json);
|
|
||||||
r.sendJson(json) catch return;
|
r.sendJson(json) catch return;
|
||||||
} else |err| {
|
} else |err| {
|
||||||
std.debug.print("LIST error: {}\n", .{err});
|
std.debug.print("LIST error: {}\n", .{err});
|
||||||
|
@ -74,13 +75,13 @@ fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
const self = @fieldParentPtr(Self, "endpoint", e);
|
||||||
if (r.body) |body| {
|
if (r.body) |body| {
|
||||||
var stream = std.json.TokenStream.init(body);
|
var stream = std.json.TokenStream.init(body);
|
||||||
var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = alloc }) catch null;
|
var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = self.alloc }) catch null;
|
||||||
if (maybe_user) |u| {
|
if (maybe_user) |u| {
|
||||||
defer std.json.parseFree(User, u, .{ .allocator = alloc });
|
defer std.json.parseFree(User, u, .{ .allocator = self.alloc });
|
||||||
if (users.addByName(u.first_name, u.last_name)) |id| {
|
if (self.users.addByName(u.first_name, u.last_name)) |id| {
|
||||||
var jsonbuf: [128]u8 = undefined;
|
var jsonbuf: [128]u8 = undefined;
|
||||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
||||||
r.sendJson(json) catch return;
|
r.sendJson(json) catch return;
|
||||||
|
@ -94,17 +95,17 @@ fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
const self = @fieldParentPtr(Self, "endpoint", e);
|
||||||
if (r.path) |path| {
|
if (r.path) |path| {
|
||||||
if (userIdFromPath(path)) |id| {
|
if (self.userIdFromPath(path)) |id| {
|
||||||
if (users.get(id)) |_| {
|
if (self.users.get(id)) |_| {
|
||||||
if (r.body) |body| {
|
if (r.body) |body| {
|
||||||
var stream = std.json.TokenStream.init(body);
|
var stream = std.json.TokenStream.init(body);
|
||||||
var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = alloc }) catch null;
|
var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = self.alloc }) catch null;
|
||||||
if (maybe_user) |u| {
|
if (maybe_user) |u| {
|
||||||
defer std.json.parseFree(User, u, .{ .allocator = alloc });
|
defer std.json.parseFree(User, u, .{ .allocator = self.alloc });
|
||||||
var jsonbuf: [128]u8 = undefined;
|
var jsonbuf: [128]u8 = undefined;
|
||||||
if (users.update(id, u.first_name, u.last_name)) {
|
if (self.users.update(id, u.first_name, u.last_name)) {
|
||||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
||||||
r.sendJson(json) catch return;
|
r.sendJson(json) catch return;
|
||||||
}
|
}
|
||||||
|
@ -121,11 +122,11 @@ fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deleteUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
fn deleteUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
const self = @fieldParentPtr(Self, "endpoint", e);
|
||||||
if (r.path) |path| {
|
if (r.path) |path| {
|
||||||
if (userIdFromPath(path)) |id| {
|
if (self.userIdFromPath(path)) |id| {
|
||||||
var jsonbuf: [128]u8 = undefined;
|
var jsonbuf: [128]u8 = undefined;
|
||||||
if (users.delete(id)) {
|
if (self.users.delete(id)) {
|
||||||
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
|
||||||
r.sendJson(json) catch return;
|
r.sendJson(json) catch return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,15 +21,15 @@ pub fn main() !void {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
Endpoint.init(allocator, "/users");
|
var endpoint = Endpoint.init(allocator, "/users");
|
||||||
|
|
||||||
// add endpoint
|
// add endpoint
|
||||||
try listener.addEndpoint(Endpoint.getUserEndpoint());
|
try listener.addEndpoint(endpoint.getUserEndpoint());
|
||||||
|
|
||||||
// fake some users
|
// fake some users
|
||||||
var uid: usize = undefined;
|
var uid: usize = undefined;
|
||||||
uid = try Endpoint.getUsers().addByName("renerocksai", null);
|
uid = try endpoint.getUsers().addByName("renerocksai", null);
|
||||||
uid = try Endpoint.getUsers().addByName("renerocksai", "your mom");
|
uid = try endpoint.getUsers().addByName("renerocksai", "your mom");
|
||||||
|
|
||||||
// listen
|
// listen
|
||||||
try listener.listen();
|
try listener.listen();
|
||||||
|
|
Loading…
Add table
Reference in a new issue