File size: 2,807 Bytes
d477010
db7a7ca
d477010
 
926928e
 
 
 
d477010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926928e
 
d477010
926928e
d477010
 
 
 
 
 
 
 
 
 
 
 
 
 
926928e
 
d477010
926928e
d477010
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926928e
d477010
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from fastapi import APIRouter, HTTPException, Depends, Request
from config_provider import get_config, service_config
import random
import json

router = APIRouter()

@router.get("/project_list")
def get_spark_project_list(config: ServiceConfig = Depends(get_config)):
    if not config.projects:
        raise HTTPException(status_code=404, detail="No projects found")

    project_list = []
    for project_name, project_data in config.projects.items():
        latest_version = max(project_data["versions"], key=lambda v: v["version_number"])
        project_list.append({
            "project_name": project_name,
            "version": latest_version["version_number"],
            "enabled": project_data.get("enabled", False),
            "status": random.choice(["loading", "ready", "error"]),  # mock status
            "last_accessed": project_data.get("last_updated")
        })

    return {"projects": project_list}

@router.post("/enable")
async def enable_project(request: Request, config: ServiceConfig = Depends(get_config)):
    data = await request.json()
    project_name = data.get("project_name")
    if not project_name:
        raise HTTPException(status_code=400, detail="project_name is required")

    project = config.projects.get(project_name)
    if not project:
        raise HTTPException(status_code=404, detail="Project not found")

    project["enabled"] = True

    with open(config.config_path, "w", encoding="utf-8") as f:
        json.dump(config, f, indent=2)

    return {"message": f"Project {project_name} enabled"}

@router.post("/disable")
async def disable_project(request: Request, config: ServiceConfig = Depends(get_config)):
    data = await request.json()
    project_name = data.get("project_name")
    if not project_name:
        raise HTTPException(status_code=400, detail="project_name is required")

    project = config.projects.get(project_name)
    if not project:
        raise HTTPException(status_code=404, detail="Project not found")

    project["enabled"] = False

    with open(config.config_path, "w", encoding="utf-8") as f:
        json.dump(config, f, indent=2)

    return {"message": f"Project {project_name} disabled"}

@router.post("/delete")
async def delete_project(request: Request, config: ServiceConfig = Depends(get_config)):
    data = await request.json()
    project_name = data.get("project_name")
    if not project_name:
        raise HTTPException(status_code=400, detail="project_name is required")

    if project_name not in config.projects:
        raise HTTPException(status_code=404, detail="Project not found")

    del config.projects[project_name]

    with open(config.config_path, "w", encoding="utf-8") as f:
        json.dump(config, f, indent=2)

    return {"message": f"Project {project_name} deleted"}