Update configuration handling: refine path resolution and allocator usage in main.zig

This commit is contained in:
2026-08-02 18:01:28 +02:00
parent 250829e922
commit 84c4158aaa
2 changed files with 37 additions and 32 deletions

View File

@@ -19,7 +19,7 @@ test "parse zocket config"
const allocator = std.testing.allocator; const allocator = std.testing.allocator;
const parsed = try parse(io, allocator, "zocket/config.toml"); const parsed = try parse(io, allocator, "config/config.toml");
defer parsed.deinit(); defer parsed.deinit();
const cfg = parsed.value; const cfg = parsed.value;

View File

@@ -9,48 +9,53 @@ const args = zocket.deps.args;
pub fn main(init: std.process.Init) !void pub fn main(init: std.process.Init) !void
{ {
const io = init.io; const debug = builtin.mode == .Debug;
const io = init.io;
if (builtin.mode == .Debug) var gpa: std.heap.DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
defer if (debug)
{ {
var arena: std.heap.DebugAllocator(.{}) = .init; if (gpa.detectLeaks()) std.posix.exit(1);
defer _ = arena.deinit(); };
try run(io, arena.allocator(), "config/config.toml"); const allocator = if (debug) gpa.allocator() else std.heap.c_allocator;
var tmpArena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
const tmpAllocator = tmpArena.allocator();
var parser = try args.ArgumentParser.init(tmpAllocator, .{
.name = "zocket",
.version = options.version,
.description = "A lightweight Zig WebSocket server and mesh routing daemon.",
});
var path: []const u8 = undefined;
try parser.addOption("path", .{
.short = 'p',
.help = "Configuration-File Path (*.toml)",
});
var result = try parser.parseProcess(init);
if (debug)
{
const tmpPath = result.getString("path") orelse "config/config.toml";
path = try allocator.dupe(u8, tmpPath);
} }
else else
{ {
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var tmpArena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
const tmpAllocator = tmpArena.allocator();
var parser = try args.ArgumentParser.init(tmpAllocator, .{
.name = "zocket",
.version = options.version,
.description = "A lightweight Zig WebSocket server and mesh routing daemon.",
});
try parser.addOption("path", .{
.short = 'p',
.help = "Configuration-File Path (*.toml)",
});
var result = try parser.parseProcess(init);
const tmpPath = result.getString("path") orelse blk: { const tmpPath = result.getString("path") orelse blk: {
const exe = try std.process.executableDirPathAlloc(io, tmpAllocator); const exe = try std.process.executableDirPathAlloc(io, tmpAllocator);
break :blk try std.Io.Dir.path.join(tmpAllocator, &.{ exe, "config/config.toml" }); break :blk try std.Io.Dir.path.join(tmpAllocator, &.{ exe, "config/config.toml" });
}; };
path = try allocator.dupe(u8, tmpPath);
const path = try allocator.dupe(u8, tmpPath);
tmpArena.deinit();
try run(io, allocator, path);
} }
tmpArena.deinit();
try run(io, allocator, path);
} }
fn run(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void fn run(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void