Spaces:
Paused
Paused
Update controllers/config_controller.py
Browse files- controllers/config_controller.py +88 -18
controllers/config_controller.py
CHANGED
@@ -1,18 +1,88 @@
|
|
1 |
-
from fastapi import APIRouter
|
2 |
-
import threading
|
3 |
-
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Request
|
2 |
+
import threading
|
3 |
+
import json
|
4 |
+
from service_config import ServiceConfig
|
5 |
+
from log import log
|
6 |
+
|
7 |
+
router = APIRouter()
|
8 |
+
service_config = ServiceConfig()
|
9 |
+
service_config.load()
|
10 |
+
|
11 |
+
def save_config():
|
12 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
13 |
+
json.dump({
|
14 |
+
"config": {
|
15 |
+
"work_mode": service_config.work_mode,
|
16 |
+
"cloud_token": service_config.cloud_token,
|
17 |
+
"system_prompt": service_config.system_prompt,
|
18 |
+
"llm_inference_service_url": service_config.llm_inference_service_url
|
19 |
+
},
|
20 |
+
"projects": service_config.projects,
|
21 |
+
"apis": service_config.apis
|
22 |
+
}, f, indent=2)
|
23 |
+
|
24 |
+
@router.post("/reload")
|
25 |
+
def reload_config():
|
26 |
+
def background_reload():
|
27 |
+
try:
|
28 |
+
service_config.load()
|
29 |
+
log("β
Service config reloaded successfully.")
|
30 |
+
except Exception as e:
|
31 |
+
log(f"β Error reloading config: {e}")
|
32 |
+
threading.Thread(target=background_reload, daemon=True).start()
|
33 |
+
return {"status": "accepted", "message": "Config reload started in background."}
|
34 |
+
|
35 |
+
# === API CRUD ===
|
36 |
+
|
37 |
+
@router.get("/list_apis")
|
38 |
+
def list_apis():
|
39 |
+
api_names = list(service_config.apis.keys())
|
40 |
+
log("π Listing APIs.")
|
41 |
+
return {"apis": api_names}
|
42 |
+
|
43 |
+
@router.post("/add_api")
|
44 |
+
async def add_api(request: Request):
|
45 |
+
data = await request.json()
|
46 |
+
api_name = data.get("api_name")
|
47 |
+
api_config = data.get("api_config")
|
48 |
+
if not api_name or not api_config:
|
49 |
+
return {"error": "API name and config required."}
|
50 |
+
|
51 |
+
if api_name in service_config.apis:
|
52 |
+
return {"error": "API already exists."}
|
53 |
+
|
54 |
+
service_config.apis[api_name] = api_config
|
55 |
+
save_config()
|
56 |
+
log(f"π Added API: {api_name}")
|
57 |
+
return {"message": f"API {api_name} added."}
|
58 |
+
|
59 |
+
@router.post("/delete_api")
|
60 |
+
async def delete_api(request: Request):
|
61 |
+
data = await request.json()
|
62 |
+
api_name = data.get("api_name")
|
63 |
+
if not api_name:
|
64 |
+
return {"error": "API name required."}
|
65 |
+
|
66 |
+
if api_name not in service_config.apis:
|
67 |
+
return {"error": "API not found."}
|
68 |
+
|
69 |
+
del service_config.apis[api_name]
|
70 |
+
save_config()
|
71 |
+
log(f"ποΈ Deleted API: {api_name}")
|
72 |
+
return {"message": f"API {api_name} deleted."}
|
73 |
+
|
74 |
+
@router.post("/update_api")
|
75 |
+
async def update_api(request: Request):
|
76 |
+
data = await request.json()
|
77 |
+
api_name = data.get("api_name")
|
78 |
+
api_config = data.get("api_config")
|
79 |
+
if not api_name or not api_config:
|
80 |
+
return {"error": "API name and updated config required."}
|
81 |
+
|
82 |
+
if api_name not in service_config.apis:
|
83 |
+
return {"error": "API not found."}
|
84 |
+
|
85 |
+
service_config.apis[api_name] = api_config
|
86 |
+
save_config()
|
87 |
+
log(f"βοΈ Updated API: {api_name}")
|
88 |
+
return {"message": f"API {api_name} updated."}
|