Create app.py
Browse files- api/app.py +40 -0
api/app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from starlette.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from api.logger import setup_logger
|
| 5 |
+
from api.routes import router
|
| 6 |
+
|
| 7 |
+
logger = setup_logger(__name__)
|
| 8 |
+
|
| 9 |
+
def create_app():
|
| 10 |
+
app = FastAPI(
|
| 11 |
+
title="GizAI API",
|
| 12 |
+
docs_url="/docs", # Enable Swagger UI
|
| 13 |
+
redoc_url="/redoc", # Enable ReDoc
|
| 14 |
+
openapi_url="/openapi.json",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# CORS settings
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"], # Adjust as needed for security
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Include routes
|
| 27 |
+
app.include_router(router)
|
| 28 |
+
|
| 29 |
+
# Global exception handler for better error reporting
|
| 30 |
+
@app.exception_handler(Exception)
|
| 31 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
| 32 |
+
logger.error(f"An error occurred: {str(exc)}")
|
| 33 |
+
return JSONResponse(
|
| 34 |
+
status_code=500,
|
| 35 |
+
content={"message": "An internal server error occurred."},
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
return app
|
| 39 |
+
|
| 40 |
+
app = create_app()
|