From 277baf8767f2550716fc03010a7ecb9e07530ab9 Mon Sep 17 00:00:00 2001 From: supisara Date: Fri, 9 Dec 2022 23:36:26 +0100 Subject: [PATCH] url prefix --- diyry.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/diyry.py b/diyry.py index 68a01cf..0d26557 100644 --- a/diyry.py +++ b/diyry.py @@ -14,7 +14,24 @@ def get_content(file): content = f.read() return markdown(content) +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()] + app = Flask(__name__) +# register the middleware to prefix all the requests with our base_url +app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/diyry') @app.route("/") def home(): @@ -34,4 +51,4 @@ def issue(slug): def file(slug, file): return send_from_directory(f"issues/{slug}", file) -app.run(port=3000, debug=True) \ No newline at end of file +app.run(port=3152, debug=True) \ No newline at end of file