暂提更新

This commit is contained in:
super
2026-03-22 11:35:45 +08:00
parent 497d65d41d
commit cfbd5d6e84
7 changed files with 86 additions and 41 deletions

View File

@@ -2,8 +2,7 @@
主页面蓝图首页、home、图片工作台、品牌页、静态文件、Logo
"""
import os
import requests
from flask import Blueprint, Response, request, send_file
from flask import Blueprint, send_file
from app_common import (
get_db,
@@ -16,10 +15,10 @@ from app_common import (
ASSETS_DIR
)
from flask import redirect, url_for, session
from flask import Flask, request, Response, stream_with_context
import requests
from config import base_url,version,java_api_base
JAVA_API_BASE = java_api_base
from config import base_url,version,JAVA_API_BASE
main_bp = Blueprint('main', __name__)
@@ -93,36 +92,60 @@ def serve_new_web_source(filename):
@main_bp.route('/newApi/<path:subpath>', methods=['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'])
def proxy_new_api(subpath):
target_url = f"{JAVA_API_BASE}/{subpath}"
try:
headers = {
key: value
for key, value in request.headers.items()
if key.lower() not in {'host', 'content-length'}
}
upstream = requests.request(
method=request.method,
url=target_url,
params=request.args,
data=request.get_data(),
headers=headers,
cookies=request.cookies,
allow_redirects=False,
timeout=300,
)
excluded_headers = {'content-encoding', 'content-length', 'transfer-encoding', 'connection'}
response_headers = [
(name, value)
for name, value in upstream.headers.items()
if name.lower() not in excluded_headers
]
return Response(upstream.content, upstream.status_code, response_headers)
except requests.RequestException as exc:
return {'success': False, 'message': str(exc), 'data': None}, 502
@main_bp.route('/logo.jpg', methods=['GET'])
def get_logo_image():
return send_file(os.path.join(BASE_DIR, "logo.jpg"), mimetype='image/jpeg')
@main_bp.route('/newApi/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(path):
target_url = f"{JAVA_API_BASE}/{path}"
# 复制请求参数
params = request.args.to_dict()
# 处理请求头
headers = {}
for key, value in request.headers:
if key.lower() in ['host', 'content-length', 'connection']:
continue
headers[key] = value
try:
# 使用流式请求
req = requests.request(
method=request.method,
url=target_url,
params=params,
headers=headers,
data=request.get_data() if request.get_data() else None,
cookies=request.cookies,
stream=True, # 启用流式传输
timeout=30
)
# 流式响应
def generate():
for chunk in req.iter_content(chunk_size=8192):
if chunk:
yield chunk
# 构建响应
response = Response(stream_with_context(generate()), status=req.status_code)
# 复制响应头
for key, value in req.headers.items():
if key.lower() not in ['content-encoding', 'content-length', 'transfer-encoding', 'connection']:
response.headers[key] = value
return response
except requests.exceptions.Timeout:
print("后端服务超时")
return Response("后端服务超时", status=504)
except requests.exceptions.ConnectionError:
print("无法连接到后端服务")
return Response("无法连接到后端服务", status=502)
except Exception as e:
print(f"代理请求失败: {e}")
return Response(f"代理错误: {str(e)}", status=500)