forked from zigzap/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mustache.zig
62 lines (56 loc) · 1.65 KB
/
mustache.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
const std = @import("std");
const zap = @import("zap");
fn on_request(r: zap.SimpleRequest) void {
const template = "{{=<< >>=}}* Users:\r\n<<#users>><<id>>. <<& name>> (<<name>>)\r\n<</users>>\r\nNested: <<& nested.item >>.";
const p = zap.mustacheData(template) catch return;
defer zap.mustacheFree(p);
const User = struct {
name: []const u8,
id: isize,
};
const ret = zap.mustacheBuild(p, .{
.users = [_]User{
.{
.name = "Rene",
.id = 1,
},
.{
.name = "Caro",
.id = 6,
},
},
.nested = .{
.item = "nesting works",
},
});
defer ret.deinit();
if (r.setContentType(.TEXT)) {
if (ret.str()) |s| {
r.sendBody(s) catch return;
} else {
r.sendBody("<html><body><h1>mustacheBuild() failed!</h1></body></html>") catch return;
}
} else |err| {
std.debug.print("Error while setting content type: {}\n", .{err});
}
}
pub fn main() !void {
var listener = zap.SimpleHttpListener.init(.{
.port = 3000,
.on_request = on_request,
.log = true,
.max_clients = 100000,
});
try listener.listen();
// zap.enableDebugLog();
// zap.debug("ZAP debug logging is on\n", .{});
// we can also use facilio logging
// zap.Log.fio_set_log_level(zap.Log.fio_log_level_debug);
// zap.Log.fio_log_debug("hello from fio\n");
std.debug.print("Listening on 0.0.0.0:3000\n", .{});
// start worker threads
zap.start(.{
.threads = 1,
.workers = 1,
});
}