Spaces:
Sleeping
Sleeping
Update proxy/proxy_server.py
Browse files- proxy/proxy_server.py +54 -12
proxy/proxy_server.py
CHANGED
|
@@ -7,16 +7,20 @@ app = Flask(__name__, static_folder="static", template_folder="templates")
|
|
| 7 |
EDITOR_BASE = "http://localhost:5555"
|
| 8 |
API_BASE = "http://localhost:5556"
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
| 11 |
@app.route("/")
|
| 12 |
def index():
|
| 13 |
return send_from_directory("templates", "index.html")
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|
|
@@ -24,8 +28,13 @@ def proxy_editor(path):
|
|
| 24 |
if request.method == "GET":
|
| 25 |
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
| 26 |
else:
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
return Response(
|
| 31 |
r.iter_content(chunk_size=8192),
|
|
@@ -33,7 +42,10 @@ def proxy_editor(path):
|
|
| 33 |
headers=_proxied_response_headers(r)
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.route("/api/<path:path>", methods=["GET", "POST"])
|
| 38 |
def proxy_api(path):
|
| 39 |
url = f"{API_BASE}/api/{path}"
|
|
@@ -42,10 +54,24 @@ def proxy_api(path):
|
|
| 42 |
|
| 43 |
if request.method == "GET":
|
| 44 |
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
| 45 |
-
else:
|
| 46 |
-
files = {name: (f.filename, f.stream, f.mimetype) for name, f in request.files.items()}
|
| 47 |
-
r = requests.post(url, files=files, headers=_filtered_headers())
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
return Response(
|
| 51 |
r.iter_content(chunk_size=8192),
|
|
@@ -53,7 +79,10 @@ def proxy_api(path):
|
|
| 53 |
headers=_proxied_response_headers(r)
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.route("/<path:path>", methods=["GET", "POST"])
|
| 58 |
def proxy_static(path):
|
| 59 |
url = f"{EDITOR_BASE}/{path}"
|
|
@@ -63,8 +92,12 @@ def proxy_static(path):
|
|
| 63 |
if request.method == "GET":
|
| 64 |
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
| 65 |
else:
|
| 66 |
-
r = requests.post(
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
return Response(
|
| 70 |
r.iter_content(chunk_size=8192),
|
|
@@ -72,14 +105,23 @@ def proxy_static(path):
|
|
| 72 |
headers=_proxied_response_headers(r)
|
| 73 |
)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def _filtered_headers():
|
| 76 |
excluded = {"host", "content-length", "content-encoding", "connection"}
|
| 77 |
return {k: v for k, v in request.headers.items() if k.lower() not in excluded}
|
| 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()
|
| 85 |
parser.add_argument("--port", type=int, default=7860)
|
|
|
|
| 7 |
EDITOR_BASE = "http://localhost:5555"
|
| 8 |
API_BASE = "http://localhost:5556"
|
| 9 |
|
| 10 |
+
# ---------------------------------------------------------
|
| 11 |
+
# 1. Combined UI page
|
| 12 |
+
# ---------------------------------------------------------
|
| 13 |
@app.route("/")
|
| 14 |
def index():
|
| 15 |
return send_from_directory("templates", "index.html")
|
| 16 |
|
| 17 |
+
|
| 18 |
+
# ---------------------------------------------------------
|
| 19 |
+
# 2. Reverse proxy to ConlluEditor (root + all subpaths)
|
| 20 |
+
# ---------------------------------------------------------
|
| 21 |
@app.route("/editor/", defaults={"path": ""}, methods=["GET", "POST"])
|
| 22 |
@app.route("/editor/<path:path>", methods=["GET", "POST"])
|
| 23 |
def proxy_editor(path):
|
|
|
|
| 24 |
url = f"{EDITOR_BASE}/{path}"
|
| 25 |
if request.query_string:
|
| 26 |
url += "?" + request.query_string.decode()
|
|
|
|
| 28 |
if request.method == "GET":
|
| 29 |
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
| 30 |
else:
|
| 31 |
+
# forward POST body
|
| 32 |
+
r = requests.post(
|
| 33 |
+
url,
|
| 34 |
+
data=request.get_data(),
|
| 35 |
+
headers=_filtered_headers(),
|
| 36 |
+
stream=True
|
| 37 |
+
)
|
| 38 |
|
| 39 |
return Response(
|
| 40 |
r.iter_content(chunk_size=8192),
|
|
|
|
| 42 |
headers=_proxied_response_headers(r)
|
| 43 |
)
|
| 44 |
|
| 45 |
+
|
| 46 |
+
# ---------------------------------------------------------
|
| 47 |
+
# 3. Reverse proxy to Upload API (upload, files, download)
|
| 48 |
+
# ---------------------------------------------------------
|
| 49 |
@app.route("/api/<path:path>", methods=["GET", "POST"])
|
| 50 |
def proxy_api(path):
|
| 51 |
url = f"{API_BASE}/api/{path}"
|
|
|
|
| 54 |
|
| 55 |
if request.method == "GET":
|
| 56 |
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
else:
|
| 59 |
+
# Correctly forward uploaded files
|
| 60 |
+
files = {
|
| 61 |
+
name: (f.filename, f.stream, f.mimetype)
|
| 62 |
+
for name, f in request.files.items()
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# Forward form fields too
|
| 66 |
+
data = request.form.to_dict(flat=True)
|
| 67 |
+
|
| 68 |
+
r = requests.post(
|
| 69 |
+
url,
|
| 70 |
+
files=files if files else None,
|
| 71 |
+
data=data if data else None,
|
| 72 |
+
headers=_filtered_headers(),
|
| 73 |
+
stream=True
|
| 74 |
+
)
|
| 75 |
|
| 76 |
return Response(
|
| 77 |
r.iter_content(chunk_size=8192),
|
|
|
|
| 79 |
headers=_proxied_response_headers(r)
|
| 80 |
)
|
| 81 |
|
| 82 |
+
|
| 83 |
+
# ---------------------------------------------------------
|
| 84 |
+
# 4. Catch‑all: static assets for ConlluEditor
|
| 85 |
+
# ---------------------------------------------------------
|
| 86 |
@app.route("/<path:path>", methods=["GET", "POST"])
|
| 87 |
def proxy_static(path):
|
| 88 |
url = f"{EDITOR_BASE}/{path}"
|
|
|
|
| 92 |
if request.method == "GET":
|
| 93 |
r = requests.get(url, headers=_filtered_headers(), stream=True)
|
| 94 |
else:
|
| 95 |
+
r = requests.post(
|
| 96 |
+
url,
|
| 97 |
+
data=request.get_data(),
|
| 98 |
+
headers=_filtered_headers(),
|
| 99 |
+
stream=True
|
| 100 |
+
)
|
| 101 |
|
| 102 |
return Response(
|
| 103 |
r.iter_content(chunk_size=8192),
|
|
|
|
| 105 |
headers=_proxied_response_headers(r)
|
| 106 |
)
|
| 107 |
|
| 108 |
+
|
| 109 |
+
# ---------------------------------------------------------
|
| 110 |
+
# Header filtering helpers
|
| 111 |
+
# ---------------------------------------------------------
|
| 112 |
def _filtered_headers():
|
| 113 |
excluded = {"host", "content-length", "content-encoding", "connection"}
|
| 114 |
return {k: v for k, v in request.headers.items() if k.lower() not in excluded}
|
| 115 |
|
| 116 |
+
|
| 117 |
def _proxied_response_headers(r):
|
| 118 |
excluded = {"content-encoding", "transfer-encoding", "connection"}
|
| 119 |
return [(k, v) for k, v in r.headers.items() if k.lower() not in excluded]
|
| 120 |
|
| 121 |
+
|
| 122 |
+
# ---------------------------------------------------------
|
| 123 |
+
# Main entry
|
| 124 |
+
# ---------------------------------------------------------
|
| 125 |
if __name__ == "__main__":
|
| 126 |
parser = argparse.ArgumentParser()
|
| 127 |
parser.add_argument("--port", type=int, default=7860)
|