forked from mtusi2026/mtusi2026lab4
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, status
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
from app.schemas import HealthResponse
|
|
from app.storage import storage
|
|
|
|
router = APIRouter(tags=["health"])
|
|
|
|
|
|
@router.get(
|
|
"/health/live", response_model=HealthResponse, status_code=status.HTTP_200_OK
|
|
)
|
|
async def health_live() -> HealthResponse:
|
|
"""Проверка жизни процесса (Liveness)."""
|
|
return HealthResponse(status="ok")
|
|
|
|
|
|
@router.get(
|
|
"/health/ready", response_model=HealthResponse, status_code=status.HTTP_200_OK
|
|
)
|
|
async def health_ready() -> HealthResponse:
|
|
"""Проверка готовности сервиса и его зависимостей (Readiness)."""
|
|
is_ready = await storage.check_health()
|
|
if not is_ready:
|
|
raise StarletteHTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="Сервис или его хранилища недоступны",
|
|
)
|
|
return HealthResponse(status="ok")
|