Something went wrong. Try again.
An HTTP/1.1 server for zig forked from karlseguin.tngl.sh/http.zig
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556const std = @import("std");const httpz = @import("httpz");const Allocator = std.mem.Allocator;
const PORT = 8809;
// This example demonstrates how to shutdown httpz// Only works on Linux/MacOS/BSD
var server_instance: ?*httpz.Server(void) = null;
pub fn main(init: std.process.Init) !void { if (comptime @import("builtin").os.tag == .windows) { std.debug.print("This example does not run on Windows. Sorry\n", .{}); return error.PlatformNotSupported; } const allocator = init.gpa;
// call our shutdown function (below) when // SIGINT or SIGTERM are received std.posix.sigaction(std.posix.SIG.INT, &.{ .handler = .{ .handler = shutdown }, .mask = std.posix.sigemptyset(), .flags = 0, }, null); std.posix.sigaction(std.posix.SIG.TERM, &.{ .handler = .{ .handler = shutdown }, .mask = std.posix.sigemptyset(), .flags = 0, }, null);
var server = try httpz.Server(void).init(init.io, allocator, .{ .address = .localhost(PORT) }, {}); defer server.deinit();
var router = try server.router(.{}); router.get("/", index, .{});
std.debug.print("listening http://localhost:{d}/\n", .{PORT}); std.debug.print("process id (pid): {d}\n", .{std.c.getpid()});
server_instance = &server; try server.listen();}
fn shutdown(_: c_int) callconv(.c) void { if (server_instance) |server| { server_instance = null; server.stop(); }}
fn index(_: *httpz.Request, res: *httpz.Response) !void { const writer = res.writer(); return writer.print("To shutdown, run:\nkill -s int {d}", .{std.c.getpid()});}