Created
December 2, 2020 05:48
-
-
Save JeffCarpenter/3c5f3c582fdf96210e6cd43fab3bab13 to your computer and use it in GitHub Desktop.
Serve Flask from a non-root path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FlaskSubpath(Flask): | |
def __init__(self, *args, application_root="/", **kwargs): | |
super().__init__(*args, **kwargs) | |
self.application_root = application_root | |
def wsgi_app(self, environ, start_response): | |
if environ["PATH_INFO"].startswith(self.application_root): | |
environ["PATH_INFO"] = environ["PATH_INFO"][len(self.application_root):] | |
environ["SCRIPT_NAME"] = self.application_root | |
elif "HTTP_FORWARDED_REQUEST_URI" in environ and environ["HTTP_FORWARDED_REQUEST_URI"].startswith(self.application_root): | |
environ["SCRIPT_NAME"] = self.application_root | |
return super().wsgi_app(environ, start_response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment