@@ -4,22 +4,16 @@ const pg = @import("pg");
4
4
const datetimez = @import ("datetimez" );
5
5
const mustache = @import ("mustache" );
6
6
7
- const Allocator = std .mem .Allocator ;
8
7
const Thread = std .Thread ;
9
8
const Mutex = Thread .Mutex ;
10
9
const template = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>{{#fortunes}}<tr><td>{{id}}</td><td>{{message}}</td></tr>{{/fortunes}}</table></body></html>" ;
11
10
12
11
pub const Global = struct {
13
12
pool : * pg.Pool ,
14
13
prng : * std.rand.DefaultPrng ,
15
- allocator : Allocator ,
16
14
mutex : std.Thread.Mutex = .{},
17
15
};
18
16
19
- const Message = struct {
20
- message : []const u8 ,
21
- };
22
-
23
17
const World = struct {
24
18
id : i32 ,
25
19
randomNumber : i32 ,
@@ -30,23 +24,21 @@ const Fortune = struct {
30
24
message : []const u8 ,
31
25
};
32
26
33
- pub fn plaintext (global : * Global , _ : * httpz.Request , res : * httpz.Response ) ! void {
34
- try setHeaders (global . allocator , res );
27
+ pub fn plaintext (_ : * Global , _ : * httpz.Request , res : * httpz.Response ) ! void {
28
+ try setHeaders (res . arena , res );
35
29
36
30
res .content_type = .TEXT ;
37
31
res .body = "Hello, World!" ;
38
32
}
39
33
40
- pub fn json (global : * Global , _ : * httpz.Request , res : * httpz.Response ) ! void {
41
- try setHeaders (global . allocator , res );
34
+ pub fn json (_ : * Global , _ : * httpz.Request , res : * httpz.Response ) ! void {
35
+ try setHeaders (res . arena , res );
42
36
43
- const message = Message { .message = "Hello, World!" };
44
-
45
- try res .json (message , .{});
37
+ try res .json (.{ .message = "Hello, World!" }, .{});
46
38
}
47
39
48
40
pub fn db (global : * Global , _ : * httpz.Request , res : * httpz.Response ) ! void {
49
- try setHeaders (global . allocator , res );
41
+ try setHeaders (res . arena , res );
50
42
51
43
global .mutex .lock ();
52
44
const random_number = 1 + (global .prng .random ().uintAtMost (u32 , 9999 ));
@@ -61,15 +53,15 @@ pub fn db(global: *Global, _: *httpz.Request, res: *httpz.Response) !void {
61
53
}
62
54
63
55
pub fn fortune (global : * Global , _ : * httpz.Request , res : * httpz.Response ) ! void {
64
- try setHeaders (global . allocator , res );
56
+ try setHeaders (res . arena , res );
65
57
66
- const fortunes_html = try getFortunesHtml (global . allocator , global .pool );
58
+ const fortunes_html = try getFortunesHtml (res . arena , global .pool );
67
59
68
60
res .header ("content-type" , "text/html; charset=utf-8" );
69
61
res .body = fortunes_html ;
70
62
}
71
63
72
- fn getWorld (pool : * pg.Pool , random_number : u32 ) ! World {
64
+ fn getWorld (pool : * pg.Pool , random_number : u32 ) ! World {
73
65
var conn = try pool .acquire ();
74
66
defer conn .release ();
75
67
@@ -81,7 +73,7 @@ fn getWorld(pool: *pg.Pool, random_number: u32) !World{
81
73
return World { .id = row .get (i32 , 0 ), .randomNumber = row .get (i32 , 1 ) };
82
74
}
83
75
84
- fn setHeaders (allocator : Allocator , res : * httpz.Response ) ! void {
76
+ fn setHeaders (allocator : std.mem. Allocator , res : * httpz.Response ) ! void {
85
77
res .header ("Server" , "Httpz" );
86
78
87
79
const now = datetimez .datetime .Date .now ();
@@ -97,10 +89,10 @@ fn setHeaders(allocator: Allocator, res: *httpz.Response) !void {
97
89
res .header ("Date" , now_str );
98
90
}
99
91
100
- fn getFortunesHtml (allocator : Allocator , pool : * pg.Pool ) ! []const u8 {
92
+ fn getFortunesHtml (allocator : std.mem. Allocator , pool : * pg.Pool ) ! []const u8 {
101
93
const fortunes = try getFortunes (allocator , pool );
102
94
103
- const raw = try mustache .allocRenderText (allocator , template ,.{ .fortunes = fortunes });
95
+ const raw = try mustache .allocRenderText (allocator , template , .{ .fortunes = fortunes });
104
96
105
97
// std.debug.print("mustache output {s}\n", .{raw});
106
98
@@ -111,7 +103,7 @@ fn getFortunesHtml(allocator: Allocator, pool: *pg.Pool) ![]const u8 {
111
103
return html ;
112
104
}
113
105
114
- fn getFortunes (allocator : Allocator , pool : * pg.Pool ) ! []const Fortune {
106
+ fn getFortunes (allocator : std.mem. Allocator , pool : * pg.Pool ) ! []const Fortune {
115
107
var conn = try pool .acquire ();
116
108
defer conn .release ();
117
109
@@ -139,7 +131,7 @@ fn cmpFortuneByMessage(_: void, a: Fortune, b: Fortune) bool {
139
131
return std .mem .order (u8 , a .message , b .message ).compare (std .math .CompareOperator .lt );
140
132
}
141
133
142
- fn deescapeHtml (allocator : Allocator , input : []const u8 ) ! []const u8 {
134
+ fn deescapeHtml (allocator : std.mem. Allocator , input : []const u8 ) ! []const u8 {
143
135
var output = std .ArrayList (u8 ).init (allocator );
144
136
defer output .deinit ();
145
137
@@ -189,4 +181,3 @@ fn deescapeHtml(allocator: Allocator, input: []const u8) ![]const u8 {
189
181
190
182
return output .toOwnedSlice ();
191
183
}
192
-
0 commit comments