"""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}