]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/werkzeug/middleware/dispatcher.py
5 This middleware creates a single WSGI application that dispatches to
6 multiple other WSGI applications mounted at different URL paths.
8 A common example is writing a Single Page Application, where you have a
9 backend API and a frontend written in JavaScript that does the routing
10 in the browser rather than requesting different pages from the server.
11 The frontend is a single HTML and JS file that should be served for any
14 This example dispatches to an API app under "/api", an admin app
15 under "/admin", and an app that serves frontend files for all other
18 app = DispatcherMiddleware(serve_frontend, {
23 In production, you might instead handle this at the HTTP server level,
24 serving files or proxying to application servers based on location. The
25 API and admin apps would each be deployed with a separate WSGI server,
26 and the static files would be served directly by the HTTP server.
28 .. autoclass:: DispatcherMiddleware
30 :copyright: 2007 Pallets
31 :license: BSD-3-Clause
33 from __future__
import annotations
38 from _typeshed
.wsgi
import StartResponse
39 from _typeshed
.wsgi
import WSGIApplication
40 from _typeshed
.wsgi
import WSGIEnvironment
43 class DispatcherMiddleware
:
44 """Combine multiple applications as a single WSGI application.
45 Requests are dispatched to an application based on the path it is
48 :param app: The WSGI application to dispatch to if the request
49 doesn't match a mounted path.
50 :param mounts: Maps path prefixes to applications for dispatching.
56 mounts
: dict[str, WSGIApplication
] |
None = None,
59 self
.mounts
= mounts
or {}
62 self
, environ
: WSGIEnvironment
, start_response
: StartResponse
63 ) -> t
.Iterable
[bytes]:
64 script
= environ
.get("PATH_INFO", "")
68 if script
in self
.mounts
:
69 app
= self
.mounts
[script
]
72 script
, last_item
= script
.rsplit("/", 1)
73 path_info
= f
"/{last_item}{path_info}"
75 app
= self
.mounts
.get(script
, self
.app
)
77 original_script_name
= environ
.get("SCRIPT_NAME", "")
78 environ
["SCRIPT_NAME"] = original_script_name
+ script
79 environ
["PATH_INFO"] = path_info
80 return app(environ
, start_response
)