Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple .env files for Config #432

Closed
thomasjiangcy opened this issue Mar 10, 2019 · 2 comments
Closed

Multiple .env files for Config #432

thomasjiangcy opened this issue Mar 10, 2019 · 2 comments

Comments

@thomasjiangcy
Copy link

thomasjiangcy commented Mar 10, 2019

Hello! Firstly, thank you for the great work - I've fallen in love with Starlette and am looking forward to the great things ahead.

I noticed that the Config object only takes in one .env file. I was thinking that it might be worthwhile to handle multiple .env files as there could be scenarios where we keep multiple .env files based on what these environment variables logically belong to.

For example, .db.env might have env vars for DB related configurations, while .web.env might have env vars specific to the Starlette application (e.g. DEBUG).

Off of my head after looking at the source code, this could naively be implemented by adding an env_dir keyword argument and looping through any files in it that match a certain regex. Maybe something like:

import re

class Config:
 
    def __init__(
        self,
        env_file: str = None,
        env_dir: typing.Union[str, Path] = None,
        env_file_regex: str = r'.+?\.env',
        environ: Mapping[str, str] = environ
    ) -> None:
        self.environ = environ
        self.file_values: typing.Dict[str, str] = {}

        if env_file is not None and os.path.isfile(env_file):
            self.file_values.update(self._read_file(env_file))

        if env_dir is not None and os.path.isdir(env_dir):
            for item in dir(env_dir):
                if re.search(env_file_regex, item):
                    self.file_values.update(self._read_file(item))
@tomchristie
Copy link
Member

Hiya! Glad to hear you're digging it!

I guess we may actually end up pulling the configuration stuff out into a strictly seperate package (or indeed even just pointing at python-decouple - since it's the same style).

With 12-factor config you really should have a fairly small set of environment. (Eg. the database configuration should just be a single URL.)

What we will want to do though is provide really good examples of how to arrange things sensibly, so that eg. you have a project that has a settings.py that includes a whole bunch of project configuration, and which you can arrange into seperate bits, and demonstrate clearly how the environment should be a small set of variables, but the project settings may be more comprehensive.

We could perfectly well also point to other configuration-manangement packages that're out there as alternatives.

@thomasjiangcy
Copy link
Author

That makes sense! Thanks for the reply :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants