Paramjit Singh commited on
Commit
f8b7398
Β·
unverified Β·
2 Parent(s): 94849945b375d5

Merge pull request #104 from SatyamPrakash09/feat/health-endpoint

Browse files

Feat: Add /health endpoint to monitor both vector and sql database he…

Files changed (1) hide show
  1. backend/app/main.py +33 -1
backend/app/main.py CHANGED
@@ -10,9 +10,12 @@ from fastapi import FastAPI
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from fastapi.staticfiles import StaticFiles
12
  from fastapi.responses import FileResponse
 
 
13
 
14
  from app.config import get_settings
15
- from app.database import init_db
 
16
 
17
  # Configure logging
18
  logging.basicConfig(
@@ -89,6 +92,35 @@ def health_check():
89
  "version": "2.0.0",
90
  }
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  # ── Serve Next.js Frontend (production) ──────────────
94
  FRONTEND_BUILD_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "frontend", "out")
 
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from fastapi.staticfiles import StaticFiles
12
  from fastapi.responses import FileResponse
13
+ from sqlalchemy import select
14
+ from sqlalchemy.exc import SQLAlchemyError
15
 
16
  from app.config import get_settings
17
+ from app.database import init_db, get_db
18
+ from app.rag.vectorstore import get_chroma_client
19
 
20
  # Configure logging
21
  logging.basicConfig(
 
92
  "version": "2.0.0",
93
  }
94
 
95
+ @app.get('/health')
96
+ def db_health():
97
+ db_status = "down"
98
+ chroma_status = "down"
99
+
100
+ # --- DB check ---
101
+ try:
102
+ db = next(get_db())
103
+ db.execute(select(1))
104
+ db_status = "up"
105
+ except SQLAlchemyError:
106
+ db_status = "down"
107
+ except Exception:
108
+ db_status = "down"
109
+
110
+ # --- Chroma check ---
111
+ try:
112
+ chroma = get_chroma_client()
113
+ chroma.heartbeat()
114
+ chroma_status = "up"
115
+ except Exception:
116
+ chroma_status = "down"
117
+
118
+ overall_status = "ok" if db_status == "up" and chroma_status == "up" else "degraded"
119
+ return{
120
+ "status": db_status,
121
+ "chroma": chroma_status,
122
+ "db": db_status
123
+ }
124
 
125
  # ── Serve Next.js Frontend (production) ──────────────
126
  FRONTEND_BUILD_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "frontend", "out")