Skip to content

Commit v0.5

kwmccabe edited this page Apr 17, 2018 · 9 revisions

v0.5 - AppConfig: DevelopmentConfig, TestingConfig, ProductionConfig, ENV FLASK_CONFIG


Files changed (4)

File web/Dockerfile MODIFIED

  • Update comments.
  • Add FLASK_CONFIG environment variable.
 # base container image
 FROM python:3.6.1-alpine
 
-# add tzdata to base image; set timezone environment variable
+# update system packages
 RUN apk add --no-cache tzdata
+
+# set environment variables for system
 ENV TZ America/Los_Angeles
 
+# set environment variables for application
+ENV FLASK_CONFIG development
+
 # setup working directory within container
 RUN mkdir -p /web
 WORKDIR /web

File web/app/init.py MODIFIED

  • Replace direct use of AppConfig with reference to a config subclass DevelopmentConfig, TestingConfig or ProductionConfig.
 from flask import Flask
-from config import AppConfig
+from config import config
 
-def create_app():
+def create_app(config_name):
     app = Flask(__name__)
 
-    app.config.from_object(AppConfig)
-    AppConfig.init_app(app)
+    app.config.from_object(config[config_name])
+    config[config_name].init_app(app)
 
     return app

File web/config.py MODIFIED

  • Create subclasses of AppConfig corresponding to different deployment environments.
  • DevelopmentConfig and TestingConfig contain single values for now.
  • ProductionConfig contains a placeholder init_app() method.
  • Create config dictionary to map FLASK_CONFIG values to objects.
     @staticmethod
     def init_app(app):
         pass
+
+
+class DevelopmentConfig(AppConfig):
+    DEBUG = True
+
+class TestingConfig(AppConfig):
+    TESTING = True
+
+
+class ProductionConfig(AppConfig):
+    DEBUG = False
+    TESTING = False
+
+    @classmethod
+    def init_app(cls, app):
+        # multi-step setups could go here
+        AppConfig.init_app(app)
+
+
+config = {
+    'development': DevelopmentConfig,
+    'testing':     TestingConfig,
+    'production':  ProductionConfig,
+    'default':     DevelopmentConfig
+}

File web/flaskapp.py MODIFIED

  • Pass the environment value FLASK_CONFIG through to app.create_app().
  • Update the route /info/config to display the value of FLASK_CONFIG.
-app = create_app()
+app = create_app(os.getenv('FLASK_CONFIG') or 'default')
... 
 @app.route('/info/config')
 def app_config():
     cnf = dict(app.config)
-    return "Current Config : %s" % cnf
+    return "'%s' Config : %s" % (os.getenv('FLASK_CONFIG'),cnf)

Commit-v0.4 | Commit-v0.5 | Commit-v0.6

Clone this wiki locally