Spaces:
Building
Building
Create project_controller.py
Browse files- project_controller.py +61 -0
project_controller.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Request, HTTPException
|
2 |
+
from service_config import ServiceConfig
|
3 |
+
|
4 |
+
router = APIRouter()
|
5 |
+
service_config = ServiceConfig()
|
6 |
+
service_config.load()
|
7 |
+
|
8 |
+
@router.get("/list")
|
9 |
+
def list_projects():
|
10 |
+
return service_config.projects
|
11 |
+
|
12 |
+
@router.post("/add")
|
13 |
+
async def add_project(request: Request):
|
14 |
+
data = await request.json()
|
15 |
+
project_name = data.get("project_name")
|
16 |
+
if project_name in service_config.projects:
|
17 |
+
raise HTTPException(status_code=400, detail="Project already exists")
|
18 |
+
service_config.projects[project_name] = {
|
19 |
+
"enabled": False,
|
20 |
+
"versions": []
|
21 |
+
}
|
22 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
23 |
+
import json
|
24 |
+
json.dump(service_config, f, indent=2)
|
25 |
+
return {"message": f"Project {project_name} added"}
|
26 |
+
|
27 |
+
@router.post("/enable")
|
28 |
+
async def enable_project(request: Request):
|
29 |
+
data = await request.json()
|
30 |
+
project_name = data.get("project_name")
|
31 |
+
if project_name not in service_config.projects:
|
32 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
33 |
+
service_config.projects[project_name]["enabled"] = True
|
34 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
35 |
+
import json
|
36 |
+
json.dump(service_config, f, indent=2)
|
37 |
+
return {"message": f"Project {project_name} enabled"}
|
38 |
+
|
39 |
+
@router.post("/disable")
|
40 |
+
async def disable_project(request: Request):
|
41 |
+
data = await request.json()
|
42 |
+
project_name = data.get("project_name")
|
43 |
+
if project_name not in service_config.projects:
|
44 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
45 |
+
service_config.projects[project_name]["enabled"] = False
|
46 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
47 |
+
import json
|
48 |
+
json.dump(service_config, f, indent=2)
|
49 |
+
return {"message": f"Project {project_name} disabled"}
|
50 |
+
|
51 |
+
@router.delete("/delete")
|
52 |
+
async def delete_project(request: Request):
|
53 |
+
data = await request.json()
|
54 |
+
project_name = data.get("project_name")
|
55 |
+
if project_name not in service_config.projects:
|
56 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
57 |
+
del service_config.projects[project_name]
|
58 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
59 |
+
import json
|
60 |
+
json.dump(service_config, f, indent=2)
|
61 |
+
return {"message": f"Project {project_name} deleted"}
|