flare / config_controller.py
ciyidogan's picture
Update config_controller.py
8d3e422 verified
raw
history blame
1.12 kB
from fastapi import APIRouter, HTTPException, Request, Depends
from config_provider import get_config, service_config
from service_config import ServiceConfig
import json
router = APIRouter()
@router.get("/get")
def get_config_values(config: ServiceConfig = Depends(get_config)):
return {
"work_mode": config.work_mode,
"cloud_token": config.cloud_token
}
@router.post("/update")
async def update_config(request: Request, config: ServiceConfig = Depends(get_config)):
data = await request.json()
work_mode = data.get("work_mode")
cloud_token = data.get("cloud_token")
if work_mode not in ["hfcloud", "cloud", "on-premise"]:
raise HTTPException(status_code=400, detail="Invalid work mode")
if (work_mode in ["hfcloud", "cloud"]) and not cloud_token:
raise HTTPException(status_code=400, detail="Cloud token required for selected mode")
config.work_mode = work_mode
config.cloud_token = cloud_token
with open(config.config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
return {"message": "Configuration updated"}