ЛР 2: мессенджер на PostgreSQL и сессии в Redis

This commit is contained in:
2026-10-23 12:16:56 +03:00
parent 65003b6119
commit 1dd701be2b
69 changed files with 2163 additions and 30 deletions
+28
View File
@@ -0,0 +1,28 @@
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()
except (SQLAlchemyError, RedisError, Unavailable, OSError):
raise HTTPException(status_code=503, detail='обязательное хранилище недоступно') from None
return HealthOut(status='ok')