prefix and environ

master
km0 3 years ago
parent f63bb99075
commit 7ce663f2b6

@ -1,10 +1,17 @@
import os
from flask import Flask, render_template from flask import Flask, render_template
from flask_sock import Sock from flask_sock import Sock
from . import prefix
app = Flask(__name__) app = Flask(__name__, )
sock = Sock(app) sock = Sock(app)
app.wsgi_app = prefix.PrefixMiddleware(
app.wsgi_app, prefix=os.environ.get("URL_PREFIX", "")
)
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html') return render_template('index.html')

@ -0,0 +1,14 @@
class PrefixMiddleware(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()]
Loading…
Cancel
Save