freeleaps-service-hub/apps/devops/app/bootstrap/application.py
2025-07-30 10:50:22 +08:00

84 lines
2.3 KiB
Python

import logging
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from app.providers import common
from app.providers.logger import register_logger
from app.providers import router
from app.providers import database
from app.providers import metrics
from app.providers import probes
from app.providers import exception_handler
from app.providers import message_queue
from app.common.config.app_settings import app_settings
def create_app() -> FastAPI:
logging.info("App initializing")
app = FreeleapsApp()
register_logger()
register(app, exception_handler)
register(app, database)
register(app, router)
register(app, common)
register(app, message_queue)
# Call the custom_openapi function to change the OpenAPI version
customize_openapi_security(app)
# Register probe APIs if enabled
if app_settings.PROBES_ENABLED:
register(app, probes)
# Register metrics APIs if enabled
if app_settings.METRICS_ENABLED:
register(app, metrics)
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)
class FreeleapsApp(FastAPI):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)