prefer getEnvMap to multiple calls to getEnvOwned

This commit is contained in:
Emil Lerch 2025-12-18 13:54:51 -08:00
parent 703e77c19d
commit 3a837e2c1f
Signed by: lobo
GPG key ID: A7B62D657EF764F8

View file

@ -10,22 +10,22 @@ pub const Config = struct {
airports_dat_path: ?[]const u8,
pub fn load(allocator: std.mem.Allocator) !Config {
var env = try std.process.getEnvMap(allocator);
defer env.deinit();
return Config{
.listen_host = std.process.getEnvVarOwned(allocator, "WTTR_LISTEN_HOST") catch try allocator.dupe(u8, "0.0.0.0"),
.listen_host = env.get("WTTR_LISTEN_HOST") orelse try allocator.dupe(u8, "0.0.0.0"),
.listen_port = blk: {
const port_str = std.process.getEnvVarOwned(allocator, "WTTR_LISTEN_PORT") catch break :blk 8002;
defer allocator.free(port_str);
const port_str = env.get("WTTR_LISTEN_PORT") orelse break :blk 8002;
break :blk std.fmt.parseInt(u16, port_str, 10) catch 8002;
},
.cache_size = blk: {
const size_str = std.process.getEnvVarOwned(allocator, "WTTR_CACHE_SIZE") catch break :blk 10_000;
defer allocator.free(size_str);
const size_str = env.get("WTTR_CACHE_SIZE") orelse break :blk 10_000;
break :blk std.fmt.parseInt(usize, size_str, 10) catch 10_000;
},
.cache_dir = std.process.getEnvVarOwned(allocator, "WTTR_CACHE_DIR") catch try allocator.dupe(u8, "/tmp/wttr-cache"),
.geolite_path = std.process.getEnvVarOwned(allocator, "WTTR_GEOLITE_PATH") catch try allocator.dupe(u8, "./GeoLite2-City.mmdb"),
.geocache_file = std.process.getEnvVarOwned(allocator, "WTTR_GEOCACHE_FILE") catch null,
.airports_dat_path = std.process.getEnvVarOwned(allocator, "WTTR_AIRPORTS_DAT") catch null,
.cache_dir = if (env.get("WTTR_CACHE_DIR")) |v| try allocator.dupe(u8, v) else try allocator.dupe(u8, "/tmp/wttr-cache"),
.geolite_path = if (env.get("WTTR_GEOLITE_PATH")) |v| try allocator.dupe(u8, v) else try allocator.dupe(u8, "./GeoLite2-City.mmdb"),
.geocache_file = if (env.get("WTTR_GEOCACHE_FILE")) |v| try allocator.dupe(u8, v) else null,
.airports_dat_path = if (env.get("WTTR_AIRPORTS_DAT")) |v| try allocator.dupe(u8, v) else null,
};
}