Skip to content

Commit

Permalink
update dispatch-by-path example
Browse files Browse the repository at this point in the history
  • Loading branch information
pavithra-m13 authored and davidism committed Aug 16, 2023
1 parent 826514b commit aa6d4c3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions docs/patterns/appdispatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ the ``Host`` header to figure out the subdomain one simply looks at the
request path up to the first slash::

from threading import Lock
from werkzeug.wsgi import pop_path_info, peek_path_info
from wsgiref.util import shift_path_info

class PathDispatcher:

Expand All @@ -166,13 +166,20 @@ request path up to the first slash::
return app

def __call__(self, environ, start_response):
app = self.get_application(peek_path_info(environ))
app = self.get_application(self._peek_path_info(environ))
if app is not None:
pop_path_info(environ)
shift_path_info(environ)
else:
app = self.default_app
return app(environ, start_response)

def _peek_path_info(environ):
segments = environ.get("PATH_INFO", "").lstrip("/").split("/", 1)
if segments:
return segments[0]

return None

The big difference between this and the subdomain one is that this one
falls back to another application if the creator function returns ``None``::

Expand Down

0 comments on commit aa6d4c3

Please sign in to comment.