freeleaps-service-hub/apps/notification/webapi/bootstrap/application.py
zhenyus 4dfbab4d4d fix: invalid import path for app config
Signed-off-by: zhenyus <zhenyus@mathmast.com>
2025-03-17 21:52:07 +08:00

85 lines
2.4 KiB
Python

import logging
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from webapi.providers import common
from webapi.providers.logger import register_logger
from webapi.providers import router
from webapi.providers import database
from webapi.providers import scheduler
from webapi.providers import message_queue
from webapi.providers import exception_handler
from webapi.providers import probes
from webapi.providers import metrics
from .freeleaps_app import FreeleapsApp
from common.config.app_settings import app_settings
def create_app() -> FastAPI:
logging.info("App initializing")
app = FreeleapsApp()
register_logger()
register(app, exception_handler)
# 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)
register(app, database)
register(app, router)
register(app, scheduler)
register(app, common)
register(app, message_queue)
# 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)