diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-05-08 15:11:51 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-05-08 15:11:51 +0500 |
| commit | 41aad458b1876d37e52274b2921739928d7bf83c (patch) | |
| tree | 755462c88cb0cad5550adebba81159c6e533e2d2 /backend/src/Settings.zig | |
| parent | 877973ea93cbe917e835c5a5d9880f9e6922ab47 (diff) | |
Diffstat (limited to 'backend/src/Settings.zig')
| -rw-r--r-- | backend/src/Settings.zig | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/backend/src/Settings.zig b/backend/src/Settings.zig new file mode 100644 index 0000000..142a662 --- /dev/null +++ b/backend/src/Settings.zig @@ -0,0 +1,78 @@ +const std = @import("std"); + +const SettingsFile = "Settings.json"; + +pub const errors = error { + Uninitialized +}; + +pub const SettingsStruct = struct { + Port: u16, + + CorsParams: struct { + Origin: []const u8, + Methods: []const u8, + Headers: []const u8, + }, + + RedisParams: struct { + Address: []const u8, + Port: u16, + }, + + DbParams: struct { + Address: []const u8, + Port: u16, + }, +}; + +pub var Current: *SettingsStruct = undefined; + +pub fn ReadConfig(allocator: std.mem.Allocator) !void { + const file = try std.fs.cwd().openFile(SettingsFile, .{}); + defer file.close(); + + const contents = try file.readToEndAlloc(allocator, std.math.maxInt(usize)); + defer allocator.free(contents); + + var parsed = std.json.parseFromSlice( + SettingsStruct, + allocator, + contents, .{}) catch |err| { + std.debug.print("Failed to parse settings: {any}\n", .{err}); + return err; + }; + + defer parsed.deinit(); + + // There is no reason to free this value, it will be freed by OS at the end of the application + Current = try allocator.create(SettingsStruct); + Current.* = .{ + .Port = parsed.value.Port, + + .CorsParams = .{ + .Origin = try allocator.dupe(u8, parsed.value.CorsParams.Origin), + .Methods = try allocator.dupe(u8, parsed.value.CorsParams.Methods), + .Headers = try allocator.dupe(u8, parsed.value.CorsParams.Headers), + }, + + .RedisParams = .{ + .Address = try allocator.dupe(u8, parsed.value.RedisParams.Address), + .Port = parsed.value.RedisParams.Port, + }, + + .DbParams = .{ + .Address = try allocator.dupe(u8, parsed.value.DbParams.Address), + .Port = parsed.value.DbParams.Port, + }, + }; +} + +pub fn Deinit(allocator: std.mem.Allocator) void { + allocator.free(Current.CorsParams.Origin); + allocator.free(Current.CorsParams.Methods); + allocator.free(Current.CorsParams.Headers); + allocator.free(Current.RedisParams.Address); + allocator.free(Current.DbParams.Address); + allocator.destroy(Current); +} |
