To serve HTTP responses, you need to initialize the tardy.Tardy runtime, set up an http.Router with routes, and bind a tardy.net.Socket to a host and port. The server is then started within the t.entry callback, which provides access to the Runtime.
const std = @import("std");
const zzz = @import("zzz");
const http = zzz.http;
const tardy = zzz.tardy;
const Runtime = tardy.Runtime;
const Socket = tardy.net.Socket;
const Server = http.Server;
const Router = http.Router;
const Context = http.Context;
const Route = Router.Route;
const Respond = http.Respond;
const log = std.log.scoped(.@"examples/basic");
const Tardy = tardy.Tardy(.auto);
fn base_handler(ctx: *const Context, _: void) !Respond {
return ctx.response.apply(.{
.status = .OK,
.mime = http.Mime.HTML,
.body = "Hello, world!",
});
}
pub fn main(init: std.process.Init) !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;
var t: Tardy = try .init(init.gpa, init.io, .{ .threading = .auto });
defer t.deinit();
var router: Router = try .init(init.gpa, &{
Route.init("/").get({}, base_handler).layer(),
}, .{});
defer router.deinit(init.gpa);
// create socket for tardy
var socket: Socket = try .init(init.io, .{
.tcp = .{ .host = host, .port = port },
});
defer socket.close_blocking();
try socket.bind();
try socket.listen(4096);
const EntryParams = struct {
router: *const Router,
socket: Socket,
};
const params: EntryParams = .{ .router = &router, .socket = socket };
try t.entry(
params,
struct {
fn entry(rt: *Runtime, p: EntryParams) !void {
var server: Server = .init(.{
.stack_size = .@"4MiB",
.socket_buffer_bytes = 1024 * 2,
.keepalive_count_max = null,
.connection_count_max = 1024,
});
try server.serve(rt, p.router, .{ .normal = p.socket });
}
}.entry,
);
}