- 拆分应用工厂、业务服务与路由,保留原有 HTTP/SSE 和存储入口 - 分离页面资源,重排客服工作台并完善响应式导航、草稿和附件交互 - 整理维护脚本、请求示例和贡献指南,移除启动脚本中的密钥配置 - 上传文件改为仅本地保留,补充隔离的后端与前端回归验证 验证:46 项 Python 测试通过,含 14 个已有子测试;12 个 Node 前端场景通过。
29 lines
914 B
Python
29 lines
914 B
Python
"""Request limiting and HTML/SSE cache headers with app-scoped state."""
|
|
|
|
import time
|
|
|
|
from flask import current_app, jsonify, request
|
|
|
|
|
|
def rate_limit():
|
|
path = request.path
|
|
if path.startswith('/static') or path.startswith('/api/human/'):
|
|
return None
|
|
ip = request.remote_addr or 'unknown'
|
|
now = time.time()
|
|
hits = current_app.extensions['rate_hits'][ip]
|
|
while hits and now - hits[0] > 60:
|
|
hits.pop(0)
|
|
if len(hits) >= current_app.config['RATE_LIMIT_PER_MIN']:
|
|
return jsonify({'e': '请求太频繁,请稍后几秒再试', 'd': 1}), 429
|
|
hits.append(now)
|
|
return None
|
|
|
|
|
|
def no_cache_html(response):
|
|
if response.mimetype in ('text/html', 'text/event-stream'):
|
|
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate'
|
|
response.headers['Pragma'] = 'no-cache'
|
|
response.headers['Expires'] = '0'
|
|
return response
|