Skip to content

Commit v0.4

kwmccabe edited this page Apr 9, 2018 · 15 revisions

Commit v0.4

app.create_app(), AppConfig, RUN apk add tzdata

Files changed (4)

File web/Dockerfile MODIFIED

Run app 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() provides a place for other setup operations.

+# see http://flask.pocoo.org/docs/0.12/api/
+
+import os
+
+class AppConfig(object):
+    SECRET_KEY = os.environ.get('SECRET_KEY') or 'kp-cUHRAYH1n-sPaJICcEE2kOpU62mCk'
+
+    @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__)
+
+# call create_app() in app/__init__.py
+from app import create_app
+app = create_app()
 
 
 @app.route('/')
     return "Current Datetime : %s" % ts
 
 
[email protected]('/info/config')
+def app_config():
+    cnf = dict(app.config)
+    return "Current Config : %s" % cnf
Clone this wiki locally