Spaces:
Sleeping
Sleeping
Update proxy/proxy_server.py
Browse files- proxy/proxy_server.py +33 -10
proxy/proxy_server.py
CHANGED
|
@@ -12,10 +12,11 @@ API_BASE = "http://localhost:5556"
|
|
| 12 |
def index():
|
| 13 |
return send_from_directory("templates", "index.html")
|
| 14 |
|
| 15 |
-
# ---------- Reverse proxy to ConlluEditor ----------
|
|
|
|
| 16 |
@app.route("/editor/<path:path>", methods=["GET", "POST"])
|
| 17 |
-
@app.route("/editor", defaults={"path": ""}, methods=["GET", "POST"])
|
| 18 |
def proxy_editor(path):
|
|
|
|
| 19 |
url = f"{EDITOR_BASE}/{path}"
|
| 20 |
if request.query_string:
|
| 21 |
url += "?" + request.query_string.decode()
|
|
@@ -26,9 +27,11 @@ def proxy_editor(path):
|
|
| 26 |
r = requests.post(url, data=request.form or request.data,
|
| 27 |
headers=_filtered_headers(), stream=True)
|
| 28 |
|
| 29 |
-
return Response(
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# ---------- Reverse proxy to upload API ----------
|
| 34 |
@app.route("/api/<path:path>", methods=["GET", "POST"])
|
|
@@ -44,9 +47,30 @@ def proxy_api(path):
|
|
| 44 |
data=request.form or None,
|
| 45 |
headers=_filtered_headers(), stream=True)
|
| 46 |
|
| 47 |
-
return Response(
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def _filtered_headers():
|
| 52 |
excluded = {"host", "content-length", "content-encoding", "connection"}
|
|
@@ -54,8 +78,7 @@ def _filtered_headers():
|
|
| 54 |
|
| 55 |
def _proxied_response_headers(r):
|
| 56 |
excluded = {"content-encoding", "transfer-encoding", "connection"}
|
| 57 |
-
|
| 58 |
-
return headers
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
parser = argparse.ArgumentParser()
|
|
|
|
| 12 |
def index():
|
| 13 |
return send_from_directory("templates", "index.html")
|
| 14 |
|
| 15 |
+
# ---------- Reverse proxy to ConlluEditor root ----------
|
| 16 |
+
@app.route("/editor/", defaults={"path": ""}, methods=["GET", "POST"])
|
| 17 |
@app.route("/editor/<path:path>", methods=["GET", "POST"])
|
|
|
|
| 18 |
def proxy_editor(path):
|
| 19 |
+
# Always forward to ConlluEditor root
|
| 20 |
url = f"{EDITOR_BASE}/{path}"
|
| 21 |
if request.query_string:
|
| 22 |
url += "?" + request.query_string.decode()
|
|
|
|
| 27 |
r = requests.post(url, data=request.form or request.data,
|
| 28 |
headers=_filtered_headers(), stream=True)
|
| 29 |
|
| 30 |
+
return Response(
|
| 31 |
+
r.iter_content(chunk_size=8192),
|
| 32 |
+
status=r.status_code,
|
| 33 |
+
headers=_proxied_response_headers(r)
|
| 34 |
+
)
|
| 35 |
|
| 36 |
# ---------- Reverse proxy to upload API ----------
|
| 37 |
@app.route("/api/<path:path>", methods=["GET", "POST"])
|
|
|
|
| 47 |
data=request.form or None,
|
| 48 |
headers=_filtered_headers(), stream=True)
|
| 49 |
|
| 50 |
+
return Response(
|
| 51 |
+
r.iter_content(chunk_size=8192),
|
| 52 |
+
status=r.status_code,
|
| 53 |
+
headers=_proxied_response_headers(r)
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# ---------- Catch-all: static assets, JS, CSS, images ----------
|
| 57 |
+
@app.route("/<path:path>", methods=["GET", "POST"])
|
| 58 |
+
def proxy_static(path):
|
| 59 |
+
url = f"{EDITOR_BASE}/{path}"
|
| 60 |
+
if request.query_string:
|
| 61 |
+
url += "?" + request.query_string.decode()
|
| 62 |
+
|
| 63 |
+
if request.method == "GET":
|
| 64 |
+
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
| 65 |
+
else:
|
| 66 |
+
r = requests.post(url, data=request.form or request.data,
|
| 67 |
+
headers=_filtered_headers(), stream=True)
|
| 68 |
+
|
| 69 |
+
return Response(
|
| 70 |
+
r.iter_content(chunk_size=8192),
|
| 71 |
+
status=r.status_code,
|
| 72 |
+
headers=_proxied_response_headers(r)
|
| 73 |
+
)
|
| 74 |
|
| 75 |
def _filtered_headers():
|
| 76 |
excluded = {"host", "content-length", "content-encoding", "connection"}
|
|
|
|
| 78 |
|
| 79 |
def _proxied_response_headers(r):
|
| 80 |
excluded = {"content-encoding", "transfer-encoding", "connection"}
|
| 81 |
+
return [(k, v) for k, v in r.headers.items() if k.lower() not in excluded]
|
|
|
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
parser = argparse.ArgumentParser()
|