Created
March 18, 2022 10:30
-
-
Save warvariuc/a0c004dbf05b8f95b78a23603a1b9f39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import logging.config | |
import mongoengine | |
import pydantic as p | |
logging.basicConfig() | |
class MongoDbDsn(p.AnyUrl): | |
allowed_schemes = {'mongodb', 'mongodb+srv', 'mongomock'} | |
class SettingsBase(p.BaseSettings): | |
ENVIRONMENT: str = 'dev' | |
class Config: | |
env_file = '.env' | |
env_file_encoding = 'utf-8' | |
class Settings(SettingsBase): | |
MONGODB_URL: MongoDbDsn = 'mongodb://localhost/...' | |
SECRET_KEY: str = '...' | |
EMAIL_HOST_USER: p.EmailStr = '...' | |
EMAIL_HOST_PASSWORD: str = '' | |
EMAIL_HOST: str = 'smtp.office365.com' | |
EMAIL_PORT: int = 587 | |
# How many minutes the access codes are valid | |
ACCESS_CODE_EXPIRATION_PERIOD = 10 | |
# Sharing-area maximum file size in bytes | |
SHARING_AREA_MAX_FILE_SIZE = 8_000_000 | |
# For how many minutes the session cookie is valid (since the user made an API call) | |
AUTH_COOKIE_LIFETIME = 60 * 8 | |
AUTH_COOKIE_SECURE = False | |
AUTH_COOKIE_NAME = 'session' | |
# Username for accessing docs; if empty -- docs are disabled | |
DOCS_USERNAME: str = 'docs' | |
# Password for accessing docs; if empty -- docs are not password protected | |
DOCS_PASSWORD: str = 'pass' | |
UPDATE_DB_BASE_URL: p.AnyUrl = "..." | |
AZURE_OAUTH: dict = { | |
"authority": "https://login.microsoftonline.com/...", | |
"client_id": "...", | |
"scope": [ "api://.../.default" ], | |
"secret": "...", | |
"endpoint": "https://graph.microsoft.com/v1.0/users", | |
} | |
LOGGING: dict = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'verbose': { | |
'format': '[%(asctime)s] %(levelname)s %(name)s:%(lineno)s - %(message)s', | |
'datefmt': '%Y-%m-%d %H:%M:%S', | |
}, | |
'simple': { | |
'format': '%(levelname)s %(asctime)s %(message)s', | |
'datefmt': '%H:%M:%S', | |
}, | |
}, | |
'handlers': { | |
'console': { | |
'level': 'INFO', | |
'formatter': 'simple', | |
'class': 'logging.StreamHandler', | |
}, | |
'console_debug': { | |
'level': 'DEBUG', | |
'formatter': 'verbose', | |
'class': 'logging.StreamHandler', | |
}, | |
'mongodb': { | |
'level': 'INFO', | |
'formatter': 'verbose', | |
'class': '...', | |
# For now we don't compress the logs for humans to be able to easily analyze them | |
'compress': False, | |
}, | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['console', 'mongodb'], | |
'level': 'NOTSET', | |
}, | |
} | |
} | |
class SettingsProd(Settings): | |
UPDATE_DB_BASE_URL: p.AnyUrl = "..." | |
# The docs are disabled on prod | |
DOCS_USERNAME: str = '' | |
class SettingsStaging(Settings): | |
UPDATE_DB_BASE_URL: p.AnyUrl = "..." | |
class SettingsDev(Settings): | |
# The docs are not password protected in dev environment | |
DOCS_PASSWORD: str = '' | |
class SettingsTest(Settings): | |
MONGODB_URL: MongoDbDsn = 'mongomock://localhost' | |
LOGGING: dict = { | |
'version': 1, | |
'disable_existing_loggers': False, | |
'formatters': { | |
'simple': { | |
'format': '%(levelname)s %(asctime)s %(message)s', | |
'datefmt': '%H:%M:%S', | |
}, | |
}, | |
'handlers': { | |
'console': { | |
'level': 'WARNING', | |
'formatter': 'simple', | |
'class': 'logging.StreamHandler', | |
}, | |
}, | |
'loggers': { | |
'': { | |
'handlers': ['console'], | |
}, | |
} | |
} | |
class Config: | |
# We don't want tests to use env variables/files | |
env_file = None | |
env_prefix = '~' | |
@functools.cache | |
def get_settings() -> Settings: | |
# https://rednafi.github.io/digressions/python/2020/06/03/python-configs.html | |
environment = SettingsBase().ENVIRONMENT | |
settings_class = { | |
'dev': SettingsDev, | |
'staging': SettingsStaging, | |
'prod': SettingsProd, | |
'testing': SettingsTest, | |
}.get(environment.lower()) | |
if not settings_class: | |
raise RuntimeError(f'Invalid `ENVIRONMENT`: {environment!r}') | |
settings = settings_class() | |
mongoengine.register_connection(mongoengine.DEFAULT_CONNECTION_NAME, host=settings.MONGODB_URL) | |
logging.config.dictConfig(settings.LOGGING) | |
return settings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment