Spaces:
Building
Building
Update config_controller.py
Browse files- config_controller.py +16 -19
config_controller.py
CHANGED
@@ -1,36 +1,33 @@
|
|
1 |
-
from fastapi import APIRouter, Request,
|
2 |
-
from
|
3 |
import json
|
4 |
|
5 |
router = APIRouter()
|
6 |
-
service_config = ServiceConfig()
|
7 |
-
service_config.load()
|
8 |
|
9 |
@router.get("/get")
|
10 |
-
def get_config
|
11 |
return {
|
12 |
-
"work_mode":
|
13 |
-
"cloud_token":
|
14 |
-
"spark_endpoint": service_config.spark_endpoint,
|
15 |
-
"last_version_number": service_config.last_version_number
|
16 |
}
|
17 |
|
18 |
@router.post("/update")
|
19 |
-
async def update_config(request: Request):
|
20 |
data = await request.json()
|
21 |
work_mode = data.get("work_mode")
|
22 |
cloud_token = data.get("cloud_token")
|
23 |
-
spark_endpoint = data.get("spark_endpoint")
|
24 |
|
25 |
-
if not
|
26 |
-
raise HTTPException(status_code=400, detail="
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
service_config.spark_endpoint = spark_endpoint
|
31 |
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
|
|
|
|
|
|
36 |
|
|
|
1 |
+
from fastapi import APIRouter, HTTPException, Request, Depends
|
2 |
+
from app import get_config, ServiceConfig
|
3 |
import json
|
4 |
|
5 |
router = APIRouter()
|
|
|
|
|
6 |
|
7 |
@router.get("/get")
|
8 |
+
def get_config_values(config: ServiceConfig = Depends(get_config)):
|
9 |
return {
|
10 |
+
"work_mode": config.work_mode,
|
11 |
+
"cloud_token": config.cloud_token
|
|
|
|
|
12 |
}
|
13 |
|
14 |
@router.post("/update")
|
15 |
+
async def update_config(request: Request, config: ServiceConfig = Depends(get_config)):
|
16 |
data = await request.json()
|
17 |
work_mode = data.get("work_mode")
|
18 |
cloud_token = data.get("cloud_token")
|
|
|
19 |
|
20 |
+
if work_mode not in ["hfcloud", "cloud", "on-premise"]:
|
21 |
+
raise HTTPException(status_code=400, detail="Invalid work mode")
|
22 |
|
23 |
+
if (work_mode in ["hfcloud", "cloud"]) and not cloud_token:
|
24 |
+
raise HTTPException(status_code=400, detail="Cloud token required for selected mode")
|
|
|
25 |
|
26 |
+
config.work_mode = work_mode
|
27 |
+
config.cloud_token = cloud_token
|
28 |
|
29 |
+
with open(config.config_path, "w", encoding="utf-8") as f:
|
30 |
+
json.dump(config, f, indent=2)
|
31 |
+
|
32 |
+
return {"message": "Configuration updated"}
|
33 |
|