1
1
const std = @import ("std" );
2
-
2
+ const builtin = @import ( "builtin" );
3
3
const zzz = @import ("zzz" );
4
4
const http = zzz .HTTP ;
5
-
6
5
const tardy = zzz .tardy ;
7
6
const Tardy = tardy .Tardy (.auto );
8
7
const Runtime = tardy .Runtime ;
9
8
const Socket = tardy .Socket ;
10
-
11
9
const Server = http .Server ;
12
10
const Router = http .Router ;
13
11
const Context = http .Context ;
@@ -21,34 +19,27 @@ pub fn main() !void {
21
19
const host : []const u8 = "0.0.0.0" ;
22
20
const port : u16 = 8080 ;
23
21
24
- const date_thread = try std .Thread .spawn (.{}, struct {
25
- fn a () ! void {
26
- while (true ) {
27
- var d = http .Date .init (std .time .timestamp ());
28
- const http_date = d .to_http_date ();
29
- _ = try http_date .into_buf (date [0.. ]);
30
- std .time .sleep (std .time .ns_per_ms * 985 );
31
- }
32
- }
33
- }.a , .{});
34
-
35
- date_thread .detach ();
36
-
37
22
var gpa = std .heap .GeneralPurposeAllocator (.{}){};
38
- defer if (gpa .deinit () == .leak ) {
39
- @panic ("Memory leak has occurred!" );
23
+
24
+ const allocator , const is_debug = switch (builtin .mode ) {
25
+ .Debug , .ReleaseSafe = > .{ gpa .allocator (), true },
26
+ .ReleaseSmall , .ReleaseFast = > .{ std .heap .smp_allocator , false },
40
27
};
41
28
42
- const allocator = gpa .allocator ();
29
+ defer {
30
+ if (is_debug and gpa .deinit () == .leak ) {
31
+ @panic ("Memory leak has occurred!" );
32
+ }
33
+ }
43
34
44
35
var t = try Tardy .init (allocator , .{
45
36
.threading = .all ,
46
37
});
47
38
defer t .deinit ();
48
39
49
40
var router = try Router .init (allocator , &.{
50
- Route .init ("/plaintext" ).get ({}, home_handler ).layer (),
51
- Route .init ("/json" ).get ({}, json_handler ).layer (),
41
+ Route .init ("/plaintext" ).get ({}, homeHandler ).layer (),
42
+ Route .init ("/json" ).get ({}, jsonHandler ).layer (),
52
43
}, .{});
53
44
defer router .deinit (allocator );
54
45
@@ -66,6 +57,7 @@ pub fn main() !void {
66
57
EntryParams { .router = & router , .socket = socket },
67
58
struct {
68
59
fn entry (rt : * Runtime , p : EntryParams ) ! void {
60
+ if (rt .id == 0 ) try rt .spawn (.{rt }, updateDate , 1024 * 1024 * 4 );
69
61
var server = Server .init (.{
70
62
.capture_count_max = 0 ,
71
63
});
@@ -75,24 +67,48 @@ pub fn main() !void {
75
67
);
76
68
}
77
69
78
- pub fn home_handler (ctx : * const Context , _ : void ) ! Respond {
70
+ pub fn homeHandler (ctx : * const Context , _ : void ) ! Respond {
79
71
return ctx .response .apply (.{
80
72
.mime = http .Mime .TEXT ,
81
73
.body = "Hello, World!" ,
82
74
.status = .OK ,
83
75
.headers = &.{
84
- .{ "Date" , try ctx .allocator .dupe (u8 , date [0.. ])},
76
+ .{ "Date" , try ctx .allocator .dupe (u8 , date [0.. ]) },
85
77
},
86
78
});
87
79
}
88
80
89
- pub fn json_handler (ctx : * const Context , _ : void ) ! Respond {
81
+ pub fn jsonHandler (ctx : * const Context , _ : void ) ! Respond {
90
82
return ctx .response .apply (.{
91
83
.mime = http .Mime .JSON ,
92
84
.body = try std .json .stringifyAlloc (ctx .allocator , Message { .message = "Hello, World!" }, .{}),
93
85
.status = .OK ,
94
86
.headers = &.{
95
- .{"Date" , try ctx .allocator .dupe (u8 , date [0.. ])},
87
+ .{ "Date" , try ctx .allocator .dupe (u8 , date [0.. ]) },
96
88
},
97
89
});
98
90
}
91
+
92
+ pub fn updateDate (rt : * Runtime ) ! void {
93
+ const format = std .fmt .comptimePrint (
94
+ "{s}, {s} {s} {s} {s}:{s}:{s} GMT" ,
95
+ .{
96
+ "{[day_name]s}" ,
97
+ "{[day]d:0>2}" ,
98
+ "{[month]s}" ,
99
+ "{[year]d}" ,
100
+ "{[hour]d:0>2}" ,
101
+ "{[minute]d:0>2}" ,
102
+ "{[second]d:0>2}" ,
103
+ },
104
+ );
105
+ while (true ) {
106
+ var d = http .Date .init (std .time .timestamp ());
107
+
108
+ const http_date = d .to_http_date ();
109
+
110
+ _ = try std .fmt .bufPrint (& date , format , http_date );
111
+
112
+ try tardy .Timer .delay (rt , .{ .nanos = std .time .ns_per_ms * 900 });
113
+ }
114
+ }
0 commit comments