izuemon commited on
Commit
6bf16e3
·
verified ·
1 Parent(s): 2519d7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py CHANGED
@@ -47,6 +47,60 @@ def get_managers():
47
  # managersの部分だけ返す
48
  return jsonify(json_data.get("managers", []))
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  if __name__ == "__main__":
52
  app.run(debug=True, host="0.0.0.0", port=7860)
 
47
  # managersの部分だけ返す
48
  return jsonify(json_data.get("managers", []))
49
 
50
+ @app.route("/cors-proxy")
51
+ def corsproxy():
52
+ url = request.args.get("url")
53
+ if not url:
54
+ return "url パラメータが必要です", 400
55
+
56
+ # URLが http/https のみ許可(安全のため)
57
+ if not url.startswith("http://") and not url.startswith("https://"):
58
+ return "http または https のURLのみ使用できます", 400
59
+
60
+ try:
61
+ # 元URLへ転送
62
+ resp = requests.get(url, headers=request.headers, timeout=10)
63
+
64
+ # CORS対応レスポンスヘッダを付与
65
+ headers = {
66
+ "Access-Control-Allow-Origin": "*",
67
+ "Access-Control-Allow-Headers": "*",
68
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS"
69
+ }
70
+
71
+ return Response(resp.content, resp.status_code, headers=headers)
72
+
73
+ except Exception as e:
74
+ return f"エラー: {str(e)}", 500
75
+
76
+
77
+ @app.route("/cors-proxy", methods=["POST"])
78
+ def corsproxy_post():
79
+ url = request.args.get("url")
80
+ if not url:
81
+ return "url パラメータが必要です", 400
82
+
83
+ if not url.startswith("http://") and not url.startswith("https://"):
84
+ return "http または https のURLのみ使用できます", 400
85
+
86
+ try:
87
+ resp = requests.post(
88
+ url,
89
+ data=request.data,
90
+ headers=request.headers,
91
+ timeout=10
92
+ )
93
+
94
+ headers = {
95
+ "Access-Control-Allow-Origin": "*",
96
+ "Access-Control-Allow-Headers": "*",
97
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS"
98
+ }
99
+
100
+ return Response(resp.content, resp.status_code, headers=headers)
101
+
102
+ except Exception as e:
103
+ return f"エラー: {str(e)}", 500
104
 
105
  if __name__ == "__main__":
106
  app.run(debug=True, host="0.0.0.0", port=7860)