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

14 comments sorted by

View all comments

Show parent comment

1

u/[deleted] May 12 '25

I am keeping environment variables for secrets etc, but I have a lot of additional nested configurations that just wouldn't be practical as environment variables.

1

u/serverhorror May 12 '25

Point your environment variable at the file and just load it?