浏览代码

Update config.py

Timothy J. Baek 8 月之前
父节点
当前提交
9bcbf5e9b3
共有 1 个文件被更改,包括 28 次插入10 次删除
  1. 28 10
      backend/config.py

+ 28 - 10
backend/config.py

@@ -47,7 +47,7 @@ def run_migrations():
         from alembic import command
         from alembic.config import Config
 
-        alembic_cfg = Config(BACKEND_DIR / "alembic.ini")
+        alembic_cfg = Config("alembic.ini")
         command.upgrade(alembic_cfg, "head")
     except Exception as e:
         print(f"Error: {e}")
@@ -90,15 +90,6 @@ if os.path.exists(f"{DATA_DIR}/config.json"):
     save_to_db(data)
     os.rename(f"{DATA_DIR}/config.json", f"{DATA_DIR}/old_config.json")
 
-
-def save_config():
-    try:
-        with open(f"{DATA_DIR}/config.json", "w") as f:
-            json.dump(CONFIG_DATA, f, indent="\t")
-    except Exception as e:
-        log.exception(e)
-
-
 DEFAULT_CONFIG = {
     "version": 0,
     "ui": {
@@ -172,6 +163,25 @@ def get_config_value(config_path: str):
     return cur_config
 
 
+PERSISTENT_CONFIG_REGISTRY = []
+
+
+def save_config(config):
+    global CONFIG_DATA
+    global PERSISTENT_CONFIG_REGISTRY
+    try:
+        save_to_db(config)
+        CONFIG_DATA = config
+
+        # Trigger updates on all registered PersistentConfig entries
+        for config_item in PERSISTENT_CONFIG_REGISTRY:
+            config_item.update()
+    except Exception as e:
+        log.exception(e)
+        return False
+    return True
+
+
 T = TypeVar("T")
 
 
@@ -187,6 +197,8 @@ class PersistentConfig(Generic[T]):
         else:
             self.value = env_value
 
+        PERSISTENT_CONFIG_REGISTRY.append(self)
+
     def __str__(self):
         return str(self.value)
 
@@ -203,6 +215,12 @@ class PersistentConfig(Generic[T]):
             )
         return super().__getattribute__(item)
 
+    def update(self):
+        new_value = get_config_value(self.config_path)
+        if new_value is not None:
+            self.value = new_value
+            log.info(f"Updated {self.env_name} to new value {self.value}")
+
     def save(self):
         log.info(f"Saving '{self.env_name}' to the database")
         path_parts = self.config_path.split(".")