From de8e7deb219bbbbcc7966730a616d014be781704 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Fri, 16 May 2025 12:24:36 +0000 Subject: [PATCH] Add health endpoint at root path --- main.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 5c867b8..152165f 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,12 @@ import socket import sys import uvicorn -from fastapi import FastAPI +from fastapi import Depends, FastAPI +from sqlalchemy.orm import Session from app.api.api import api_router from app.core.config import settings +from app.db.session import get_db # Configure logging logging.basicConfig(level=logging.INFO) @@ -19,6 +21,26 @@ app = FastAPI( version="0.1.0", ) +@app.get("/health", tags=["health"], summary="Health check endpoint") +def health_check(db: Session = Depends(get_db)): + """ + Check the health of the application: + + - **status**: Overall application status + - **database**: Database connection status + """ + try: + # Make a simple query to check if the database is responsive + db.execute("SELECT 1") + db_status = "healthy" + except Exception: + db_status = "unhealthy" + + return { + "status": "ok", + "database": db_status + } + app.include_router(api_router, prefix=settings.API_V1_STR) def find_available_port(start_port, max_attempts=10):