diff --git a/src/application.py b/src/application.py index 68cc8bf..f00d8e6 100644 --- a/src/application.py +++ b/src/application.py @@ -15,16 +15,11 @@ You should have received a copy of the GNU General Public License along with scoreboard-benchmark . If not, see . """ -try: # This is mainly required for Travis CI automated testing. - from src.config import config -except ImportError: - from src.config_lock import config - +from src.config import config from src.models import db from src.views.errors import error_pages from src.views.scoreboard import scoreboard -from src.resources.utilities import cache -import os +from src.resources.utilities import cache, get_env_variable import flask_bootstrap import flask @@ -42,13 +37,10 @@ def create_app(config_name): return app -try: - configuration = os.environ['BSFLASK_ENV'] - print("Running with configuration: " + configuration) - app = create_app(configuration) -except (IndexError, KeyError): - print("Using default configuration.") - app = create_app('default') +configuration = get_env_variable("BSFLASK_ENV", fallback="default") +app = create_app(configuration) +print("Running with", configuration, "configuration setting.") + if __name__ == "__main__": diff --git a/src/config_lock.py b/src/config.py similarity index 88% rename from src/config_lock.py rename to src/config.py index 4c5e4d0..b860e81 100644 --- a/src/config_lock.py +++ b/src/config.py @@ -17,6 +17,7 @@ """ import os from src.models import db +from src.resources.utilities import get_env_variable basedir = os.path.abspath(os.path.dirname(__file__)) @@ -61,10 +62,10 @@ class ProductionConfig(Config): BOOTSTRAP_USE_MINIFIED = True DEBUG = False # Database Configuration - MYSQL_USERNAME = "" - MYSQL_PASSWORD = "" - MYSQL_HOSTNAME = "" - MYSQL_DATABASE = "" + MYSQL_USERNAME = get_env_variable("BSFLASK_MYSQL_USERNAME", fallback=None) + MYSQL_PASSWORD = get_env_variable("BSFLASK_MYSQL_PASSWORD", fallback=None) + MYSQL_HOSTNAME = get_env_variable("BSFLASK_MYSQL_HOSTNAME", fallback=None) + MYSQL_DATABASE = get_env_variable("BSFLASK_MYSQL_DATABASE", fallback=None) SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://{username}:{password}@{hostname}/{database}'\ .format(username=MYSQL_USERNAME, password=MYSQL_PASSWORD, hostname=MYSQL_HOSTNAME, database=MYSQL_DATABASE) diff --git a/src/resources/utilities.py b/src/resources/utilities.py index 8f3edd7..c4750a9 100644 --- a/src/resources/utilities.py +++ b/src/resources/utilities.py @@ -17,6 +17,7 @@ """ from flask_cache import Cache +import os cache = Cache() @@ -46,3 +47,31 @@ def to_zero_count(page_no): except (TypeError, ValueError): # page_no is not an int page_no = 0 return page_no + + +def get_env_variable(variable, fallback): + """ + Will try to retrieve the environment variable from the system and if it fails + it returns the fallback value. + + Args: + variable: The environment variable that should be retrieved + fallback: The default return value in case the environment variable is not retrieved + + Returns: + On success is returns the variable's value from the environment, on failure it returns the + value provided by fallback + + >>> get_env_variable('HOMES', 'fallback') + 'fallback' + + >>> import os + >>> os.environ['TEST'] = '13' + >>> get_env_variable("TEST", "14") + '13' + + """ + try: + return os.environ[variable] + except KeyError: + return fallback