-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconfig.zig
More file actions
61 lines (54 loc) · 2.02 KB
/
config.zig
File metadata and controls
61 lines (54 loc) · 2.02 KB
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
const std = @import("std");
pub const Config = struct {
host: []const u8,
port: u16,
cdp_url: ?[]const u8,
auth_secret: ?[]const u8,
state_dir: []const u8,
stale_tab_interval_s: u32,
request_timeout_ms: u32,
navigate_timeout_ms: u32,
extensions: ?[]const u8,
headless: bool,
};
pub fn load() Config {
return .{
.host = std.posix.getenv("HOST") orelse "127.0.0.1",
.port = parsePort() orelse 8080,
.cdp_url = std.posix.getenv("CDP_URL"),
.auth_secret = getenvAny(&.{ "KURI_SECRET", "BROWDIE_SECRET" }),
.state_dir = getenvAny(&.{ "STATE_DIR" }) orelse ".kuri",
.stale_tab_interval_s = parseU32("STALE_TAB_INTERVAL_S") orelse 30,
.request_timeout_ms = parseU32("REQUEST_TIMEOUT_MS") orelse 30_000,
.navigate_timeout_ms = parseU32("NAVIGATE_TIMEOUT_MS") orelse 30_000,
.extensions = getenvAny(&.{ "KURI_EXTENSIONS", "BROWDIE_EXTENSIONS" }),
.headless = parseBool("HEADLESS") orelse true,
};
}
fn getenvAny(names: []const []const u8) ?[]const u8 {
for (names) |name| {
if (std.posix.getenv(name)) |value| return value;
}
return null;
}
fn parsePort() ?u16 {
const val = std.posix.getenv("PORT") orelse return null;
return std.fmt.parseInt(u16, val, 10) catch null;
}
fn parseU32(name: []const u8) ?u32 {
const val = std.posix.getenv(name) orelse return null;
return std.fmt.parseInt(u32, val, 10) catch null;
}
fn parseBool(name: []const u8) ?bool {
const val = std.posix.getenv(name) orelse return null;
if (std.mem.eql(u8, val, "false") or std.mem.eql(u8, val, "0")) return false;
return true;
}
test "load returns defaults" {
const cfg = load();
try std.testing.expectEqualStrings("127.0.0.1", cfg.host);
try std.testing.expectEqual(@as(u16, 8080), cfg.port);
try std.testing.expectEqual(@as(?[]const u8, null), cfg.cdp_url);
try std.testing.expectEqual(@as(u32, 30), cfg.stale_tab_interval_s);
try std.testing.expectEqual(@as(?[]const u8, null), cfg.extensions);
}