Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frameworks/Zig/zzz/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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 = .{
Expand Down
66 changes: 41 additions & 25 deletions frameworks/Zig/zzz/src/main.zig
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,34 +19,27 @@ 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,
});
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);

Expand All @@ -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,
});
Expand All @@ -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 });
}
}
1 change: 0 additions & 1 deletion frameworks/Zig/zzz/zzz.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading