diff --git a/frameworks/Zig/zzz/build.zig.zon b/frameworks/Zig/zzz/build.zig.zon index 8147f5750a0..58f2a17b2b1 100644 --- a/frameworks/Zig/zzz/build.zig.zon +++ b/frameworks/Zig/zzz/build.zig.zon @@ -5,8 +5,8 @@ .minimum_zig_version = "0.14.0", .dependencies = .{ .zzz = .{ - .url = "git+https://github.com/tardy-org/zzz#90cc62494644e7234efd85ab1df5d65440f9eead", - .hash = "zzz-0.3.0-4HoaJqpQAgDgcImt8cC2cpT59J25JNDynMDt4qLaXDYK", + .url = "git+https://github.com/tardy-org/zzz#18ec7f1129ce4d0573b7c67f011b4d05c7b195d4", + .hash = "zzz-0.3.0-4HoaJrBQAgCXB_66nre0nvYZmAXMxj__gS8sFjpR68Hs", }, }, .paths = .{ diff --git a/frameworks/Zig/zzz/src/main.zig b/frameworks/Zig/zzz/src/main.zig index a3d51bf4225..bfe20def8a0 100644 --- a/frameworks/Zig/zzz/src/main.zig +++ b/frameworks/Zig/zzz/src/main.zig @@ -1,13 +1,11 @@ const std = @import("std"); - +const builtin = @import("builtin"); const zzz = @import("zzz"); const http = zzz.HTTP; - const tardy = zzz.tardy; const Tardy = tardy.Tardy(.auto); const Runtime = tardy.Runtime; const Socket = tardy.Socket; - const Server = http.Server; const Router = http.Router; const Context = http.Context; @@ -21,25 +19,18 @@ pub fn main() !void { const host: []const u8 = "0.0.0.0"; const port: u16 = 8080; - const date_thread = try std.Thread.spawn(.{}, struct { - fn a() !void { - while (true) { - var d = http.Date.init(std.time.timestamp()); - const http_date = d.to_http_date(); - _ = try http_date.into_buf(date[0..]); - std.time.sleep(std.time.ns_per_ms * 985); - } - } - }.a, .{}); - - date_thread.detach(); - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; - defer if (gpa.deinit() == .leak) { - @panic("Memory leak has occurred!"); + + const allocator, const is_debug = switch (builtin.mode) { + .Debug, .ReleaseSafe => .{ gpa.allocator(), true }, + .ReleaseSmall, .ReleaseFast => .{ std.heap.smp_allocator, false }, }; - const allocator = gpa.allocator(); + defer { + if (is_debug and gpa.deinit() == .leak) { + @panic("Memory leak has occurred!"); + } + } var t = try Tardy.init(allocator, .{ .threading = .all, @@ -47,8 +38,8 @@ pub fn main() !void { defer t.deinit(); var router = try Router.init(allocator, &.{ - Route.init("/plaintext").get({}, home_handler).layer(), - Route.init("/json").get({}, json_handler).layer(), + Route.init("/plaintext").get({}, homeHandler).layer(), + Route.init("/json").get({}, jsonHandler).layer(), }, .{}); defer router.deinit(allocator); @@ -66,6 +57,7 @@ pub fn main() !void { EntryParams{ .router = &router, .socket = socket }, struct { fn entry(rt: *Runtime, p: EntryParams) !void { + if (rt.id == 0) try rt.spawn(.{rt}, updateDate, 1024 * 1024 * 4); var server = Server.init(.{ .capture_count_max = 0, }); @@ -75,24 +67,48 @@ pub fn main() !void { ); } -pub fn home_handler(ctx: *const Context, _: void) !Respond { +pub fn homeHandler(ctx: *const Context, _: void) !Respond { return ctx.response.apply(.{ .mime = http.Mime.TEXT, .body = "Hello, World!", .status = .OK, .headers = &.{ - .{"Date", try ctx.allocator.dupe(u8, date[0..])}, + .{ "Date", try ctx.allocator.dupe(u8, date[0..]) }, }, }); } -pub fn json_handler(ctx: *const Context, _: void) !Respond { +pub fn jsonHandler(ctx: *const Context, _: void) !Respond { return ctx.response.apply(.{ .mime = http.Mime.JSON, .body = try std.json.stringifyAlloc(ctx.allocator, Message{ .message = "Hello, World!" }, .{}), .status = .OK, .headers = &.{ - .{"Date", try ctx.allocator.dupe(u8, date[0..])}, + .{ "Date", try ctx.allocator.dupe(u8, date[0..]) }, }, }); } + +pub fn updateDate(rt: *Runtime) !void { + const format = std.fmt.comptimePrint( + "{s}, {s} {s} {s} {s}:{s}:{s} GMT", + .{ + "{[day_name]s}", + "{[day]d:0>2}", + "{[month]s}", + "{[year]d}", + "{[hour]d:0>2}", + "{[minute]d:0>2}", + "{[second]d:0>2}", + }, + ); + while (true) { + var d = http.Date.init(std.time.timestamp()); + + const http_date = d.to_http_date(); + + _ = try std.fmt.bufPrint(&date, format, http_date); + + try tardy.Timer.delay(rt, .{ .nanos = std.time.ns_per_ms * 900 }); + } +} diff --git a/frameworks/Zig/zzz/zzz.dockerfile b/frameworks/Zig/zzz/zzz.dockerfile index 135445cadfa..514196a7497 100644 --- a/frameworks/Zig/zzz/zzz.dockerfile +++ b/frameworks/Zig/zzz/zzz.dockerfile @@ -26,7 +26,6 @@ COPY build.zig.zon build.zig.zon USER ziguser RUN zig build -Doptimize=ReleaseFast -Dcpu=native -RUN ls FROM debian:12-slim