Skip to content
Myr

Merge branch '2.3.x'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parents 6d49a1e, 0273664 · Browse files · 1 file+29 added−26 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+29 added−26 removed

  • other:+29 added−26 removed

1 file+29 added−26 removed

modified:docs/patterns/appdispatch.rst+29 added-26 removed
16 unmodified lines
17Working with this Document18--------------------------1920Each of the techniques and examples below results in an ``application``21object that can be run with any WSGI server. For production, see22:doc:`/deploying/index`. For development, Werkzeug provides a server23through :func:`werkzeug.serving.run_simple`::21object that can be run with any WSGI server. For development, use the22``flask run`` command to start a development server. For production, see23:doc:`/deploying/index`.2425 from werkzeug.serving import run_simple26 run_simple('localhost', 5000, application, use_reloader=True)25.. code-block:: python2628Note that :func:`run_simple <werkzeug.serving.run_simple>` is not intended for29use in production. Use a production WSGI server. See :doc:`/deploying/index`.3031In order to use the interactive debugger, debugging must be enabled both on32the application and the simple server. Here is the "hello world" example with33debugging and :func:`run_simple <werkzeug.serving.run_simple>`::3427 from flask import Flask36 from werkzeug.serving import run_simple2829 app = Flask(__name__)39 app.debug = True3031 @app.route('/')32 def hello_world():33 return 'Hello World!'3445 if __name__ == '__main__':46 run_simple('localhost', 5000, app,47 use_reloader=True, use_debugger=True, use_evalex=True)354936Combining Applications37----------------------3839If you have entirely separated applications and you want them to work next
3 unmodified lines
43are combined by the dispatcher middleware into a larger one that is44dispatched based on prefix.4546For example you could have your main application run on ``/`` and your61backend interface on ``/backend``::47backend interface on ``/backend``.4849.. code-block:: python5051 from werkzeug.middleware.dispatcher import DispatcherMiddleware52 from frontend_app import application as frontend53 from backend_app import application as backend54
21 unmodified lines
7677The perfect level for abstraction in that regard is the WSGI layer. You78write your own WSGI application that looks at the request that comes and79delegates it to your Flask application. If that application does not92exist yet, it is dynamically created and remembered::80exist yet, it is dynamically created and remembered.8182.. code-block:: python8384 from threading import Lock8586 class SubdomainDispatcher:87
18 unmodified lines
106 app = self.get_application(environ['HTTP_HOST'])107 return app(environ, start_response)108109120This dispatcher can then be used like this::110This dispatcher can then be used like this:111112.. code-block:: python113114 from myapplication import create_app, get_user_for_subdomain115 from werkzeug.exceptions import NotFound116117 def make_app(subdomain):
16 unmodified lines
134----------------135136Dispatching by a path on the URL is very similar. Instead of looking at137the ``Host`` header to figure out the subdomain one simply looks at the146request path up to the first slash::138request path up to the first slash.139140.. code-block:: python141142 from threading import Lock149 from werkzeug.wsgi import pop_path_info, peek_path_info143 from wsgiref.util import shift_path_info144145 class PathDispatcher:146147 def __init__(self, default_app, create_app):
11 unmodified lines
159 self.instances[prefix] = app160 return app161162 def __call__(self, environ, start_response):169 app = self.get_application(peek_path_info(environ))163 app = self.get_application(_peek_path_info(environ))164 if app is not None:171 pop_path_info(environ)165 shift_path_info(environ)166 else:167 app = self.default_app168 return app(environ, start_response)169170 def _peek_path_info(environ):171 segments = environ.get("PATH_INFO", "").lstrip("/").split("/", 1)172 if segments:173 return segments[0]174175 return None176177The big difference between this and the subdomain one is that this one177falls back to another application if the creator function returns ``None``::178falls back to another application if the creator function returns ``None``.179180.. code-block:: python181182 from myapplication import create_app, default_app, get_user_for_prefix183184 def make_app(prefix):185 user = get_user_for_prefix(prefix)
4 unmodified lines