freeleaps-service-hub/app/authentication/webapi/bootstrap/application.py

73 lines
2.1 KiB
Python

import logging
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from app.authentication.webapi.providers import common
from app.authentication.webapi.providers import logger
from app.authentication.webapi.providers import router
from app.authentication.webapi.providers import database
# from app.authentication.webapi.providers import scheduler
from app.authentication.webapi.providers import exception_handler
from .freeleaps_app import FreeleapsApp
def create_app() -> FastAPI:
logging.info("App initializing")
app = FreeleapsApp()
register(app, exception_handler)
register(app, database)
register(app, logger)
register(app, router)
# register(app, scheduler)
register(app, common)
# Call the custom_openapi function to change the OpenAPI version
customize_openapi_security(app)
return app
# This function overrides the OpenAPI schema version to 3.0.0
def customize_openapi_security(app: FastAPI) -> None:
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
# Generate OpenAPI schema
openapi_schema = get_openapi(
title="FreeLeaps API",
version="3.1.0",
description="FreeLeaps API Documentation",
routes=app.routes,
)
# Ensure the components section exists in the OpenAPI schema
if "components" not in openapi_schema:
openapi_schema["components"] = {}
# Add security scheme to components
openapi_schema["components"]["securitySchemes"] = {
"bearerAuth": {"type": "http", "scheme": "bearer", "bearerFormat": "JWT"}
}
# Add security requirement globally
openapi_schema["security"] = [{"bearerAuth": []}]
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
def register(app, provider):
logging.info(provider.__name__ + " registering")
provider.register(app)
def boot(app, provider):
logging.info(provider.__name__ + " booting")
provider.boot(app)