121 lines
3.2 KiB
Python
121 lines
3.2 KiB
Python
"""
|
|
FastAPI Application Handler Module
|
|
|
|
This module contains all the handler functions for configuring and setting up the FastAPI application:
|
|
- CORS middleware configuration
|
|
- Exception handlers setup
|
|
- Uvicorn server configuration
|
|
"""
|
|
|
|
from typing import Dict, Any
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi import FastAPI, Request, HTTPException, status
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from ErrorHandlers.bases import (
|
|
BaseErrorModelClass,
|
|
StatusesModelClass,
|
|
LanguageModelClass,
|
|
)
|
|
from ErrorHandlers import statuses
|
|
from middleware.auth_middleware import MiddlewareModule
|
|
|
|
|
|
def setup_cors_middleware(app: FastAPI) -> None:
|
|
"""
|
|
Configure CORS middleware for the application.
|
|
|
|
Args:
|
|
app: FastAPI application instance
|
|
"""
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
|
|
"""
|
|
Handle HTTP exceptions and return formatted error responses.
|
|
|
|
Args:
|
|
request: FastAPI request object
|
|
exc: HTTP exception instance
|
|
|
|
Returns:
|
|
JSONResponse: Formatted error response
|
|
"""
|
|
error_code = getattr(exc, "error_code", None)
|
|
if error_code:
|
|
status_code = StatusesModelClass.retrieve_error_by_code(error_code)
|
|
error_message = LanguageModelClass.retrieve_error_by_code(
|
|
error_code, request.headers.get("accept-language", "en")
|
|
)
|
|
else:
|
|
status_code = exc.status_code
|
|
error_message = str(exc.detail)
|
|
|
|
return JSONResponse(
|
|
status_code=status_code,
|
|
content={"detail": error_message, "error_code": error_code},
|
|
)
|
|
|
|
|
|
async def generic_exception_handler(request: Request, exc: Exception) -> JSONResponse:
|
|
"""
|
|
Handle generic exceptions and return formatted error responses.
|
|
|
|
Args:
|
|
request: FastAPI request object
|
|
exc: Exception instance
|
|
|
|
Returns:
|
|
JSONResponse: Formatted error response
|
|
"""
|
|
return JSONResponse(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
content={"detail": "Internal server error", "error_code": "INTERNAL_ERROR"},
|
|
)
|
|
|
|
|
|
def setup_exception_handlers(app: FastAPI) -> None:
|
|
"""
|
|
Configure custom exception handlers for the application.
|
|
|
|
Args:
|
|
app: FastAPI application instance
|
|
"""
|
|
app.add_exception_handler(HTTPException, http_exception_handler)
|
|
app.add_exception_handler(Exception, generic_exception_handler)
|
|
|
|
|
|
def setup_middleware(app: FastAPI) -> None:
|
|
"""
|
|
Configure all middleware for the application.
|
|
|
|
Args:
|
|
app: FastAPI application instance
|
|
"""
|
|
setup_cors_middleware(app)
|
|
app.add_middleware(MiddlewareModule.RequestTimingMiddleware)
|
|
setup_exception_handlers(app)
|
|
|
|
|
|
def get_uvicorn_config() -> Dict[str, Any]:
|
|
"""
|
|
Get Uvicorn server configuration.
|
|
|
|
Returns:
|
|
Dict[str, Any]: Uvicorn configuration dictionary
|
|
"""
|
|
return {
|
|
"app": "app:app",
|
|
"host": "0.0.0.0",
|
|
"port": 41575,
|
|
"log_level": "info",
|
|
"reload": True,
|
|
} |