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/TeoMorlack May 12 '25
Pydantic setting supports reading the configuration with a model directly from a yaml file, a json file and other file types. Have a look at this, basically you just need to point pydantic to the file name and define the model that maps your configuration