INNER CODE UNIT · Python
SubpathMiddleware
giuseppe99barchetta/SuggestArr · api_service/app.py:39
class SubpathMiddleware:
"""
WSGI Middleware to strip the SUBPATH from the request URL
so Flask routing works correctly behind various reverse proxies.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
env_vars = load_env_vars()
subpath = env_vars.get('SUBPATH')
subpath = str(subpath).strip('/') if subpath else ''
if subpath and environ['PATH_INFO'].startswith(f'/{subpath}'):
# Strip the subpath from PATH_INFO.
# If the URL is exactly /<subpath> (no trailing slash), the result
# would be an empty string which is not a valid WSGI PATH_INFO.
# Fall back to '/' so Flask can match the root route.