Started work on app dispatch docs
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 b3fc9eb · Browse files · 2 files·+79 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+79 added−0 removed
- other:+79 added−0 removed
Provenance
- Commit
45b97d1- Tree
e334c23- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b3fc9eb
2 files changed+79 added−0 removed
added:docs/patterns/appdispatch.rst+78 added
1+.. _app-dispatch:2+3+Application Dispatching4+=======================5+6+Sometimes you might want to use multiple instances of the same application7+with different configurations. Assuming the application is created inside8+a function and you can call that function to instanciate it, that is9+really easy to implement. In order to develop your application to support10+creating new instances in functions have a look at the11+:ref:`app-factories` pattern.12+13+14+Dispatch by Subdomain15+---------------------16+17+A very common example would be creating applications per subdomain. For18+instance you configure your webserver to dispatch all requests for all19+subdomains to your application and you then use the subdomain information20+to create user-specific instances.21+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.24+25+The code for the dispatching looks roughly like this:26+27+.. sourcecode:: python28+29+ from threading import Lock30+31+ class SubdomainDispatcher(object):32+33+ def __init__(self, domain, create_app):34+ self.domain = domain35+ self.create_app = create_app36+ self.lock = Lock()37+ self.instances = {}38+39+ def get_application(self, host):40+ host = host.split(':')[0]41+ assert host.endswith(self.domain), 'Configuration error'42+ subdomain = host[:-len(self.domain)].rstrip('.')43+ with self.lock:44+ app = self.instances.get(subdomain)45+ if app is None:46+ app = self.make_app(subdomain)47+ self.instances[subdomain] = app48+ return app49+50+ def make_app(self, subdomain):51+ return self.create_app(subdomain)52+53+ def __call__(self, environ, start_response):54+ app = self.get_application(environ['HTTP_HOST'])55+ return app(environ, start_response)56+57+58+If you want to use it, you can do something like this:59+60+.. sourcecode:: python61+62+ from myapplication import create_app, get_user_for_subdomain63+ from werkzeug.exceptions import NotFound64+65+ def make_app(subdomain):66+ user = get_user_for_subdomain(subdomain)67+ if user is None:68+ # if there is no user for that subdomain we still have69+ # to return a WSGI application that handles that request.70+ # We can then just return the NotFound() exception as71+ # application which will render a default 404 page.72+ # You might also redirect the user to the main page then73+ return NotFound()74+75+ # otherwise create the application for the specific user76+ return create_app(user)77+78+ application = SubdomainDispatcher('example.com', make_app)modified:docs/patterns/index.rst+1 added
16 unmodified lines17 :maxdepth: 218 19 packages20 appfactories21+ appdispatch22 distribute23 fabric24 sqlite325 sqlalchemy11 unmodified lines