diff --git a/edison/__init__.py b/edison/__init__.py index dac6ddb..befb02a 100644 --- a/edison/__init__.py +++ b/edison/__init__.py @@ -1,8 +1,10 @@ import os from flask import Flask +from edison.config import get_config_object # Put app here so the entire app could import it. app = Flask(__name__) +app.config.from_object(get_config_object(app.config["ENV"])) basedir = os.path.abspath(os.path.dirname(__file__)) diff --git a/edison/config.py b/edison/config.py new file mode 100644 index 0000000..97f665d --- /dev/null +++ b/edison/config.py @@ -0,0 +1,36 @@ +import inspect +import sys + + +def get_config_object(env_keyword: str): + """ + Returns the the desired config class path. + + The function iterates through a dictionary returned by inspect. + The dictionary contains details about all of the file members. + Its key is the name of the member and value is obj which contains all the details about the member. + The desired config path is being picked by the ENV_KEYWORD field defined in the config class. + + Parameters: + env_keyword (str): Should be equals to one of the config classes ENV_KEYWORD field. + + Returns: + str: module_name.class_name, which is the full path of the config class. + """ + for name, obj in inspect.getmembers(sys.modules[__name__]): + if issubclass(obj, Config) and obj.ENV_KEYWORD == env_keyword: + return ".".join([obj.__module__, name]) + + +class Config: + ENV_KEYWORD = "" + DEBUG = False + + +class ProductionConfig(Config): + ENV_KEYWORD = "production" + + +class DevelopmentConfig(Config): + ENV_KEYWORD = "development" + DEBUG = True diff --git a/setup.sh b/setup.sh index 02dcd7a..1e6346c 100644 --- a/setup.sh +++ b/setup.sh @@ -17,6 +17,8 @@ sudo apt-get install -y postgresql postgresql-contrib echo "install requirements" pip3 install -r /vagrant/requirements.txt +export FLASK_ENV=development + echo "running app.py" export FLASK_APP=/vagrant/edison/app.py flask run -h 0.0.0.0 -p $FLASK_PORT >> /vagrant/edison/app.log 2>&1 &