There is a class called MsvConfig
in which I want to keep some app configurations. I want to load this class using Dependency Injection. One of settings I keep in config is CORS_origins
.
def create_app() -> FastAPI:
app = FastAPI(dependencies=[Depends(MsvConfig)])
app.include_router(features.product_search.endpoints.endpoints.router)
return app
app = create_app()
@app.on_event("startup")
async def startup_event(msv_config: MsvConfig = Depends(MsvConfig)):
origins = msv_config.CORS_origins
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
When I try to do it this way, I get an error:
AttributeError: 'Depends' object has no attribute 'CORS_origins'
Can you give me a hint how it should be solved? Every example of using Depends
shows it on a FastAPI's http endpoints, but here I need to do something before any request arrives to my API.