LogicGoInfotechSpaces commited on
Commit
9cf4498
·
verified ·
1 Parent(s): 1b0a7c7

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +73 -3
app/main.py CHANGED
@@ -1,9 +1,14 @@
1
  from contextlib import asynccontextmanager
 
 
 
 
2
 
3
  from fastapi import FastAPI
4
 
5
  from app.api.routes import router as api_router
6
- from app.db.mongo import close_mongo_connection, connect_to_mongo
 
7
 
8
 
9
  @asynccontextmanager
@@ -25,5 +30,70 @@ app.include_router(api_router, prefix="/api/v1")
25
 
26
 
27
  @app.get("/health", tags=["Health"])
28
- async def health_check() -> dict[str, str]:
29
- return {"status": "ok"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from contextlib import asynccontextmanager
2
+ import asyncio
3
+ from typing import Any, Dict
4
+
5
+ from fastapi.responses import JSONResponse
6
 
7
  from fastapi import FastAPI
8
 
9
  from app.api.routes import router as api_router
10
+ from app.core.openai_client import openai_client
11
+ from app.db.mongo import close_mongo_connection, connect_to_mongo, get_client
12
 
13
 
14
  @asynccontextmanager
 
30
 
31
 
32
  @app.get("/health", tags=["Health"])
33
+ async def health_check() -> JSONResponse | Dict[str, Any]:
34
+ checks: Dict[str, bool] = {"mongo": False, "openai": False}
35
+ errors: Dict[str, str] = {}
36
+
37
+ # Run checks with conservative timeouts to avoid hanging the health endpoint.
38
+ mongo_ok, mongo_err = await _run_check(_check_mongo)
39
+ checks["mongo"] = mongo_ok
40
+ if mongo_err:
41
+ errors["mongo"] = mongo_err
42
+
43
+ openai_ok, openai_err = await _run_check(_check_openai)
44
+ checks["openai"] = openai_ok
45
+ if openai_err:
46
+ errors["openai"] = openai_err
47
+
48
+ if errors:
49
+ return JSONResponse(status_code=503, content={"status": "unhealthy", "checks": checks, "errors": errors})
50
+
51
+ return {"status": "healthy", "checks": checks}
52
+
53
+
54
+ async def _run_check(fn, timeout: float = 5.0) -> tuple[bool, str | None]:
55
+ try:
56
+ await asyncio.wait_for(fn(), timeout=timeout)
57
+ return True, None
58
+ except Exception as exc: # pragma: no cover - defensive
59
+ return False, str(exc)
60
+
61
+
62
+ async def _check_mongo() -> None:
63
+ await connect_to_mongo()
64
+ client = get_client()
65
+ await client.admin.command("ping")
66
+
67
+
68
+ async def _check_openai() -> None:
69
+ await openai_client.models.list()
70
+
71
+ # from contextlib import asynccontextmanager
72
+
73
+ # from fastapi import FastAPI
74
+
75
+ # from app.api.routes import router as api_router
76
+ # from app.db.mongo import close_mongo_connection, connect_to_mongo
77
+
78
+
79
+ # @asynccontextmanager
80
+ # async def lifespan(_: FastAPI):
81
+ # await connect_to_mongo()
82
+ # try:
83
+ # yield
84
+ # finally:
85
+ # await close_mongo_connection()
86
+
87
+
88
+ # app = FastAPI(
89
+ # title="Auto Categorizer API",
90
+ # version="1.0.0",
91
+ # lifespan=lifespan,
92
+ # )
93
+
94
+ # app.include_router(api_router, prefix="/api/v1")
95
+
96
+
97
+ # @app.get("/health", tags=["Health"])
98
+ # async def health_check() -> dict[str, str]:
99
+ # return {"status": "ok"}