Simplify webapp and handle both local dev and deployed URLs

This commit is contained in:
Tom 2025-02-03 10:21:06 +00:00
parent 3dba4eaa5e
commit fcdf4e0d51
4 changed files with 15 additions and 30 deletions

View File

@ -1,13 +1,11 @@
import os
from flask import (
Flask,
render_template,
request,
redirect,
Response,
)
import requests
from flask_cors import CORS
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
@ -28,28 +26,5 @@ config = {}
@app.route("/")
def index():
return render_template("index.html", request = request, config = config)
# @app.route('/stac', methods=["GET", "POST"]) # ref. https://medium.com/@zwork101/making-a-flask-proxy-server-online-in-10-lines-of-code-44b8721bca6
# def redirect_to_API_HOST(): #NOTE var :subpath will be unused as all path we need will be read from :request ie from flask import request
# url = f'http://localhost:8124/stac'
# res = requests.request( # ref. https://stackoverflow.com/a/36601467/248616
# method = request.method,
# url = url,
# headers = {k:v for k,v in request.headers if k.lower() != 'host'}, # exclude 'host' header
# data = request.get_data(),
# cookies = request.cookies,
# allow_redirects = False,
# )
# excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] #NOTE we here exclude all "hop-by-hop headers" defined by RFC 2616 section 13.5.1 ref. https://www.rfc-editor.org/rfc/rfc2616#section-13.5.1
# headers = [
# (k,v) for k,v in res.raw.headers.items()
# if k.lower() not in excluded_headers
# ]
# response = Response(res.content, res.status_code, headers)
# return response
return render_template("index.html", request = request, config = config, api_url = os.environ.get("API_URL", "/api/stac"))

View File

@ -1 +1,2 @@
export API_URL="http://127.0.0.1:8124/api/stac"
flask run --debug --port=5006

View File

@ -5,8 +5,14 @@ function getSTACUrlFromQuery() {
const params = new URLSearchParams(window.location.search);
// get current window url and remove path part
let api_url = new URL(window.location.href);
api_url.pathname = "/api/stac";
if (window.API_URL.startsWith("http")) {
// Absolute URL: Use it directly
api_url = new URL(window.API_URL);
} else {
// Relative URL: Combine with the current window's location
api_url = new URL(window.location.href);
api_url.pathname = window.API_URL;
}
for (const [key, value] of params.entries()) {
api_url.searchParams.set(key, value);

View File

@ -53,6 +53,9 @@
</div>
</div>
<script>
window.API_URL = "{{ api_url }}";
</script>
<script src="/static/app.js"></script>
</body>
</html>