|
1 | 1 | """Application settings.""" |
2 | 2 | import os |
| 3 | +from typing import Optional, Union |
3 | 4 |
|
4 | | -SECRET_KEY = os.getenv('SECRET_KEY') |
5 | | - |
| 5 | +# Server configuration |
| 6 | +APP_NAME = os.getenv('COMPOSE_PROJECT_NAME', 'gator') |
6 | 7 | SERVER_NAME = os.getenv('SERVER_NAME') |
| 8 | +SECRET_KEY = os.getenv('SECRET_KEY') |
| 9 | +GATOR_HOST = os.getenv('GATOR_HOST') |
7 | 10 |
|
8 | 11 | # 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 | + |
9 | 32 | 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'), |
12 | 35 | '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), |
15 | 41 | } |
16 | | - |
17 | | -GATOR_HOST = os.getenv('GATOR_HOST') |
|
0 commit comments