const std = @import("std"); const Allocator = std.mem.Allocator; const DebugAllocator = std.heap.DebugAllocator; const Sigaction = std.posix.Sigaction; const Io = std.Io; const Threaded = Io.Threaded; const builtin = @import("builtin"); const zits = @import("zits"); const Message = zits.Server.message.Message; const ServerInfo = Message.ServerInfo; const Server = zits.Server; const safe_build = builtin.mode == .Debug or builtin.mode == .ReleaseSafe; var exit_lock: std.Io.Mutex = .init; var io: Io = undefined; fn handleSigInt(sig: std.os.linux.SIG) callconv(.c) void { _ = sig; exit_lock.unlock(io); } pub fn main(alloc: Allocator, outer_io: Io, args: []const [:0]const u8) !void { io = outer_io; var server_config: ServerInfo = .{ .server_id = Server.default_id, .server_name = Server.default_name, .version = "zits-master", .max_payload = 1048576, .headers = true, }; { var i: usize = 0; while (i < args.len) : (i += 1) { switch (to_flag.get(args[i]) orelse .help) { .help => { try std.Io.File.stdout().writeStreamingAll(io, help); return; }, .port => { i += 1; if (args.len > i) { server_config.port = std.fmt.parseUnsigned(u16, args[i], 10) catch { std.log.err("Could not parse port: {s}", .{args[i]}); return; }; } else { std.log.err("Must specify port with {s}", .{args[i - 1]}); return; } }, } } } try exit_lock.lock(io); // Configure the signal action const act = Sigaction{ .handler = .{ .handler = handleSigInt }, .mask = std.posix.sigemptyset(), .flags = 0, }; // Register the handler for SIGINT (Ctrl+C) std.posix.sigaction(std.posix.SIG.INT, &act, null); var server: Server = .{ .info = server_config, }; defer server.deinit(io, alloc); var server_task = try io.concurrent(Server.start, .{ &server, io, alloc }); defer server_task.cancel(io) catch {}; // Block until Ctrl+C try exit_lock.lock(io); std.debug.print("\n", .{}); std.log.info("Shutting down...", .{}); server_task.cancel(io) catch |err| switch (err) { error.Canceled => {}, else => |e| std.log.err("Error shutting down: {t}", .{e}), }; std.log.info("Goodbye", .{}); } const help = "serve help\n"; const to_flag: std.StaticStringMap(Flag) = .initComptime(.{ .{ "-p", .port }, .{ "--port", .port }, .{ "-h", .help }, .{ "--help", .help }, }); const Flag = enum { port, help, };