How do you handle settings? #1548
-
How do you handle loading settings from ENV and then making them available in the route handlers? At the moment I'm using a BaseSettings class from pydantic. Is this the right way? And how do I make this available in all the route handlers that need it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
We have created an
class AppSettings(BaseSettings):
app_name: str = "My Awesome App"
app_short_name: str = "awesome"
admin_email: str = "[email protected]"
mongodb_url: str
mongodb_db: str
@lru_cache()
def get_settings() -> AppSettings:
return AppSettings()
app = Starlite(
...
dependencies={
"settings": Provide(get_settings, use_cache=True),
},
) Within a given controller, we can say something like: async def my_endpoint(
self,
settings: AppSettings,
) ...and the Since this is based off Pydantic's https://docs.litestar.dev/latest/usage/dependency-injection.html |
Beta Was this translation helpful? Give feedback.
We have created an
AppSettings
class based offBaseSettings
. We then use dependency injection to make the settings available.config.py
dependencies.py
main.py
Within a given controller, we can say something like:
...and the
settings
variable holds our app settings.S…