summaryrefslogtreecommitdiff
path: root/src/subcommand/serve.zig
blob: e6bb64892b56847d9ba340f1df73e61cf33724f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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,
};