Added appfactory pattern
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 182ee31 · Browse files · 2 files·+74 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+74 added−0 removed
- other:+74 added−0 removed
Provenance
- Commit
02b916d- Tree
1f8130e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
182ee31
2 files changed+74 added−0 removed
added:docs/patterns/appfactories.rst+73 added
1+Application Factories2+=====================3+4+If you are already using packages and modules for your application5+(:ref:`packages`) there are couple of really nice ways to further improve6+the experience. A common pattern is creating the application object when7+the module is imported. But if you move the creation of this object,8+into a function, you can then create multiple instances of this and later.9+10+So why would you want to do this?11+12+1. Testing. You can have instances of the application with different13+ settings to test every case.14+2. Multiple instances. Imagine you want to run different versions of the15+ same application. Of course you could have multiple instances with16+ different configs set up in your webserver, but if you use factories,17+ you can have multiple instances of the same application running in the18+ same application process which can be handy.19+20+So how would you then actually implement that?21+22+Basic Factories23+---------------24+25+The idea is to set up the application in a function. Like this::26+27+ def create_app(config_filename):28+ app = Flask(__name__)29+ app.config.from_pyfile(config_filename)30+31+ from yourapplication.views.admin import admin32+ from yourapplication.views.frontend import frontend33+ app.register_module(admin)34+ app.register_module(frontend)35+36+ return app37+38+The downside is that you cannot use the application object in the modules39+at import time. You can however use it from within a request. How do you40+get access the application with the config? Use41+:data:`~flask.current_app`::42+43+ from flask import current_app, Module, render_template44+ admin = Module(__name__, url_prefix='/admin')45+46+ @admin.route('/')47+ def index():48+ return render_template(current_app.config['INDEX_TEMPLATE'])49+50+Here we look up the name of a template in the config.51+52+Using Applications53+------------------54+55+So to use such an application you then have to create the application56+first. Here an example `run.py` file that runs such an application::57+58+ from yourapplication import create_app59+ app = create_app('/path/to/config.cfg')60+ app.run()61+62+Factory Improvements63+--------------------64+65+The factory function from above is not very clever so far, you can improve66+it. The following changes are straightforward and possible:67+68+1. make it possible to pass in configuration values for unittests so that69+ you don't have to create config files on the filesystem70+2. call a function from a module when the application is setting up so71+ that you have a place to modify attributes of the application (like72+ hooking in before / after request handlers etc.)73+3. Add in WSGI middlewares when the application is creating if necessary.modified:docs/patterns/index.rst+1 added
15 unmodified lines16 .. toctree::17 :maxdepth: 218 19 packages20+ appfactories21 distribute22 sqlite323 sqlalchemy24 fileuploads8 unmodified lines