mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: better healthcheck
This commit is contained in:
parent
6cd1cf7e9c
commit
46f016f661
8 changed files with 112 additions and 9 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
from fastapi import APIRouter, Depends
|
||||
from src.routers import health
|
||||
from src.routers import usergroups
|
||||
from src.routers import dev, trail, users, auth, orgs, roles
|
||||
from src.routers.ai import ai
|
||||
|
|
@ -42,6 +43,8 @@ if os.environ.get("CLOUD_INTERNAL_KEY"):
|
|||
dependencies=[Depends(cloud_internal.check_internal_cloud_key)],
|
||||
)
|
||||
|
||||
v1_router.include_router(health.router, prefix="/health", tags=["health"])
|
||||
|
||||
# Dev Routes
|
||||
v1_router.include_router(
|
||||
dev.router,
|
||||
|
|
|
|||
11
apps/api/src/routers/health.py
Normal file
11
apps/api/src/routers/health.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from fastapi import Depends, APIRouter
|
||||
from sqlmodel import Session
|
||||
from src.services.health.health import check_health
|
||||
from src.core.events.database import get_db_session
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("")
|
||||
async def health(db_session: Session = Depends(get_db_session)):
|
||||
return await check_health(db_session)
|
||||
0
apps/api/src/services/health/__init__.py
Normal file
0
apps/api/src/services/health/__init__.py
Normal file
21
apps/api/src/services/health/health.py
Normal file
21
apps/api/src/services/health/health.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from fastapi import HTTPException
|
||||
from sqlmodel import Session, select
|
||||
from src.db.organizations import Organization
|
||||
|
||||
async def check_database_health(db_session: Session) -> bool:
|
||||
statement = select(Organization)
|
||||
result = db_session.exec(statement)
|
||||
|
||||
if not result:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
async def check_health(db_session: Session) -> bool:
|
||||
# Check database health
|
||||
database_healthy = await check_database_health(db_session)
|
||||
|
||||
if not database_healthy:
|
||||
raise HTTPException(status_code=503, detail="Database is not healthy")
|
||||
|
||||
return True
|
||||
Loading…
Add table
Add a link
Reference in a new issue