94 lines
3.2 KiB
Python
94 lines
3.2 KiB
Python
"""
|
|
FastAPI Application Entry Point
|
|
|
|
This module initializes and configures the FastAPI application with:
|
|
- CORS middleware for cross-origin requests
|
|
- Request timing middleware for performance monitoring
|
|
- Custom exception handlers for consistent error responses
|
|
- Prometheus instrumentation for metrics
|
|
- API routers for endpoint organization
|
|
"""
|
|
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from create_routes import get_all_routers
|
|
from prometheus_fastapi_instrumentator import Instrumentator
|
|
from app_handler import setup_middleware, get_uvicorn_config
|
|
from create_file import setup_security_schema, configure_route_security
|
|
from open_api_creator import OpenAPISchemaCreator, create_openapi_schema
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""Create and configure the FastAPI application."""
|
|
app = FastAPI(
|
|
responses={
|
|
422: {
|
|
"description": "Validation Error",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"detail": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object",
|
|
"properties": {
|
|
"loc": {
|
|
"type": "array",
|
|
"items": {"type": "string"},
|
|
},
|
|
"msg": {"type": "string"},
|
|
"type": {"type": "string"},
|
|
},
|
|
},
|
|
}
|
|
},
|
|
}
|
|
}
|
|
},
|
|
}
|
|
}
|
|
)
|
|
|
|
# Get all routers and protected routes from the new configuration
|
|
routers, protected_routes = get_all_routers()
|
|
|
|
# Include all routers
|
|
for router in routers:
|
|
app.include_router(router)
|
|
|
|
# Configure OpenAPI schema with security
|
|
def custom_openapi():
|
|
if app.openapi_schema:
|
|
return app.openapi_schema
|
|
|
|
# Create OpenAPI schema using our custom creator
|
|
openapi_schema = create_openapi_schema(app)
|
|
|
|
# Add security scheme
|
|
openapi_schema.update(setup_security_schema())
|
|
|
|
# Configure security for protected routes
|
|
for path, methods in protected_routes.items():
|
|
for method in methods:
|
|
configure_route_security(
|
|
path, method, openapi_schema, list(protected_routes.keys())
|
|
)
|
|
|
|
app.openapi_schema = openapi_schema
|
|
return app.openapi_schema
|
|
|
|
app.openapi = custom_openapi
|
|
return app
|
|
|
|
|
|
app = create_app() # Initialize FastAPI application
|
|
Instrumentator().instrument(app=app).expose(app=app) # Setup Prometheus metrics
|
|
setup_middleware(app) # Configure middleware and exception handlers
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn_config = get_uvicorn_config() # Run the application with Uvicorn
|
|
uvicorn.Server(uvicorn.Config(**uvicorn_config)).run()
|