ConfigManager should not replace app.config

Updated ConfigManager to only modify app.config and not replace it.
Swagger does not play well, when app.config is not a real dict and
it is not necessary to keep ConfigManager around after init.

Also added "API" flag to config (default: disabled).
master
Alexander Graf 4 years ago
parent 3b35180b41
commit 7229c89de1

@ -51,6 +51,7 @@ DEFAULT_CONFIG = {
'WEBMAIL': 'none', 'WEBMAIL': 'none',
'RECAPTCHA_PUBLIC_KEY': '', 'RECAPTCHA_PUBLIC_KEY': '',
'RECAPTCHA_PRIVATE_KEY': '', 'RECAPTCHA_PRIVATE_KEY': '',
'API': False,
# Advanced settings # Advanced settings
'PASSWORD_SCHEME': 'PBKDF2', 'PASSWORD_SCHEME': 'PBKDF2',
'LOG_LEVEL': 'WARNING', 'LOG_LEVEL': 'WARNING',
@ -71,7 +72,7 @@ DEFAULT_CONFIG = {
'POD_ADDRESS_RANGE': None 'POD_ADDRESS_RANGE': None
} }
class ConfigManager(dict): class ConfigManager:
""" Naive configuration manager that uses environment only """ Naive configuration manager that uses environment only
""" """
@ -86,19 +87,16 @@ class ConfigManager(dict):
def get_host_address(self, name): def get_host_address(self, name):
# if MYSERVICE_ADDRESS is defined, use this # if MYSERVICE_ADDRESS is defined, use this
if '{}_ADDRESS'.format(name) in os.environ: if f'{name}_ADDRESS' in os.environ:
return os.environ.get('{}_ADDRESS'.format(name)) return os.environ.get(f'{name}_ADDRESS')
# otherwise use the host name and resolve it # otherwise use the host name and resolve it
return system.resolve_address(self.config['HOST_{}'.format(name)]) return system.resolve_address(self.config[f'HOST_{name}'])
def resolve_hosts(self): def resolve_hosts(self):
self.config["IMAP_ADDRESS"] = self.get_host_address("IMAP") for key in ['IMAP', 'POP3', 'AUTHSMTP', 'SMTP', 'REDIS']:
self.config["POP3_ADDRESS"] = self.get_host_address("POP3") self.config[f'{key}_ADDRESS'] = self.get_host_address(key)
self.config["AUTHSMTP_ADDRESS"] = self.get_host_address("AUTHSMTP") if self.config['WEBMAIL'] != 'none':
self.config["SMTP_ADDRESS"] = self.get_host_address("SMTP") self.config['WEBMAIL_ADDRESS'] = self.get_host_address('WEBMAIL')
self.config["REDIS_ADDRESS"] = self.get_host_address("REDIS")
if self.config["WEBMAIL"] != "none":
self.config["WEBMAIL_ADDRESS"] = self.get_host_address("WEBMAIL")
def __coerce_value(self, value): def __coerce_value(self, value):
if isinstance(value, str) and value.lower() in ('true','yes'): if isinstance(value, str) and value.lower() in ('true','yes'):
@ -108,6 +106,7 @@ class ConfigManager(dict):
return value return value
def init_app(self, app): def init_app(self, app):
# get current app config
self.config.update(app.config) self.config.update(app.config)
# get environment variables # get environment variables
self.config.update({ self.config.update({
@ -121,27 +120,8 @@ class ConfigManager(dict):
template = self.DB_TEMPLATES[self.config['DB_FLAVOR']] template = self.DB_TEMPLATES[self.config['DB_FLAVOR']]
self.config['SQLALCHEMY_DATABASE_URI'] = template.format(**self.config) self.config['SQLALCHEMY_DATABASE_URI'] = template.format(**self.config)
self.config['RATELIMIT_STORAGE_URL'] = 'redis://{0}/2'.format(self.config['REDIS_ADDRESS']) self.config['RATELIMIT_STORAGE_URL'] = f'redis://{self.config["REDIS_ADDRESS"]}/2'
self.config['QUOTA_STORAGE_URL'] = 'redis://{0}/1'.format(self.config['REDIS_ADDRESS']) self.config['QUOTA_STORAGE_URL'] = f'redis://{self.config["REDIS_ADDRESS"]}/1'
# update the app config itself
app.config = self
def setdefault(self, key, value): # update the app config
if key not in self.config: app.config.update(self.config)
self.config[key] = value
return self.config[key]
def get(self, *args):
return self.config.get(*args)
def keys(self):
return self.config.keys()
def __getitem__(self, key):
return self.config.get(key)
def __setitem__(self, key, value):
self.config[key] = value
def __contains__(self, key):
return key in self.config

Loading…
Cancel
Save