11const std = @import ("std" );
2-
2+ const builtin = @import ( "builtin" );
33const zzz = @import ("zzz" );
44const http = zzz .HTTP ;
5-
65const tardy = zzz .tardy ;
76const Tardy = tardy .Tardy (.auto );
87const Runtime = tardy .Runtime ;
98const Socket = tardy .Socket ;
10-
119const Server = http .Server ;
1210const Router = http .Router ;
1311const Context = http .Context ;
@@ -21,34 +19,27 @@ pub fn main() !void {
2119 const host : []const u8 = "0.0.0.0" ;
2220 const port : u16 = 8080 ;
2321
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-
3722 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 },
4027 };
4128
42- const allocator = gpa .allocator ();
29+ defer {
30+ if (is_debug and gpa .deinit () == .leak ) {
31+ @panic ("Memory leak has occurred!" );
32+ }
33+ }
4334
4435 var t = try Tardy .init (allocator , .{
4536 .threading = .all ,
4637 });
4738 defer t .deinit ();
4839
4940 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 (),
5243 }, .{});
5344 defer router .deinit (allocator );
5445
@@ -66,6 +57,7 @@ pub fn main() !void {
6657 EntryParams { .router = & router , .socket = socket },
6758 struct {
6859 fn entry (rt : * Runtime , p : EntryParams ) ! void {
60+ if (rt .id == 0 ) try rt .spawn (.{rt }, updateDate , 1024 * 1024 * 4 );
6961 var server = Server .init (.{
7062 .capture_count_max = 0 ,
7163 });
@@ -75,24 +67,48 @@ pub fn main() !void {
7567 );
7668}
7769
78- pub fn home_handler (ctx : * const Context , _ : void ) ! Respond {
70+ pub fn homeHandler (ctx : * const Context , _ : void ) ! Respond {
7971 return ctx .response .apply (.{
8072 .mime = http .Mime .TEXT ,
8173 .body = "Hello, World!" ,
8274 .status = .OK ,
8375 .headers = &.{
84- .{ "Date" , try ctx .allocator .dupe (u8 , date [0.. ])},
76+ .{ "Date" , try ctx .allocator .dupe (u8 , date [0.. ]) },
8577 },
8678 });
8779}
8880
89- pub fn json_handler (ctx : * const Context , _ : void ) ! Respond {
81+ pub fn jsonHandler (ctx : * const Context , _ : void ) ! Respond {
9082 return ctx .response .apply (.{
9183 .mime = http .Mime .JSON ,
9284 .body = try std .json .stringifyAlloc (ctx .allocator , Message { .message = "Hello, World!" }, .{}),
9385 .status = .OK ,
9486 .headers = &.{
95- .{"Date" , try ctx .allocator .dupe (u8 , date [0.. ])},
87+ .{ "Date" , try ctx .allocator .dupe (u8 , date [0.. ]) },
9688 },
9789 });
9890}
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