Adding support for configuration via environment variables
This commit is contained in:
parent
0c0be87cd0
commit
425ebf1771
3 changed files with 40 additions and 18 deletions
|
@ -15,16 +15,11 @@
|
|||
You should have received a copy of the GNU General Public License
|
||||
along with scoreboard-benchmark . If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
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.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)
|
||||
configuration = get_env_variable("BSFLASK_ENV", fallback="default")
|
||||
app = create_app(configuration)
|
||||
except (IndexError, KeyError):
|
||||
print("Using default configuration.")
|
||||
app = create_app('default')
|
||||
print("Running with", configuration, "configuration setting.")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -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)
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue