File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99from fastapi .exceptions import RequestValidationError
1010from fastapi .staticfiles import StaticFiles
1111from fastapi .templating import Jinja2Templates
12- from sqlalchemy import select , func
12+ from sqlalchemy import select , func , text
1313from sqlalchemy .ext .asyncio import AsyncSession
1414from sqlalchemy .orm import selectinload
1515from starlette .exceptions import HTTPException as StarletteHTTPException
@@ -35,6 +35,17 @@ async def lifespan(_app: FastAPI):
3535app .include_router (users .router , prefix = "/api/users" , tags = ["users" ])
3636app .include_router (posts .router , prefix = "/api/posts" , tags = ["posts" ])
3737
38+ @app .get ("/health" )
39+ async def health_check (db : Annotated [AsyncSession , Depends (get_db )]):
40+ try :
41+ await db .execute (text ("SELECT 1" ))
42+ except Exception as e :
43+ raise HTTPException (
44+ status_code = status .HTTP_503_SERVICE_UNAVAILABLE ,
45+ detail = "Database is not available" ,
46+ ) from e
47+ return {"status" : "healthy" }
48+
3849
3950@app .get ("/" , include_in_schema = False , name = "home" )
4051@app .get ("/posts" , include_in_schema = False , name = "posts" )
You can’t perform that action at this time.
0 commit comments