Aleye commited on
Commit
31e85b5
·
verified ·
1 Parent(s): b9eff0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, Response
2
+ import requests
3
+ import os
4
+
5
+ app = Flask(__name__)
6
+
7
+
8
+ TARGET_DOMAIN = os.getenv("TARGET_API")
9
+ if not TARGET_DOMAIN:
10
+ raise ValueError("Missing environment variable: PROXY_TARGET_DOMAIN")
11
+
12
+ @app.route('/', defaults={'path': ''})
13
+ @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
14
+ def proxy(path):
15
+ try:
16
+ target_url = f"{TARGET_DOMAIN}/{path}"
17
+ headers = {key: value for (key, value) in request.headers if key.lower() not in ('host', 'content-length')}
18
+ headers['Accept-Encoding'] = 'identity' # 禁止压缩
19
+
20
+ resp = requests.request(
21
+ method=request.method,
22
+ url=target_url,
23
+ headers=headers,
24
+ data=request.get_data(),
25
+ cookies=request.cookies,
26
+ params=request.args,
27
+ stream=True
28
+ )
29
+
30
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
31
+ headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
32
+ headers.append(('Content-Type', 'application/json; charset=utf-8'))
33
+
34
+ return Response(resp.iter_content(chunk_size=1024), headers=headers, status=resp.status_code)
35
+
36
+ except Exception as e:
37
+ return str(e), 500
38
+
39
+
40
+
41
+
42
+ if __name__ == '__main__':
43
+ # 必须指定host和port
44
+ app.run(host='0.0.0.0', port=7860, debug=False)