adding APPLICATION_ROOT to config + workaround for url mapping for non-root URL sitatuions

This commit is contained in:
manetta 2022-03-10 17:52:52 +01:00
parent cedf8058c3
commit 0a60da6fea
2 changed files with 27 additions and 4 deletions

View File

@ -1,4 +1,5 @@
class Config(object): class Config(object):
APPLICATION_ROOT = '/'
PORTNUMBER = 5001 PORTNUMBER = 5001
PAD_URL = 'https://pad.vvvvvvaria.org/' # with a slash in the end! PAD_URL = 'https://pad.vvvvvvaria.org/' # with a slash in the end!
PAD_API_URL = 'https://pad.vvvvvvaria.org/api/1.2.15/' PAD_API_URL = 'https://pad.vvvvvvaria.org/api/1.2.15/'

View File

@ -17,6 +17,28 @@ import markdown
APP = Flask(__name__) APP = Flask(__name__)
APP.config.from_object("config.Config") APP.config.from_object("config.Config")
# ---
# Workaround to map urls if the application runs from a non-root URL
# From: https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes
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.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=APP.config['APPLICATION_ROOT'])
# --- # ---
def get_pad_content(pad_name, ext=""): def get_pad_content(pad_name, ext=""):
@ -108,13 +130,13 @@ def index():
exts = ['.md', '.css'] exts = ['.md', '.css']
for ext in exts: for ext in exts:
create_pad_on_first_run(name, ext) create_pad_on_first_run(name, ext)
return redirect(f'/{ name }/') return redirect(url_for('pad', name=name))
else: else:
return render_template('start.html') return render_template('start.html')
@APP.route('/<name>/', methods=['GET']) @APP.route('/<name>/')
def main(name): def main(name):
return redirect(f'/{ name }/pad/') return redirect(url_for('pad', name=name))
@APP.route('/<name>/pad/') @APP.route('/<name>/pad/')
def pad(name): def pad(name):
@ -122,7 +144,7 @@ def pad(name):
url = os.path.join(APP.config['PAD_URL'], pad_name) url = os.path.join(APP.config['PAD_URL'], pad_name)
return render_template('iframe.html', url=url, name=name.strip()) return render_template('iframe.html', url=url, name=name.strip())
@APP.route('/<name>/stylesheet/', methods=['GET']) @APP.route('/<name>/stylesheet/')
def stylesheet(name): def stylesheet(name):
pad_name = f'{ name }.css' pad_name = f'{ name }.css'
url = os.path.join(APP.config['PAD_URL'], pad_name) url = os.path.join(APP.config['PAD_URL'], pad_name)