|
@@ -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 });
|
|
|
+ }
|
|
|
+}
|