Skip to content

Commit

Permalink
Minor improvements in app factory (settings management)
Browse files Browse the repository at this point in the history
  • Loading branch information
skasberger committed Mar 23, 2021
1 parent bf29fc3 commit 3105d12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@ app/docs/build
app/docs/Makefile
Pipfile.lock
logs/
node_modules/
22 changes: 9 additions & 13 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ROOT_DIR = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))


class Config(BaseSettings):
class BaseConfig(BaseSettings):
"""Setting the default environment settings."""

SQLALCHEMY_DATABASE_URI: str = ""
Expand All @@ -20,7 +20,6 @@ class Config(BaseSettings):
FB_BATCH_SIZE: int = 50
URL_BATCH_SIZE: int = 1000
FLASK_DEBUG: bool = False
DEBUG: bool = False
TESTING: bool = False
TRAVIS: bool = False
SQLALCHEMY_TRACK_MODIFICATIONS: bool = False
Expand All @@ -30,53 +29,50 @@ def init_app(app):
pass


class DevelopmentConfig(Config):
class DevelopmentConfig(BaseConfig):
"""Setting the development environment settings.
Database is sqlite file or a postgresql database string passed by an environment variable.
"""

FLASK_ENV: str = "development"
FLASK_DEBUG: bool = True
DEBUG: bool = True
DEBUG_TB_INTERCEPT_REDIRECTS: bool = False
FLASK_RUN_EXTRA_FILES: str = "app/templates/"

class Config:
env_file = os.path.join(ROOT_DIR, "env/development.env")

@classmethod
def init_app(cls, app):
Config.init_app(app)
BaseConfig.init_app(app)

from flask_debugtoolbar import DebugToolbarExtension

DebugToolbarExtension(app)


class TestingConfig(Config):
FLASK_ENV: str = "testing"
class TestingConfig(BaseConfig):

TESTING: bool = True

class Config:
env_file = os.path.join(ROOT_DIR, "env/testing.env")

@classmethod
def init_app(cls, app):
Config.init_app(app)
BaseConfig.init_app(app)


class ProductionConfig(Config):
class ProductionConfig(BaseConfig):
"""Setting the production environment settings.
"""

FLASK_ENV: str = "production"

class Config:
env_file = os.path.join(ROOT_DIR, "env/production.env")

@classmethod
def init_app(cls, app):
Config.init_app(app)
BaseConfig.init_app(app)

# email errors to the administrators
import logging
Expand Down
10 changes: 6 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ def create_app() -> Flask:
"""Create application and load settings."""
print("* Start FHE Collector...")

config = get_config_class(os.getenv("FLASK_CONFIG"))
app = Flask("fhe_collector", root_path=ROOT_DIR)
env_name = os.getenv("FLASK_ENV")
config = get_config_class(env_name)
app.config.from_object(config())
config.init_app(app)

Expand All @@ -58,9 +59,10 @@ def create_app() -> Flask:
app.register_error_handler(404, not_found_error)
app.register_error_handler(500, internal_error)

print(' * Settings "{0}" loaded'.format(os.getenv("FLASK_CONFIG")))
print(" * Environment: " + app.config["FLASK_ENV"])
print(" * Database: " + app.config["SQLALCHEMY_DATABASE_URI"])
print(f" * Environment: {env_name}")
if app.config["DEBUG"]:
print(f" * Debug: On")
print(f" * Database: {app.config['SQLALCHEMY_DATABASE_URI']}")

return app

Expand Down

0 comments on commit 3105d12

Please sign in to comment.