ciyidogan commited on
Commit
c836e99
Β·
verified Β·
1 Parent(s): 55b48d2

Update controllers/config_controller.py

Browse files
Files changed (1) hide show
  1. controllers/config_controller.py +88 -18
controllers/config_controller.py CHANGED
@@ -1,18 +1,88 @@
1
- from fastapi import APIRouter
2
- import threading
3
- from service_config import ServiceConfig
4
- from log import log
5
-
6
- router = APIRouter()
7
- service_config = ServiceConfig()
8
-
9
- @router.post("/reload")
10
- def reload_config():
11
- def background_reload():
12
- try:
13
- service_config.load()
14
- log("βœ… Service config reloaded successfully.")
15
- except Exception as e:
16
- log(f"❌ Error reloading config: {e}")
17
- threading.Thread(target=background_reload, daemon=True).start()
18
- return {"status": "accepted", "message": "Config reload started in background."}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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."}