ciyidogan commited on
Commit
b37555b
·
verified ·
1 Parent(s): 09f6c98

Update config_controller.py

Browse files
Files changed (1) hide show
  1. config_controller.py +16 -19
config_controller.py CHANGED
@@ -1,36 +1,33 @@
1
- from fastapi import APIRouter, Request, HTTPException
2
- from service_config import ServiceConfig
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": service_config.work_mode,
13
- "cloud_token": service_config.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 work_mode or not spark_endpoint:
26
- raise HTTPException(status_code=400, detail="work_mode and spark_endpoint cannot be empty")
27
 
28
- service_config.work_mode = work_mode
29
- service_config.cloud_token = cloud_token
30
- service_config.spark_endpoint = spark_endpoint
31
 
32
- with open(service_config.config_path, "w", encoding="utf-8") as f:
33
- json.dump(service_config, f, indent=2)
34
 
35
- return {"message": "Configuration updated successfully"}
 
 
 
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