1
0
forked from erp-dev/erp

feat: disabled sse module and added health check to api_v2 for caddy2

This commit is contained in:
2025-12-27 09:04:12 +08:00
parent 67698dfa71
commit 69c3daa919
5 changed files with 103 additions and 48 deletions

View File

@@ -29,61 +29,63 @@ def create_sse_event(request):
- 需要JWT认证且用户必须有关联的商户
"""
# 处理 OPTIONS 预检请求
if request.method == 'OPTIONS':
response = HttpResponse()
origin = request.META.get('HTTP_ORIGIN')
if origin:
response['Access-Control-Allow-Origin'] = origin
response['Access-Control-Allow-Methods'] = 'GET, OPTIONS'
response['Access-Control-Allow-Headers'] = 'authorization, content-type, cache-control, accept'
response['Access-Control-Allow-Credentials'] = 'true'
response['Access-Control-Max-Age'] = '86400' # 24小时
return response
# if request.method == 'OPTIONS':
# response = HttpResponse()
# origin = request.META.get('HTTP_ORIGIN')
# if origin:
# response['Access-Control-Allow-Origin'] = origin
# response['Access-Control-Allow-Methods'] = 'GET, OPTIONS'
# response['Access-Control-Allow-Headers'] = 'authorization, content-type, cache-control, accept'
# response['Access-Control-Allow-Credentials'] = 'true'
# response['Access-Control-Max-Age'] = '86400' # 24小时
# return response
def event_stream():
# 获取当前商户ID
merchant_id = request.merchant_id
# def event_stream():
# # 获取当前商户ID
# merchant_id = request.merchant_id
# 创建一个同步队列用于接收消息
conn_queue = queue.Queue(maxsize=100)
services.push_connection(merchant_id, conn_queue)
# # 创建一个同步队列用于接收消息
# conn_queue = queue.Queue(maxsize=100)
# services.push_connection(merchant_id, conn_queue)
try:
# 发送初始连接成功消息
yield f"data: {json.dumps({'type': 'connected', 'message': 'SSE connection established', 'merchant_id': merchant_id})}\n\n"
# try:
# # 发送初始连接成功消息
# yield f"data: {json.dumps({'type': 'connected', 'message': 'SSE connection established', 'merchant_id': merchant_id})}\n\n"
# 持续从队列中获取消息并发送给客户端
while True:
try:
# 使用同步方式等待新消息带超时30秒以便发送心跳
message = conn_queue.get(timeout=30.0)
yield f"data: {json.dumps(message)}\n\n"
except queue.Empty:
# 30秒超时发送心跳保持连接活跃
yield f": heartbeat\n\n"
except Exception as e:
print(f"Error in SSE stream: {e}")
break
finally:
# 清理:从连接集合中移除此队列
services.remove_connection(merchant_id, conn_queue)
# # 持续从队列中获取消息并发送给客户端
# while True:
# try:
# # 使用同步方式等待新消息带超时30秒以便发送心跳
# message = conn_queue.get(timeout=30.0)
# yield f"data: {json.dumps(message)}\n\n"
# except queue.Empty:
# # 30秒超时发送心跳保持连接活跃
# yield f": heartbeat\n\n"
# except Exception as e:
# print(f"Error in SSE stream: {e}")
# break
# finally:
# # 清理:从连接集合中移除此队列
# services.remove_connection(merchant_id, conn_queue)
response = StreamingHttpResponse(
event_stream(),
content_type='text/event-stream',
)
# response = StreamingHttpResponse(
# event_stream(),
# content_type='text/event-stream',
# )
# SSE 必需的响应头
response['Cache-Control'] = 'no-cache'
response['X-Accel-Buffering'] = 'no' # 禁用 nginx 缓冲
# CORS 响应头django-cors-headers 中间件会自动添加,但我们显式设置以确保)
# 如果请求带有 Origin 头,手动添加 CORS 响应头
origin = request.META.get('HTTP_ORIGIN')
if origin:
response['Access-Control-Allow-Origin'] = origin
response['Access-Control-Allow-Credentials'] = 'true'
# # SSE 必需的响应头
# response['Cache-Control'] = 'no-cache'
# response['X-Accel-Buffering'] = 'no' # 禁用 nginx 缓冲
# # CORS 响应头django-cors-headers 中间件会自动添加,但我们显式设置以确保)
# # 如果请求带有 Origin 头,手动添加 CORS 响应头
# origin = request.META.get('HTTP_ORIGIN')
# if origin:
# response['Access-Control-Allow-Origin'] = origin
# response['Access-Control-Allow-Credentials'] = 'true'
response = HttpResponse()
response.content = 'not allowed'
response.status_code = 405
return response