def create_app(routers): from fastapi import FastAPI from fastapi.responses import JSONResponse from fastapi.openapi.utils import get_openapi from fastapi.responses import RedirectResponse from api_configs import Config api_app = FastAPI(title=str(Config.TITLE), default_response_class=JSONResponse) @api_app.get("/", include_in_schema=False, summary=str(Config.DESCRIPTION)) async def home(): return RedirectResponse(url="/docs") for router in list( [ getattr(routers, router) for router in routers.__all__ if getattr(routers, router) ] ): api_app.include_router(router) openapi_schema = get_openapi( title=Config.TITLE, description=Config.DESCRIPTION, version="0.0.1", routes=api_app.routes, ) if "components" in openapi_schema: openapi_schema["components"]["securitySchemes"] = { "Bearer Auth": { "type": "apiKey", "in": "header", "name": "evyos-session-key", "description": "Enter: **'Bearer <JWT>'**, where JWT is the access token", } } for route in api_app.routes: path = str(getattr(route, "path")) if route.include_in_schema: methods = [method.lower() for method in getattr(route, "methods")] for method in methods: insecure_paths = Config.INSECURE_PATHS.copy() insecure_paths.remove("/authentication/select") if path not in insecure_paths: openapi_schema["paths"][path][method]["security"] = [ {"Bearer Auth": []} ] openapi_schema["paths"][path][method]["responses"]["403"] = { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/HTTPValidationError" } } }, "description": "Returned if user is unauthorized.", } api_app.openapi_schema = openapi_schema return api_app