Skip to content

Instantly share code, notes, and snippets.

@yakneens
Created September 15, 2017 18:45
Show Gist options
  • Save yakneens/2e419127f24a3c57f173382b3e8fd075 to your computer and use it in GitHub Desktop.
Save yakneens/2e419127f24a3c57f173382b3e8fd075 to your computer and use it in GitHub Desktop.
...
class URLPrefixMiddleware(object):
def __init__(self, app, prefix=''):
self.app = app
self.prefix = prefix
def __call__(self, environ, start_response):
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()]
def create_app(config=None):
app = Flask(__name__)
app.wsgi_app = URLPrefixMiddleware(app.wsgi_app, prefix=str(configuration.get("webserver", "URL_PREFIX")))
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment