11import logging
2- from typing import Any
2+ from typing import Annotated
33
4- from fastapi import APIRouter
4+ from fastapi import APIRouter , Depends
5+ from fastapi .responses import JSONResponse
56
7+ from app .container import get_database
8+ from app .db .db import Database
69
710logger = logging .getLogger (__name__ )
811router = APIRouter ()
@@ -14,10 +17,58 @@ def ok_or_error(value: bool) -> str:
1417
1518@router .get (
1619 "/health" ,
17- summary = "Health check" ,
18- tags = ["Service Information" ],
20+ summary = "Health Check" ,
21+ description = "health check for all dependent API services and components." ,
22+ status_code = 200 ,
23+ responses = {
24+ 200 : {
25+ "description" : "Health check completed (may contain unhealthy components)" ,
26+ "content" : {
27+ "application/json" : {
28+ "examples" : {
29+ "all_healthy" : {
30+ "summary" : "All services healthy" ,
31+ "value" : {
32+ "status" : "ok" ,
33+ "components" : {"database" : "ok" },
34+ },
35+ },
36+ }
37+ }
38+ },
39+ },
40+ 500 : {"description" : "Unexpected error during health check execution" },
41+ 503 : {
42+ "description" : "One or more components are unhealthy" ,
43+ "content" : {
44+ "application/json" : {
45+ "examples" : {
46+ "some_unhealthy" : {
47+ "summary" : "Some services unhealthy" ,
48+ "value" : {
49+ "status" : "error" ,
50+ "components" : {"database" : "error" },
51+ },
52+ },
53+ }
54+ }
55+ },
56+ },
57+ },
58+ tags = ["Health" ],
1959)
20- def health () -> dict [str , Any ]:
21- return {
22- "status" : "ok" ,
60+ def health (
61+ db : Annotated [Database , Depends (get_database )],
62+ ) -> JSONResponse :
63+ logger .info ("Checking application health" )
64+
65+ components = {
66+ "database" : ok_or_error (db .is_healthy ()),
2367 }
68+ healthy = ok_or_error (all (value == "ok" for value in components .values ()))
69+ content = {"status" : healthy , "components" : components }
70+ if healthy == "ok" :
71+ return JSONResponse (content = content )
72+ unhealthy = [name for name , status in components .items () if status != "ok" ]
73+ logger .warning ("Health check failed for components: %s" , ", " .join (unhealthy ))
74+ return JSONResponse (status_code = 503 , content = content )
0 commit comments