|
1 | 1 | const std = @import("std"); |
2 | 2 | const Store = @import("store.zig").Store; |
3 | 3 | const api = @import("api.zig"); |
| 4 | +const config = @import("config.zig"); |
4 | 5 |
|
5 | 6 | const version = "0.1.0"; |
6 | 7 |
|
@@ -35,27 +36,49 @@ pub fn main() !void { |
35 | 36 | defer args2.deinit(); |
36 | 37 | _ = args2.next(); // skip program name |
37 | 38 |
|
38 | | - var port: u16 = 7700; |
39 | | - var db_path: [:0]const u8 = "nulltickets.db"; |
| 39 | + var port_override: ?u16 = null; |
| 40 | + var db_override: ?[:0]const u8 = null; |
| 41 | + var config_path: []const u8 = "config.json"; |
40 | 42 |
|
41 | 43 | while (args2.next()) |arg| { |
42 | 44 | if (std.mem.eql(u8, arg, "--port")) { |
43 | 45 | if (args2.next()) |val| { |
44 | | - port = std.fmt.parseInt(u16, val, 10) catch { |
| 46 | + port_override = std.fmt.parseInt(u16, val, 10) catch { |
45 | 47 | std.debug.print("invalid port: {s}\n", .{val}); |
46 | 48 | return; |
47 | 49 | }; |
48 | 50 | } |
49 | 51 | } else if (std.mem.eql(u8, arg, "--db")) { |
50 | 52 | if (args2.next()) |val| { |
51 | | - db_path = val; |
| 53 | + db_override = val; |
| 54 | + } |
| 55 | + } else if (std.mem.eql(u8, arg, "--config")) { |
| 56 | + if (args2.next()) |val| { |
| 57 | + config_path = val; |
52 | 58 | } |
53 | 59 | } else if (std.mem.eql(u8, arg, "--version")) { |
54 | 60 | std.debug.print("nulltickets v{s}\n", .{version}); |
55 | 61 | return; |
56 | 62 | } |
57 | 63 | } |
58 | 64 |
|
| 65 | + var cfg_arena = std.heap.ArenaAllocator.init(allocator); |
| 66 | + defer cfg_arena.deinit(); |
| 67 | + const cfg = config.loadFromFile(cfg_arena.allocator(), config_path) catch |err| { |
| 68 | + std.debug.print("failed to load config from {s}: {}\n", .{ config_path, err }); |
| 69 | + return; |
| 70 | + }; |
| 71 | + |
| 72 | + const port = port_override orelse cfg.port; |
| 73 | + const db_path: [:0]const u8 = db_override orelse blk: { |
| 74 | + const db_z = cfg_arena.allocator().allocSentinel(u8, cfg.db.len, 0) catch { |
| 75 | + std.debug.print("out of memory\n", .{}); |
| 76 | + return; |
| 77 | + }; |
| 78 | + @memcpy(db_z, cfg.db); |
| 79 | + break :blk db_z; |
| 80 | + }; |
| 81 | + |
59 | 82 | std.debug.print("nulltickets v{s}\n", .{version}); |
60 | 83 | std.debug.print("opening database: {s}\n", .{db_path}); |
61 | 84 |
|
|
0 commit comments