Skip to content

Commit v0.4

kwmccabe edited this page Apr 17, 2018 · 15 revisions

v0.4 - app.create_app(), AppConfig, RUN apk add tzdata


Files changed (4)

File web/Dockerfile MODIFIED

  • Run apk add tzdata to install timezone support within the container
  • Set the environment variable TZ America/Los_Angeles.
  • The change should be visible at the existing route /info/date.
+# add tzdata to base image; set timezone environment variable
+RUN apk add --no-cache tzdata
+ENV TZ America/Los_Angeles

File web/app/init.py ADDED

  • The __init__.py file (blank or otherwise) marks the app directory as a package.
  • Move app = Flask(__name__) from flaskapp.py into a function defined on this main app package.
  • Update values in app.config by passing an instance our new AppConfig object to the application.
  • Call AppConfig.init_app() to perform other setup operations.
  • Return an instance of the application.
+from flask import Flask
+from config import AppConfig
+
+def create_app():
+    app = Flask(__name__)
+
+    app.config.from_object(AppConfig)
+    AppConfig.init_app(app)
+
+    return app

File web/config.py ADDED

  • A minimal AppConfig object, passed to the application via app.config.from_object(AppConfig)
  • Set a single value, SECRET_KEY.
  • The empty method init_app() will provide a location for other setup operations.
+import os
+
+class AppConfig(object):
+    SECRET_KEY = os.environ.get('SECRET_KEY') or 'my-super-secret-app-key'
+
+    @staticmethod
+    def init_app(app):
+        pass

File web/flaskapp.py MODIFIED

  • Replace app = Flask(__name__) with app = create_app(), as defined in app/__init__.py.
  • Create new route /info/config to display app.config values.
-from flask import Flask
-app = Flask(__name__)

+from app import create_app
+app = create_app()

...

+@app.route('/info/config')
+def app_config():
+    cnf = dict(app.config)
+    return "Current Config : %s" % cnf

Commit-v0.3 | Commit-v0.4 | Commit-v0.5

Clone this wiki locally