### Creating a Basic "Hello, World" HTTP Server in Zig Source: https://github.com/tardy-org/zzz/blob/main/docs/getting_started.md This Zig code defines a simple HTTP server using the zzz framework. It sets up a Tardy runtime, initializes a router with a single route ("/") that handles GET requests, binds the server to a specific host and port, and serves incoming connections with a "Hello, World!" response. It demonstrates basic server setup, routing, and response handling. ```Zig const std = @import("std"); const log = std.log.scoped(.@"examples/basic"); 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; const Route = http.Route; const Respond = http.Respond; fn base_handler(ctx: *const Context, _: void) !Respond { return ctx.response.apply(.{ .status = .OK, .mime = http.Mime.HTML, .body = "Hello, world!", }); } pub fn main() !void { const host: []const u8 = "0.0.0.0"; const port: u16 = 9862; var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){}; const allocator = gpa.allocator(); defer _ = gpa.deinit(); var t = try Tardy.init(allocator, .{ .threading = .auto }); defer t.deinit(); var router = try Router.init(allocator, &.{ Route.init("/").get({}, base_handler).layer(), }, .{}); defer router.deinit(allocator); var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } }); defer socket.close_blocking(); try socket.bind(); try socket.listen(4096); const EntryParams = struct { router: *const Router, socket: Socket, }; try t.entry( EntryParams{ .router = &router, .socket = socket }, struct { fn entry(rt: *Runtime, p: EntryParams) !void { var server = Server.init(.{ .stack_size = 1024 * 1024 * 4, .socket_buffer_bytes = 1024 * 2, .keepalive_count_max = null, .connection_count_max = 1024, }); try server.serve(rt, p.router, .{ .normal = p.socket }); } }.entry, ); } ``` -------------------------------- ### Fetching zzz Library with Zig Source: https://github.com/tardy-org/zzz/blob/main/docs/getting_started.md Command to fetch and save the zzz library at a specific version using the Zig package manager. This is a prerequisite for using the library in a Zig project. ```Shell zig fetch --save git+https://github.com/mookums/zzz#v0.3.0 ``` -------------------------------- ### Setting up an HTTPS Server with zzz, tardy, and secsock in Zig Source: https://github.com/tardy-org/zzz/blob/main/docs/https.md This Zig code snippet demonstrates how to initialize a zzz HTTP server with TLS support using the tardy runtime and secsock library. It shows how to set up a basic router, create a TCP socket, initialize secsock's BearSSL, add embedded certificate and key files, convert the socket to a secure socket, and finally serve the router securely using the tardy runtime's entry point. It requires zzz, tardy, and secsock dependencies and assumes certificate and key files are available. ```Zig const std = @import("std"); const log = std.log.scoped(.@"examples/tls"); 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 Context = http.Context; const Route = http.Route; const Router = http.Router; const Respond = http.Respond; const secsock = zzz.secsock; const SecureSocket = secsock.SecureSocket; fn root_handler(ctx: *const Context, _: void) !Respond { const body = "\\ \n" "\\ \n" "\\
\n" "\\ \n" "\\ \n" "\\ \n" "\\