File size: 1,171 Bytes
29dbc6a
 
 
 
 
 
 
 
 
 
3a87070
 
 
 
29dbc6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fastapi import FastAPI, Request
from starlette.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from api.logger import setup_logger
from api.routes import router

logger = setup_logger(__name__)

def create_app():
    app = FastAPI(
        title="NiansuhAI API Gateway",
        docs_url=None,          # Disable Swagger UI
        redoc_url=None,         # Disable ReDoc
        openapi_url=None,       # Disable OpenAPI schema
    )

    # CORS settings
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],                   # Adjust as needed for security
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # Include routes
    app.include_router(router)

    # Global exception handler for better error reporting
    @app.exception_handler(Exception)
    async def global_exception_handler(request: Request, exc: Exception):
        logger.error(f"An error occurred: {str(exc)}")
        return JSONResponse(
            status_code=500,
            content={"message": "An internal server error occurred."},
        )

    return app

app = create_app()