Skip to content

Commit

Permalink
Merge pull request #931 from Tim-Erwin/envvar_prefix
Browse files Browse the repository at this point in the history
make the prefix for environment variables alterable
  • Loading branch information
r0fls authored Sep 5, 2017
2 parents 158da09 + e2e25eb commit 8b4ca51
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 8 additions & 2 deletions docs/sanic/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ In general the convention is to only have UPPERCASE configuration parameters. Th

There are several ways how to load configuration.

### From environment variables.
### From Environment Variables

Any variables defined with the `SANIC_` prefix will be applied to the sanic config. For example, setting `SANIC_REQUEST_TIMEOUT` will be loaded by the application automatically. You can pass the `load_env` boolean to the Sanic constructor to override that:
Any variables defined with the `SANIC_` prefix will be applied to the sanic config. For example, setting `SANIC_REQUEST_TIMEOUT` will be loaded by the application automatically and fed into the `REQUEST_TIMEOUT` config variable. You can pass a different prefix to Sanic:

```python
app = Sanic(load_env='MYAPP_')
```

Then the above variable would be `MYAPP_REQUEST_TIMEOUT`. If you want to disable loading from environment variables you can set it to `False` instead:

```python
app = Sanic(load_env=False)
Expand Down
11 changes: 6 additions & 5 deletions sanic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def __init__(self, defaults=None, load_env=True, keep_alive=True):
self.GRACEFUL_SHUTDOWN_TIMEOUT = 15.0 # 15 sec

if load_env:
self.load_environment_vars()
prefix = SANIC_PREFIX if load_env is True else load_env
self.load_environment_vars(prefix=prefix)

def __getattr__(self, attr):
try:
Expand Down Expand Up @@ -195,14 +196,14 @@ def from_object(self, obj):
if key.isupper():
self[key] = getattr(obj, key)

def load_environment_vars(self):
def load_environment_vars(self, prefix=SANIC_PREFIX):
"""
Looks for any ``SANIC_`` prefixed environment variables and applies
Looks for prefixed environment variables and applies
them to the configuration if present.
"""
for k, v in os.environ.items():
if k.startswith(SANIC_PREFIX):
_, config_key = k.split(SANIC_PREFIX, 1)
if k.startswith(prefix):
_, config_key = k.split(prefix, 1)
try:
self[config_key] = int(v)
except ValueError:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ class Config:
def test_auto_load_env():
environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic()
assert app.config.TEST_ANSWER == "42"
assert app.config.TEST_ANSWER == 42
del environ["SANIC_TEST_ANSWER"]

def test_auto_load_env():
def test_dont_load_env():
environ["SANIC_TEST_ANSWER"] = "42"
app = Sanic(load_env=False)
assert getattr(app.config, 'TEST_ANSWER', None) == None
del environ["SANIC_TEST_ANSWER"]

def test_load_env_prefix():
environ["MYAPP_TEST_ANSWER"] = "42"
app = Sanic(load_env='MYAPP_')
assert app.config.TEST_ANSWER == 42
del environ["MYAPP_TEST_ANSWER"]

def test_load_from_file():
app = Sanic('test_load_from_file')
config = b"""
Expand Down

0 comments on commit 8b4ca51

Please sign in to comment.