From 3a837e2c1f0df58d5d01cbd732378fbd66e1fc21 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 18 Dec 2025 13:54:51 -0800 Subject: [PATCH] prefer getEnvMap to multiple calls to getEnvOwned --- src/config.zig | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/config.zig b/src/config.zig index 658e4aa..4b0fc18 100644 --- a/src/config.zig +++ b/src/config.zig @@ -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, }; }