Add authorization and runtime settings services
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import json
|
||||
import os
|
||||
from config import SETTINGS_FILE
|
||||
from services import ai_client, auth
|
||||
from utils import context
|
||||
|
||||
_loading = False
|
||||
|
||||
|
||||
def load():
|
||||
global _loading
|
||||
if not os.path.exists(SETTINGS_FILE):
|
||||
save()
|
||||
return
|
||||
|
||||
_loading = True
|
||||
try:
|
||||
with open(SETTINGS_FILE, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
if isinstance(data, dict):
|
||||
ai_client.import_provider_settings(data.get("ai", {}))
|
||||
context.import_settings(data.get("context", {}))
|
||||
auth.set_public(bool(data.get("public_mode", False)))
|
||||
finally:
|
||||
_loading = False
|
||||
|
||||
|
||||
def save():
|
||||
if _loading:
|
||||
return
|
||||
|
||||
data = {
|
||||
"ai": ai_client.export_provider_settings(),
|
||||
"public_mode": auth.is_public(),
|
||||
"context": context.export_settings(),
|
||||
}
|
||||
tmp = f"{SETTINGS_FILE}.tmp"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
f.write("\n")
|
||||
os.replace(tmp, SETTINGS_FILE)
|
||||
Reference in New Issue
Block a user