Updated appdispatch 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 45b97d1 · Browse files · 1 file·+94 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+94 added−18 removed
- other:+94 added−18 removed
Provenance
- Commit
60de3f2- Tree
8cd47d8- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
45b97d1
1 file changed+94 added−18 removed
modified:docs/patterns/appdispatch.rst+94 added-18 removed
1 unmodified line2 3 Application Dispatching4 =======================5 6+Application dispatching is the process of combining multiple Flask7+applications on the WSGI level. You can not only combine Flask8+applications into something larger but any WSGI application. This would9+even allow you to run a Django and a Flask application in the same10+interpreter side by side if you want. The usefulness of this depends on11+how the applications work internally.12+13+This is fundamentally different from the :ref:`module approach14+<larger-applications>` is that in this case you are running the same or15+different Flask applications that are entirely isolated from each other.16+They run different configurations and are dispatched on the WSGI level.17+18+Combining Applications19+----------------------20+21+If you have entirely separated applications and you want them to work next22+to each other in the same Python interpreter process you can take23+advantage of the :class:`werkzeug.wsgi.DispatcherMiddleware`. The idea24+here is that each Flask application is a valid WSGI application and they25+are combined by the dispatcher middleware into a larger one that26+dispatched based on prefix.27+28+For example you could have your main application run on `/` and your29+backend interface on `/admin`::30+31+ from werkzeug.wsgi import DispatcherMiddleware32+ from frontend_app import application as frontend33+ from backend_app import application as backend34+35+ application = DispatcherMiddleware(frontend, {36+ '/backend': backend37+ })38+39+40+Dispatch by Subdomain41+---------------------42+43 Sometimes you might want to use multiple instances of the same application44 with different configurations. Assuming the application is created inside45 a function and you can call that function to instanciate it, that is46 really easy to implement. In order to develop your application to support47 creating new instances in functions have a look at the48 :ref:`app-factories` pattern.49 13-14-Dispatch by Subdomain15----------------------16-50 A very common example would be creating applications per subdomain. For51 instance you configure your webserver to dispatch all requests for all52 subdomains to your application and you then use the subdomain information20-to create user-specific instances.53+to create user-specific instances. Once you have your server set up to54+listen on all subdomains you can use a very simple WSGI application to do55+the dynamic application creation.56 22-Once you have your server set up to listen on all subdomains you can use a23-very simple WSGI application to do the dynamic application creation.57+The perfect level for abstraction in that regard is the WSGI layer. You58+write your own WSGI application that looks at the request that comes and59+and delegates it to your Flask application. If that application does not60+exist yet, it is dynamically created and remembered::61 25-The code for the dispatching looks roughly like this:26-27-.. sourcecode:: python28-62 from threading import Lock63 64 class SubdomainDispatcher(object):65 9 unmodified lines75 subdomain = host[:-len(self.domain)].rstrip('.')76 with self.lock:77 app = self.instances.get(subdomain)78 if app is None:46- app = self.make_app(subdomain)79+ app = self.create_app(subdomain)80 self.instances[subdomain] = app81 return app82 50- def make_app(self, subdomain):51- return self.create_app(subdomain)52-83 def __call__(self, environ, start_response):84 app = self.get_application(environ['HTTP_HOST'])85 return app(environ, start_response)86 87 58-If you want to use it, you can do something like this:88+This dispatcher can then be used like this::89 60-.. sourcecode:: python61-90 from myapplication import create_app, get_user_for_subdomain91 from werkzeug.exceptions import NotFound92 93 def make_app(subdomain):9 unmodified lines103 # otherwise create the application for the specific user104 return create_app(user)105 106 application = SubdomainDispatcher('example.com', make_app)107+108+109+Dispatch by Path110+----------------111+112+Dispatching by a path on the URL is very similar. Instead of looking at113+the `Host` header to figure out the subdomain one simply looks at the114+request path up to the first slash::115+116+ from threading import Lock117+ from werkzeug.wsgi import pop_path_info, peek_path_info118+119+ class PathDispatcher(object):120+121+ def __init__(self, default_app, create_app):122+ self.default_app = default_app123+ self.create_app = create_app124+ self.lock = Lock()125+ self.instances = {}126+127+ def get_application(self, prefix):128+ with self.lock:129+ app = self.instances.get(prefix)130+ if app is None:131+ app = self.create_app(prefix)132+ if app is not None:133+ self.instances[prefix] = app134+ return app135+136+ def __call__(self, environ, start_response):137+ app = self.get_application(peek_path_info(environ))138+ if app is not None:139+ pop_path_info(environ)140+ else:141+ app = self.default_app142+ return app(environ, start_response)143+144+The big difference between this and the subdomain one is that this one145+falls back to another application if the creator function returns `None`::146+147+ from myapplication import create_app, default_app, get_user_for_prefix148+149+ def make_app(prefix):150+ user = get_user_for_prefix(prefix)151+ if user is not None:152+ return create_app(user)153+154+ application = PathDispatcher('example.com', default_app, make_app)