]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/werkzeug/middleware/dispatcher.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / werkzeug / middleware / dispatcher.py
1 """
2 Application Dispatcher
3 ======================
4
5 This middleware creates a single WSGI application that dispatches to
6 multiple other WSGI applications mounted at different URL paths.
7
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
12 path besides "/api".
13
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
16 requests::
17
18 app = DispatcherMiddleware(serve_frontend, {
19 '/api': api_app,
20 '/admin': admin_app,
21 })
22
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.
27
28 .. autoclass:: DispatcherMiddleware
29
30 :copyright: 2007 Pallets
31 :license: BSD-3-Clause
32 """
33 from __future__ import annotations
34
35 import typing as t
36
37 if t.TYPE_CHECKING:
38 from _typeshed.wsgi import StartResponse
39 from _typeshed.wsgi import WSGIApplication
40 from _typeshed.wsgi import WSGIEnvironment
41
42
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
46 mounted under.
47
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.
51 """
52
53 def __init__(
54 self,
55 app: WSGIApplication,
56 mounts: dict[str, WSGIApplication] | None = None,
57 ) -> None:
58 self.app = app
59 self.mounts = mounts or {}
60
61 def __call__(
62 self, environ: WSGIEnvironment, start_response: StartResponse
63 ) -> t.Iterable[bytes]:
64 script = environ.get("PATH_INFO", "")
65 path_info = ""
66
67 while "/" in script:
68 if script in self.mounts:
69 app = self.mounts[script]
70 break
71
72 script, last_item = script.rsplit("/", 1)
73 path_info = f"/{last_item}{path_info}"
74 else:
75 app = self.mounts.get(script, self.app)
76
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)