forked from zigzap/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_json.zig
More file actions
75 lines (64 loc) · 2.12 KB
/
hello_json.zig
File metadata and controls
75 lines (64 loc) · 2.12 KB
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
//!
//! Part of the Zap examples.
//!
//! Build me with `zig build hello_json`.
//! Run me with `zig build run-hello_json`.
//!
const std = @import("std");
const zap = @import("zap");
const User = struct {
first_name: ?[]const u8 = null,
last_name: ?[]const u8 = null,
};
fn on_request(r: zap.Request) !void {
if (r.methodAsEnum() != .GET) return;
// /user/n
if (r.path) |the_path| {
if (the_path.len < 7 or !std.mem.startsWith(u8, the_path, "/user/"))
return;
const user_id: usize = @intCast(the_path[6] - 0x30);
std.debug.print("user_id: {d}\n", .{user_id});
std.debug.print("users: {d}\n", .{users.count()});
const user = users.get(user_id);
std.debug.print("user: {?}\n", .{user});
var buf: [256]u8 = undefined;
var json_to_send: []const u8 = undefined;
json_to_send = try zap.util.stringifyBuf(&buf, user, .{});
std.debug.print("<< json: {s}\n", .{json_to_send});
r.setContentType(.JSON) catch return;
r.setContentTypeFromFilename("test.json") catch return;
r.sendBody(json_to_send) catch return;
}
}
const UserMap = std.AutoHashMap(usize, User);
var users: UserMap = undefined;
fn setupUserData(a: std.mem.Allocator) !void {
users = UserMap.init(a);
try users.put(1, .{ .first_name = "renerocksai" });
try users.put(2, .{ .first_name = "Your", .last_name = "Mom" });
}
pub fn main() !void {
const a = std.heap.page_allocator;
try setupUserData(a);
defer users.deinit();
var listener = zap.HttpListener.init(.{
.port = 3000,
.on_request = on_request,
.log = false,
});
try listener.listen();
std.debug.print(
\\ Listening on 0.0.0.0:3000
\\
\\ Check out:
\\ http://localhost:3000/user/1 # -- first user
\\ http://localhost:3000/user/2 # -- second user
\\ http://localhost:3000/user/3 # -- non-existing user
\\
, .{});
// start worker threads
zap.start(.{
.threads = 2,
.workers = 1, // user map cannot be shared among multiple worker processes
});
}