File size: 2,027 Bytes
590a501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Platform service control API — online start/stop by feature module."""

from fastapi import APIRouter, HTTPException

from app.services.platform_manager import platform_manager

router = APIRouter(prefix="/platform", tags=["platform"])


@router.get("/services")
async def list_platform_services():
    services = [s.to_dict() for s in platform_manager.list_services()]
    return {"services": services}


@router.get("/services/{service_id}")
async def get_platform_service(service_id: str):
    try:
        return platform_manager.get_status(service_id).to_dict()
    except KeyError:
        raise HTTPException(status_code=404, detail=f"Unknown service: {service_id}")


@router.post("/services/{service_id}/start")
async def start_platform_service(service_id: str):
    try:
        service = await platform_manager.start(service_id)
        return {
            "message": f"{service.name} 已启动",
            "service": service.to_dict(),
            "services": [s.to_dict() for s in platform_manager.list_services()],
        }
    except KeyError:
        raise HTTPException(status_code=404, detail=f"Unknown service: {service_id}")
    except Exception as exc:
        raise HTTPException(status_code=500, detail=str(exc))


@router.post("/services/{service_id}/stop")
async def stop_platform_service(service_id: str):
    try:
        service = await platform_manager.stop(service_id)
        return {
            "message": f"{service.name} 已停止",
            "service": service.to_dict(),
            "services": [s.to_dict() for s in platform_manager.list_services()],
        }
    except KeyError:
        raise HTTPException(status_code=404, detail=f"Unknown service: {service_id}")
    except Exception as exc:
        raise HTTPException(status_code=500, detail=str(exc))


@router.post("/services/start-all")
async def start_all_services():
    services = [s.to_dict() for s in await platform_manager.start_all()]
    return {"message": "已尝试启动全部模块", "services": services}