Add Pydantic models and FastAPI router structure for agent and backend
CI / basic-check (push) Has been cancelled
CI / basic-check (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from app.config import settings
|
||||
from app.models import ServiceActionResponse, ServiceListResponse, ServiceState
|
||||
from app.services.systemd_manager import SystemdManager
|
||||
|
||||
router = APIRouter(prefix='/services', tags=['services'])
|
||||
manager = SystemdManager()
|
||||
|
||||
|
||||
@router.get('', response_model=ServiceListResponse)
|
||||
def list_services() -> ServiceListResponse:
|
||||
items = manager.list_services(settings.monitored_services)
|
||||
return ServiceListResponse(count=len(items), time=datetime.now(timezone.utc), items=items)
|
||||
|
||||
|
||||
@router.get('/{name}', response_model=ServiceState)
|
||||
def get_service(name: str) -> ServiceState:
|
||||
items = manager.list_services([name])
|
||||
if not items:
|
||||
raise HTTPException(status_code=404, detail='Service not found')
|
||||
return items[0]
|
||||
|
||||
|
||||
@router.post('/{name}/{action}', response_model=ServiceActionResponse)
|
||||
def service_action(name: str, action: Literal['start', 'stop', 'restart']) -> ServiceActionResponse:
|
||||
if action == 'start':
|
||||
manager.start(name)
|
||||
elif action == 'stop':
|
||||
manager.stop(name)
|
||||
elif action == 'restart':
|
||||
manager.restart(name)
|
||||
return ServiceActionResponse(
|
||||
status='accepted',
|
||||
action=action,
|
||||
service=name,
|
||||
time=datetime.now(timezone.utc),
|
||||
message=f'{action} requested',
|
||||
)
|
||||
Reference in New Issue
Block a user