1+ from fastapi import FastAPI , HTTPException
2+ from fastapi .middleware .cors import CORSMiddleware
3+ from fastapi .responses import JSONResponse
4+ from fastapi .middleware .trustedhost import TrustedHostMiddleware
5+
6+ from app .routers import health , prediction
7+ from app .config import get_settings
8+
9+ settings = get_settings ()
10+
11+ app = FastAPI (
12+ title = settings .PROJECT_NAME ,
13+ description = "AI-Powered Healthcare Diagnosis System with Multi-Disease Detection and LangChain Agents" ,
14+ version = "1.0.0" ,
15+ docs_url = "/docs" if settings .ENVIRONMENT != "production" else None ,
16+ redoc_url = "/redoc" if settings .ENVIRONMENT != "production" else None ,
17+ )
18+
19+ # Configure CORS
20+ app .add_middleware (
21+ CORSMiddleware ,
22+ allow_origins = settings .CORS_ORIGINS ,
23+ allow_credentials = True ,
24+ allow_methods = ["GET" , "POST" ],
25+ allow_headers = ["*" ],
26+ )
27+
28+ # Add trusted host middleware
29+ app .add_middleware (
30+ TrustedHostMiddleware ,
31+ allowed_hosts = settings .ALLOWED_HOSTS ,
32+ )
33+
34+ # Include routers
35+ app .include_router (health .router , prefix = f"{ settings .API_V1_STR } /health" , tags = ["health" ])
36+ app .include_router (prediction .router , prefix = f"{ settings .API_V1_STR } /predict" , tags = ["prediction" ])
37+
38+ @app .get ("/" , tags = ["root" ])
39+ async def root ():
40+ """
41+ Root endpoint returning API information and health status.
42+ """
43+ return {
44+ "status" : "healthy" ,
45+ "version" : "1.0.0" ,
46+ "environment" : settings .ENVIRONMENT ,
47+ "documentation" : "/docs" if settings .ENVIRONMENT != "production" else None
48+ }
49+
50+ @app .exception_handler (HTTPException )
51+ async def http_exception_handler (request , exc ):
52+ return JSONResponse (
53+ status_code = exc .status_code ,
54+ content = {"detail" : exc .detail },
55+ )
56+
57+ @app .exception_handler (Exception )
58+ async def general_exception_handler (request , exc ):
59+ return JSONResponse (
60+ status_code = 500 ,
61+ content = {"detail" : "Internal server error" },
62+ )
0 commit comments