Skip to content

How do you handle settings? #1548

Answered by rgajason
danechitoaie asked this question in Q&A
Discussion options

You must be logged in to vote

We have created an AppSettings class based off BaseSettings. We then use dependency injection to make the settings available.

config.py

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

dependencies.py

@lru_cache()
def get_settings() -> AppSettings:
    return AppSettings()

main.py

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 settings variable holds our app settings.

S…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@danechitoaie
Comment options

@provinzkraut
Comment options

@danechitoaie
Comment options

@provinzkraut
Comment options

Answer selected by provinzkraut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants