From 7ccad2799efce0c966bfcde2df75e46252f2c89c Mon Sep 17 00:00:00 2001 From: Ben Busby <33362396+benbusby@users.noreply.github.com> Date: Sun, 10 May 2020 13:27:02 -0600 Subject: [PATCH] Added config option to address instance behind reverse proxy Config options now allow setting a "root url", which defaults to the request url root. Saving a new url in this field will allow for proper redirects and usage of the opensearch element. Also provides a possible solution for #17, where the default flask redirect method redirects to http instead of https. --- app/routes.py | 22 +++++++++++++++------- app/static/css/main.css | 4 ++++ app/static/js/controller.js | 19 ++++++------------- app/templates/index.html | 4 ++++ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/app/routes.py b/app/routes.py index 5091cfc..09c39a8 100644 --- a/app/routes.py +++ b/app/routes.py @@ -19,12 +19,17 @@ CONFIG_PATH = app.config['STATIC_FOLDER'] + '/config.json' @app.before_request def before_request_func(): g.user_request = Request(request.headers.get('User-Agent')) - g.user_config = json.load(open(CONFIG_PATH)) if os.path.exists(CONFIG_PATH) else {} + g.user_config = json.load(open(CONFIG_PATH)) if os.path.exists(CONFIG_PATH) else {'url': request.url_root} + + if 'url' not in g.user_config or not g.user_config['url']: + g.user_config['url'] = request.url_root + + g.app_location = g.user_config['url'] @app.errorhandler(404) def unknown_page(e): - return redirect('/') + return redirect(g.app_location) @app.route('/', methods=['GET']) @@ -35,11 +40,11 @@ def index(): @app.route('/opensearch.xml', methods=['GET']) def opensearch(): - url_root = request.url_root - if url_root.endswith('/'): - url_root = url_root[:-1] + opensearch_url = g.app_location + if opensearch_url.endswith('/'): + opensearch_url = opensearch_url[:-1] - template = render_template('opensearch.xml', main_url=url_root) + template = render_template('opensearch.xml', main_url=opensearch_url) response = make_response(template) response.headers['Content-Type'] = 'application/xml' return response @@ -78,11 +83,14 @@ def config(): return json.dumps(g.user_config) else: config_data = request.form.to_dict() + if 'url' not in config_data or not config_data['url']: + config_data['url'] = request.url_root + with open(app.config['STATIC_FOLDER'] + '/config.json', 'w') as config_file: config_file.write(json.dumps(config_data, indent=4)) config_file.close() - return redirect('/') + return redirect(config_data['url']) @app.route('/url', methods=['GET']) diff --git a/app/static/css/main.css b/app/static/css/main.css index df6be12..f482373 100644 --- a/app/static/css/main.css +++ b/app/static/css/main.css @@ -113,3 +113,7 @@ button::-moz-focus-inner { -webkit-box-decoration-break: clone; box-decoration-break: clone; } + +.hidden { + display: none; +} diff --git a/app/static/js/controller.js b/app/static/js/controller.js index fed98d8..acf16af 100644 --- a/app/static/js/controller.js +++ b/app/static/js/controller.js @@ -15,7 +15,7 @@ const setupSearchLayout = () => { }); } -const fillConfigValues = (near, nojs, dark) => { +const fillConfigValues = (near, nojs, dark, url) => { // Request existing config info let xhrGET = new XMLHttpRequest(); xhrGET.open("GET", "/config"); @@ -29,19 +29,11 @@ const fillConfigValues = (near, nojs, dark) => { let configSettings = JSON.parse(xhrGET.responseText); near.value = configSettings["near"] ? configSettings["near"] : ""; - near.addEventListener("keyup", function() { - configSettings["near"] = near.value; - }); - nojs.checked = !!configSettings["nojs"]; - nojs.addEventListener("change", function() { - configSettings["nojs"] = nojs.checked ? 1 : 0; - }); - dark.checked = !!configSettings["dark"]; - dark.addEventListener("change", function() { - configSettings["dark"] = dark.checked ? 1 : 0; - }); + + // Addresses the issue of incorrect URL being used behind reverse proxy + url.value = configSettings["url"] ? configSettings["url"] : ""; }; xhrGET.send(); @@ -65,8 +57,9 @@ const setupConfigLayout = () => { const near = document.getElementById("config-near"); const noJS = document.getElementById("config-nojs"); const dark = document.getElementById("config-dark"); + const url = document.getElementById("config-url"); - fillConfigValues(near, noJS, dark); + fillConfigValues(near, noJS, dark, url); } document.addEventListener("DOMContentLoaded", function() { diff --git a/app/templates/index.html b/app/templates/index.html index a221e49..b6c12fd 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -53,6 +53,10 @@ +