Skip to content

Commit

Permalink
Implement health check endpoint for verifying database connection
Browse files Browse the repository at this point in the history
  • Loading branch information
augusto-herrmann committed Feb 13, 2025
1 parent 79a6359 commit eebc390
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.responses import JSONResponse, RedirectResponse
from sqlalchemy.exc import IntegrityError, OperationalError
from sqlalchemy.ext.asyncio import AsyncSession


import crud
import crud_auth
from db_config import DbContextManager, create_db_and_tables
from db_config import (
check_db_connection,
create_db_and_tables,
DbContextManager,
get_db,
)
import email_config
import response_schemas
import schemas
Expand Down Expand Up @@ -174,6 +180,21 @@ async def docs_redirect(
)


@app.get("/health", include_in_schema=False)
async def health_check(db: AsyncSession = Depends(get_db)):
"""Verifica se o banco de dados está disponível."""
try:
# Executa uma query leve para verificar a conectividade com
# o banco de dados
await check_db_connection(db)
except OperationalError as exception:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Banco de dados indisponível",
) from exception
return {"status": "ok"}


@app.get("/robots.txt", include_in_schema=False)
async def robots_txt() -> Response:
"""Retorna um arquivo robots.txt para orientar crawlers e permitir
Expand Down

0 comments on commit eebc390

Please # to comment.