Update configuration handling: refine path resolution and allocator usage in main.zig
This commit is contained in:
@@ -19,7 +19,7 @@ test "parse zocket config"
|
||||
|
||||
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();
|
||||
|
||||
const cfg = parsed.value;
|
||||
|
||||
33
src/main.zig
33
src/main.zig
@@ -9,22 +9,19 @@ const args = zocket.deps.args;
|
||||
|
||||
pub fn main(init: std.process.Init) !void
|
||||
{
|
||||
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;
|
||||
defer _ = arena.deinit();
|
||||
if (gpa.detectLeaks()) std.posix.exit(1);
|
||||
};
|
||||
|
||||
try run(io, arena.allocator(), "config/config.toml");
|
||||
}
|
||||
else
|
||||
{
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
const allocator = if (debug) gpa.allocator() else std.heap.c_allocator;
|
||||
|
||||
var tmpArena = std.heap.ArenaAllocator.init(std.heap.smp_allocator);
|
||||
var tmpArena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
|
||||
const tmpAllocator = tmpArena.allocator();
|
||||
|
||||
var parser = try args.ArgumentParser.init(tmpAllocator, .{
|
||||
@@ -33,6 +30,8 @@ pub fn main(init: std.process.Init) !void
|
||||
.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)",
|
||||
@@ -40,17 +39,23 @@ pub fn main(init: std.process.Init) !void
|
||||
|
||||
var result = try parser.parseProcess(init);
|
||||
|
||||
if (debug)
|
||||
{
|
||||
const tmpPath = result.getString("path") orelse "config/config.toml";
|
||||
path = try allocator.dupe(u8, tmpPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
const tmpPath = result.getString("path") orelse blk: {
|
||||
const exe = try std.process.executableDirPathAlloc(io, tmpAllocator);
|
||||
break :blk try std.Io.Dir.path.join(tmpAllocator, &.{ exe, "config/config.toml" });
|
||||
};
|
||||
|
||||
const path = try allocator.dupe(u8, tmpPath);
|
||||
path = try allocator.dupe(u8, tmpPath);
|
||||
}
|
||||
|
||||
tmpArena.deinit();
|
||||
|
||||
try run(io, allocator, path);
|
||||
}
|
||||
}
|
||||
|
||||
fn run(io: std.Io, allocator: std.mem.Allocator, path: []const u8) !void
|
||||
|
||||
Reference in New Issue
Block a user