forked from zigzap/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userweb.zig
152 lines (138 loc) · 4.74 KB
/
userweb.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const std = @import("std");
const zap = @import("zap");
const Users = @import("users.zig");
const User = Users.User;
// an Endpoint
pub const Self = @This();
alloc: std.mem.Allocator = undefined,
ep: zap.Endpoint = undefined,
_users: Users = undefined,
pub fn init(
a: std.mem.Allocator,
user_path: []const u8,
) Self {
return .{
.alloc = a,
._users = Users.init(a),
.ep = zap.Endpoint.init(.{
.path = user_path,
.get = getUser,
.post = postUser,
.put = putUser,
.patch = putUser,
.delete = deleteUser,
.options = optionsUser,
}),
};
}
pub fn deinit(self: *Self) void {
self._users.deinit();
}
pub fn users(self: *Self) *Users {
return &self._users;
}
pub fn endpoint(self: *Self) *zap.Endpoint {
return &self.ep;
}
fn userIdFromPath(self: *Self, path: []const u8) ?usize {
if (path.len >= self.ep.settings.path.len + 2) {
if (path[self.ep.settings.path.len] != '/') {
return null;
}
const idstr = path[self.ep.settings.path.len + 1 ..];
return std.fmt.parseUnsigned(usize, idstr, 10) catch null;
}
return null;
}
fn getUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
if (r.path) |path| {
// /users
if (path.len == e.settings.path.len) {
return self.listUsers(r);
}
var jsonbuf: [256]u8 = undefined;
if (self.userIdFromPath(path)) |id| {
if (self._users.get(id)) |user| {
if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| {
r.sendJson(json) catch return;
}
}
}
}
}
fn listUsers(self: *Self, r: zap.Request) void {
if (self._users.toJSON()) |json| {
defer self.alloc.free(json);
r.sendJson(json) catch return;
} else |err| {
std.debug.print("LIST error: {}\n", .{err});
}
}
fn postUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
if (r.body) |body| {
var maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null;
if (maybe_user) |u| {
defer u.deinit();
if (self._users.addByName(u.value.first_name, u.value.last_name)) |id| {
var jsonbuf: [128]u8 = undefined;
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
r.sendJson(json) catch return;
}
} else |err| {
std.debug.print("ADDING error: {}\n", .{err});
return;
}
}
}
}
fn putUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
if (r.path) |path| {
if (self.userIdFromPath(path)) |id| {
if (self._users.get(id)) |_| {
if (r.body) |body| {
var maybe_user: ?std.json.Parsed(User) = std.json.parseFromSlice(User, self.alloc, body, .{}) catch null;
if (maybe_user) |u| {
defer u.deinit();
var jsonbuf: [128]u8 = undefined;
if (self._users.update(id, u.value.first_name, u.value.last_name)) {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
r.sendJson(json) catch return;
}
} else {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| {
r.sendJson(json) catch return;
}
}
}
}
}
}
}
}
fn deleteUser(e: *zap.Endpoint, r: zap.Request) void {
const self = @fieldParentPtr(Self, "ep", e);
if (r.path) |path| {
if (self.userIdFromPath(path)) |id| {
var jsonbuf: [128]u8 = undefined;
if (self._users.delete(id)) {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| {
r.sendJson(json) catch return;
}
} else {
if (zap.stringifyBuf(&jsonbuf, .{ .status = "ERROR", .id = id }, .{})) |json| {
r.sendJson(json) catch return;
}
}
}
}
}
fn optionsUser(e: *zap.Endpoint, r: zap.Request) void {
_ = e;
r.setHeader("Access-Control-Allow-Origin", "*") catch return;
r.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") catch return;
r.setStatus(zap.StatusCode.no_content);
r.markAsFinished(true);
}