Created
August 29, 2018 15:52
-
-
Save EdMan1022/4843dfc4533e713cb5aeb339d9424381 to your computer and use it in GitHub Desktop.
Flask config setup that utilizes a Singleton pattern to always return the same configuration instance when called multiple times. Could allow for other functions or methods to run and modify the config, and have those changes represented later on during app lifetime.
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
from os import environ | |
from logging.config import dictConfig | |
from werkzeug.security import generate_password_hash | |
class BaseConfig(object): | |
def _config_logger(self): | |
""" | |
Configures the log level for the various run configurations | |
:return: None | |
""" | |
pass | |
def __init__(self): | |
self.MAX_CONTENT_LENGTH = 1e6 | |
self._config_logger() | |
class ProdConfig(BaseConfig): | |
def _config_logger(self): | |
super(ProdConfig, self)._config_logger() | |
dictConfig({ | |
"version": 1, | |
"root": { | |
"level": "ERROR" | |
}}) | |
def __init__(self): | |
super(ProdConfig, self).__init__() | |
self.AUTH_TOKEN = environ["AUTH_TOKEN"] | |
self.SECRET_KEY = environ["SECRET_KEY"] | |
self.QWLB_URL_CREATE = environ["QWLB_URL_CREATE"] | |
self.ELQ_URL = environ["ELQ_URL"] | |
self.QWLB_API_USER = environ["QWLB_API_USER"] | |
self.QWLB_API_PASSWORD = generate_password_hash( | |
environ["QWLB_API_PASSWORD"]) | |
class DevConfig(BaseConfig): | |
def _config_logger(self): | |
super(DevConfig, self)._config_logger() | |
dictConfig({ | |
"version": 1, | |
"root": { | |
"level": "INFO" | |
}}) | |
def __init__(self): | |
super(DevConfig, self).__init__() | |
self.DEBUG = True | |
self.AUTH_TOKEN = "test" | |
self.SECRET_KEY = "test" | |
self.QWLB_URL_CREATE = "http://0.0.0.0:8080/test" | |
self.ELQ_URL = "http://0.0.0.0:8080/test" | |
self.QWLB_API_USER = "test" | |
self.QWLB_API_PASSWORD = generate_password_hash("test") | |
class TestConfig(DevConfig): | |
def _config_logger(self): | |
super(DevConfig, self)._config_logger() | |
dictConfig({ | |
"version": 1, | |
"root": { | |
"level": "DEBUG" | |
}}) | |
def __init__(self): | |
super(TestConfig, self).__init__() | |
self.TESTING = True | |
self.DEBUG = False | |
class GetConfig(object): | |
_instance = None | |
def __init__(self, namespace): | |
if not self._instance: | |
if namespace == 'test': | |
config = TestConfig | |
elif namespace == 'dev' \ | |
or namespace == 'qwlb-dev': | |
config = DevConfig | |
else: | |
config = ProdConfig | |
self._instance = config() | |
def __getattr__(self, name): | |
return getattr(self._instance, name) | |
def get_config(): | |
return GetConfig(environ['NAMESPACE']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment