Skip to content

Commit 2df0b2d

Browse files
committed
better configuration handling for mongodb
1 parent 9f821db commit 2df0b2d

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

config/app_settings.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
"""Application settings."""
22
import os
3+
from typing import Optional, Union
34

4-
SECRET_KEY = os.getenv('SECRET_KEY')
5-
5+
# Server configuration
6+
APP_NAME = os.getenv('COMPOSE_PROJECT_NAME', 'gator')
67
SERVER_NAME = os.getenv('SERVER_NAME')
8+
SECRET_KEY = os.getenv('SECRET_KEY')
9+
GATOR_HOST = os.getenv('GATOR_HOST')
710

811
# MongoDB configuration
12+
13+
14+
def _get_mongodb_credential(credential_type: str,
15+
default: Optional[str] = None) -> Union[str, None]:
16+
"""Return a credential for the MongoDB database.
17+
18+
Will first check the environment variable MONGODB_{credential_type},
19+
and if that is not set, will check
20+
MONGODB_INITDB_ROOT_{credential_type}.
21+
"""
22+
assert credential_type in {'username', 'password'},\
23+
'credential_type must be either "username" or "password"'
24+
25+
credential_type = credential_type.upper()
26+
credential = os.getenv(f'MONGODB_{credential_type}')
27+
if not credential:
28+
credential = os.getenv(f'MONGO_INITDB_ROOT_{credential_type}', default)
29+
return credential
30+
31+
932
MONGODB_SETTINGS = {
10-
'db': os.getenv('MONGODB_DB', 'sqrl'),
11-
'host': os.getenv('MONGODB_HOST', 'mongodb'),
33+
'db': os.getenv('MONGODB_DB', APP_NAME),
34+
'host': os.getenv('MONGODB_HOST', 'localhost'),
1235
'port': int(os.getenv('MONGODB_PORT', 27017)),
13-
'username': os.getenv('MONGODB_USERNAME'),
14-
'password': os.getenv('MONGODB_PASSWORD'),
36+
# If no username or password is given, use the root credentials used to
37+
# init the Docker service.
38+
'username': _get_mongodb_credential('username', default='username'),
39+
'password': _get_mongodb_credential('password', default='password'),
40+
'authentication_source': os.getenv('MONGODB_AUTH_SOURCE', None),
1541
}
16-
17-
GATOR_HOST = os.getenv('GATOR_HOST')

0 commit comments

Comments
 (0)