Skip to content
Myr

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

2 files+79 added−0 removed

added:docs/patterns/appdispatch.rst+78 added
1.. _app-dispatch:23Application Dispatching4=======================56Sometimes you might want to use multiple instances of the same application7with different configurations. Assuming the application is created inside8a function and you can call that function to instanciate it, that is9really easy to implement. In order to develop your application to support10creating new instances in functions have a look at the11:ref:`app-factories` pattern.121314Dispatch by Subdomain15---------------------1617A very common example would be creating applications per subdomain. For18instance you configure your webserver to dispatch all requests for all19subdomains to your application and you then use the subdomain information20to create user-specific instances.2122Once you have your server set up to listen on all subdomains you can use a23very simple WSGI application to do the dynamic application creation.2425The code for the dispatching looks roughly like this:2627.. sourcecode:: python2829 from threading import Lock3031 class SubdomainDispatcher(object):3233 def __init__(self, domain, create_app):34 self.domain = domain35 self.create_app = create_app36 self.lock = Lock()37 self.instances = {}3839 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 app4950 def make_app(self, subdomain):51 return self.create_app(subdomain)5253 def __call__(self, environ, start_response):54 app = self.get_application(environ['HTTP_HOST'])55 return app(environ, start_response)565758If you want to use it, you can do something like this:5960.. sourcecode:: python6162 from myapplication import create_app, get_user_for_subdomain63 from werkzeug.exceptions import NotFound6465 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()7475 # otherwise create the application for the specific user76 return create_app(user)7778 application = SubdomainDispatcher('example.com', make_app)
modified:docs/patterns/index.rst+1 added
16 unmodified lines
17 :maxdepth: 21819 packages20 appfactories21 appdispatch22 distribute23 fabric24 sqlite325 sqlalchemy
11 unmodified lines