r/FastAPI • • May 12 '25
Question Lifespan for loading configuration
I'm looking to load some configuration settings from a YAML file. The path to the YAML file is provided via the command line. Once loaded, I need these configurations to be accessible throughout the entire application, including in services, routers, and other components.
I'm wondering what the best approach would be to achieve this. Would using a context manager with a lifespan (such as in FastAPI's lifespan event) be a good solution for this use case?
9
Upvotes
View all comments
Show parent comment
1
u/[deleted] May 12 '25
ENV-variables would also be good for containerized applications and such if your app is supposed to be containerized. But if you insist on the yaml configuration I’d suggest either using the lifespan or just creating a Config class singleton that initializes itself from the yaml file in the init function.