Skip to content

Commit adfc1ff

Browse files
authored
[Zig] Update ZZZ (#9962)
* update zig to 0.14.1 and fix date formating issue on day < 10 * fix(Zig/ZZZ): tar and mv cmd * Update zzz.dockerfile * Update zzz.dockerfile
1 parent 0eb6214 commit adfc1ff

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

frameworks/Zig/zzz/build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
.minimum_zig_version = "0.14.0",
66
.dependencies = .{
77
.zzz = .{
8-
.url = "git+https://github.com/tardy-org/zzz#90cc62494644e7234efd85ab1df5d65440f9eead",
9-
.hash = "zzz-0.3.0-4HoaJqpQAgDgcImt8cC2cpT59J25JNDynMDt4qLaXDYK",
8+
.url = "git+https://github.com/tardy-org/zzz#18ec7f1129ce4d0573b7c67f011b4d05c7b195d4",
9+
.hash = "zzz-0.3.0-4HoaJrBQAgCXB_66nre0nvYZmAXMxj__gS8sFjpR68Hs",
1010
},
1111
},
1212
.paths = .{

frameworks/Zig/zzz/src/main.zig

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const std = @import("std");
2-
2+
const builtin = @import("builtin");
33
const zzz = @import("zzz");
44
const http = zzz.HTTP;
5-
65
const tardy = zzz.tardy;
76
const Tardy = tardy.Tardy(.auto);
87
const Runtime = tardy.Runtime;
98
const Socket = tardy.Socket;
10-
119
const Server = http.Server;
1210
const Router = http.Router;
1311
const 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+
}

frameworks/Zig/zzz/zzz.dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ COPY build.zig.zon build.zig.zon
2626
USER ziguser
2727

2828
RUN zig build -Doptimize=ReleaseFast -Dcpu=native
29-
RUN ls
3029

3130
FROM debian:12-slim
3231

0 commit comments

Comments
 (0)