from fastapi import APIRouter, HTTPException, Request from redis.exceptions import RedisError from sqlalchemy import text from sqlalchemy.exc import SQLAlchemyError from ...domain.errors import Unavailable from ..schemas import HealthOut router = APIRouter(tags=['health']) @router.get('/health/live', response_model=HealthOut) async def live() -> HealthOut: return HealthOut(status='ok') @router.get('/health/ready', response_model=HealthOut) async def ready(request: Request) -> HealthOut: if not request.app.state.ready: raise HTTPException(status_code=503, detail='приложение не готово принимать трафик') try: async with request.app.state.engine.connect() as connection: await connection.execute(text('SELECT 1')) await request.app.state.redis.ping() await request.app.state.storage.ping() except (SQLAlchemyError, RedisError, Unavailable, OSError): raise HTTPException(status_code=503, detail='обязательное хранилище недоступно') from None return HealthOut(status='ok')