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
Provenance
- Commit
9e9acfb- Tree
3ddfc8f- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
6d49a1e- Parent 2
0273664
1 file changed+29 added−26 removed
modified:docs/patterns/appdispatch.rst+29 added-26 removed
16 unmodified lines17 Working with this Document18 --------------------------19 20 Each of the techniques and examples below results in an ``application``21-object that can be run with any WSGI server. For production, see22-:doc:`/deploying/index`. For development, Werkzeug provides a server23-through :func:`werkzeug.serving.run_simple`::21+object 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`.24 25- from werkzeug.serving import run_simple26- run_simple('localhost', 5000, application, use_reloader=True)25+.. code-block:: python26 28-Note that :func:`run_simple <werkzeug.serving.run_simple>` is not intended for29-use in production. Use a production WSGI server. See :doc:`/deploying/index`.30-31-In order to use the interactive debugger, debugging must be enabled both on32-the application and the simple server. Here is the "hello world" example with33-debugging and :func:`run_simple <werkzeug.serving.run_simple>`::34-27 from flask import Flask36- from werkzeug.serving import run_simple28 29 app = Flask(__name__)39- app.debug = True30 31 @app.route('/')32 def hello_world():33 return 'Hello World!'34 45- if __name__ == '__main__':46- run_simple('localhost', 5000, app,47- use_reloader=True, use_debugger=True, use_evalex=True)35 49-36 Combining Applications37 ----------------------38 39 If you have entirely separated applications and you want them to work next3 unmodified lines43 are combined by the dispatcher middleware into a larger one that is44 dispatched based on prefix.45 46 For example you could have your main application run on ``/`` and your61-backend interface on ``/backend``::47+backend interface on ``/backend``.48 49+.. code-block:: python50+51 from werkzeug.middleware.dispatcher import DispatcherMiddleware52 from frontend_app import application as frontend53 from backend_app import application as backend54 21 unmodified lines76 77 The perfect level for abstraction in that regard is the WSGI layer. You78 write your own WSGI application that looks at the request that comes and79 delegates it to your Flask application. If that application does not92-exist yet, it is dynamically created and remembered::80+exist yet, it is dynamically created and remembered.81 82+.. code-block:: python83+84 from threading import Lock85 86 class SubdomainDispatcher:87 18 unmodified lines106 app = self.get_application(environ['HTTP_HOST'])107 return app(environ, start_response)108 109 120-This dispatcher can then be used like this::110+This dispatcher can then be used like this:111 112+.. code-block:: python113+114 from myapplication import create_app, get_user_for_subdomain115 from werkzeug.exceptions import NotFound116 117 def make_app(subdomain):16 unmodified lines134 ----------------135 136 Dispatching by a path on the URL is very similar. Instead of looking at137 the ``Host`` header to figure out the subdomain one simply looks at the146-request path up to the first slash::138+request path up to the first slash.139 140+.. code-block:: python141+142 from threading import Lock149- from werkzeug.wsgi import pop_path_info, peek_path_info143+ from wsgiref.util import shift_path_info144 145 class PathDispatcher:146 147 def __init__(self, default_app, create_app):11 unmodified lines159 self.instances[prefix] = app160 return app161 162 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)169 170+ def _peek_path_info(environ):171+ segments = environ.get("PATH_INFO", "").lstrip("/").split("/", 1)172+ if segments:173+ return segments[0]174+175+ return None176+177 The big difference between this and the subdomain one is that this one177-falls back to another application if the creator function returns ``None``::178+falls back to another application if the creator function returns ``None``.179 180+.. code-block:: python181+182 from myapplication import create_app, default_app, get_user_for_prefix183 184 def make_app(prefix):185 user = get_user_for_prefix(prefix)4 unmodified lines