|
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, |
|
redoc_url=None, |
|
openapi_url=None, |
|
) |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
app.include_router(router) |
|
|
|
|
|
@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() |
|
|