Created
March 18, 2017 18:57
-
-
Save m4ce/9fbd438007950135fa54184f60572667 to your computer and use it in GitHub Desktop.
Prefix flask application app root url
This file contains hidden or 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 PrefixMiddleware(object): | |
def __init__(self, app, prefix=''): | |
self.app = app | |
self.prefix = prefix | |
def __call__(self, environ, start_response): | |
print(environ['PATH_INFO']) | |
if environ['PATH_INFO'].startswith(self.prefix): | |
environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] | |
environ['SCRIPT_NAME'] = self.prefix | |
return self.app(environ, start_response) | |
else: | |
start_response('404', [('Content-Type', 'text/plain')]) | |
return ["This url does not belong to the app.".encode()] | |
from flask_app.app import app | |
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/prefix') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment