aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-11-24 17:43:49 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2025-11-24 17:43:49 +0100
commitf2cd03729059206fe584e2a9f278dd67625a9221 (patch)
tree6dbe280f0b8f24e32e81c7ff96dd5f1ccc63909e /src
parentb2323081234b8635681062d7737daabd7ab837a9 (diff)
add config
Diffstat (limited to 'src')
-rw-r--r--src/config.zig41
-rw-r--r--src/storage/root.zig12
-rw-r--r--src/storage/session-manager/root.zig2
3 files changed, 50 insertions, 5 deletions
diff --git a/src/config.zig b/src/config.zig
index 687cd25..02d3c8a 100644
--- a/src/config.zig
+++ b/src/config.zig
@@ -1,4 +1,41 @@
const std = @import("std");
-pub const storage_path = "./storage/";
-pub const session_expires_after = std.time.ns_per_week * 4;
+const Self = @This();
+
+const Config = struct {
+ storage_path: []const u8,
+ session_expires_after: usize,
+};
+
+arena: *std.heap.ArenaAllocator,
+storage_path: []const u8,
+session_expires_after: usize,
+
+pub fn load(allocator: std.mem.Allocator) !Self {
+ const home_path = std.posix.getenv("HOME") orelse return error.MissingHome;
+ const home = try std.fs.cwd().openDirZ(
+ home_path,
+ .{},
+ );
+ const file = home.openFile(
+ ".config/memora/config.json",
+ .{}
+ ) catch return error.MissingConfig;
+
+ const content = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
+ defer allocator.free(content);
+
+ const parsed = try std.json.parseFromSlice(Config, allocator, content, .{
+ .allocate = .alloc_always,
+ });
+ return .{
+ .arena = parsed.arena,
+ .storage_path = parsed.value.storage_path,
+ .session_expires_after = parsed.value.session_expires_after * std.time.ns_per_day,
+ };
+}
+
+pub fn deinit(self: *Self) void {
+ self.arena.deinit();
+ self.* = undefined;
+}
diff --git a/src/storage/root.zig b/src/storage/root.zig
index 381dc45..f11798b 100644
--- a/src/storage/root.zig
+++ b/src/storage/root.zig
@@ -1,6 +1,6 @@
const std = @import("std");
-const config = @import("../config.zig");
const prompt = @import("../prompt.zig");
+const Config = @import("../config.zig");
pub const User = @import("user.zig");
@@ -15,15 +15,22 @@ const Self = @This();
dir: std.fs.Dir,
sessions: SessionManager = .empty,
images: ImageManager = .empty,
+config: Config,
allocator: std.mem.Allocator,
-pub fn init(allocator: std.mem.Allocator) !Self {
+pub fn init(
+ allocator: std.mem.Allocator,
+) !Self {
+ var config = try Config.load(allocator);
+ errdefer config.deinit();
+
const dir = std.fs.cwd().openDir(config.storage_path, .{}) catch blk: {
try std.fs.cwd().makeDir(config.storage_path);
break :blk try std.fs.cwd().openDir(config.storage_path, .{});
};
var self = Self {
+ .config = config,
.dir = dir,
.allocator = allocator,
};
@@ -67,5 +74,6 @@ pub fn init(allocator: std.mem.Allocator) !Self {
pub fn deinit(self: *Self) void {
self.dir.close();
+ self.config.deinit();
self.* = undefined;
}
diff --git a/src/storage/session-manager/root.zig b/src/storage/session-manager/root.zig
index d0e1441..8a59f21 100644
--- a/src/storage/session-manager/root.zig
+++ b/src/storage/session-manager/root.zig
@@ -65,7 +65,7 @@ pub fn get(self: *Self, storage: *Storage, fingerprint: []const u8) ?*Session {
const now = std.time.Instant.now() catch return null;
const since = now.since(session.age);
- if (since > config.session_expires_after) {
+ if (since > storage.config.session_expires_after) {
session.deinit(storage.allocator);
_ = self.cache.remove(fingerprint);
return null;