暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

log_stream_routes.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package routes
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "git.x2erp.com/qdy/go-base/authbase"
  8. "git.x2erp.com/qdy/go-base/webx"
  9. "git.x2erp.com/qdy/go-base/webx/router"
  10. "git.x2erp.com/qdy/go-svc-code/internal/opencode"
  11. )
  12. // RegisterLogStreamRoutes 注册日志流路由
  13. func RegisterLogStreamRoutes(ws *router.RouterService, webService *webx.WebService, process *opencode.Process, opencodePort int) {
  14. // 日志流需要直接处理 HTTP 流式响应,不能使用标准的路由包装
  15. // 我们直接注册到 webService 的底层路由器
  16. webService.GetRouter().Handle("/api/logs/stream", LogStreamHandler(process, opencodePort))
  17. }
  18. // LogStreamHandler 日志流的 HTTP 处理器(已包含TokenAuth认证)
  19. func LogStreamHandler(process *opencode.Process, opencodePort int) http.HandlerFunc {
  20. // 创建内部处理器
  21. handler := func(w http.ResponseWriter, r *http.Request) {
  22. fmt.Printf("🔍 [LogStreamHandler] 新日志流连接: %s %s\n", r.Method, r.URL.String())
  23. fmt.Printf("🔍 [LogStreamHandler] 查询参数: %v\n", r.URL.Query())
  24. // 设置 SSE 头
  25. w.Header().Set("Content-Type", "text/event-stream")
  26. w.Header().Set("Cache-Control", "no-cache")
  27. w.Header().Set("Connection", "keep-alive")
  28. w.Header().Set("Access-Control-Allow-Origin", "*")
  29. flusher, ok := w.(http.Flusher)
  30. if !ok {
  31. http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
  32. return
  33. }
  34. // 获取sessionId过滤参数
  35. sessionId := r.URL.Query().Get("sessionId")
  36. if sessionId != "" {
  37. // 记录日志过滤设置
  38. fmt.Fprintf(w, "data: 过滤日志,仅显示会话 %s 的日志\n\n", sessionId)
  39. flusher.Flush()
  40. }
  41. // 检查是否有可用的opencode进程
  42. if process == nil {
  43. // 外部opencode模式,提供模拟日志流
  44. fmt.Printf("🔍 [LogStreamHandler] 使用模拟日志流(外部opencode模式)\n")
  45. // 发送初始消息
  46. fmt.Fprintf(w, "data: 外部 opencode 模式,模拟日志流已连接\n\n")
  47. flusher.Flush()
  48. fmt.Fprintf(w, "data: opencode 服务运行在端口 %d\n\n", opencodePort)
  49. flusher.Flush()
  50. fmt.Fprintf(w, "data: 当前时间: %s\n\n", time.Now().Format("2006-01-02 15:04:05"))
  51. flusher.Flush()
  52. // 监听客户端断开连接
  53. for {
  54. select {
  55. case <-r.Context().Done():
  56. // 客户端断开连接
  57. return
  58. case <-time.After(30 * time.Second):
  59. // 发送心跳保持连接
  60. fmt.Fprintf(w, ": heartbeat\n\n")
  61. flusher.Flush()
  62. }
  63. }
  64. }
  65. // 获取日志通道
  66. logChan := process.GetLogs()
  67. if logChan == nil {
  68. http.Error(w, "日志通道不可用", http.StatusInternalServerError)
  69. return
  70. }
  71. // 发送初始消息
  72. fmt.Fprintf(w, "data: 连接到 opencode 日志流\n\n")
  73. flusher.Flush()
  74. // 监听日志通道
  75. for {
  76. select {
  77. case log, ok := <-logChan:
  78. if !ok {
  79. // 通道关闭
  80. fmt.Fprintf(w, "data: 日志流已结束\n\n")
  81. flusher.Flush()
  82. return
  83. }
  84. // 根据sessionId过滤日志
  85. if sessionId != "" && !strings.Contains(log, sessionId) {
  86. // 日志不包含sessionId,跳过
  87. if strings.Contains(log, "session") {
  88. fmt.Printf("🔍 [LogStreamHandler] 跳过不匹配的日志 (sessionId=%s): %s\n", sessionId, log)
  89. }
  90. continue
  91. }
  92. // 发送日志
  93. fmt.Printf("🔍 [LogStreamHandler] 发送日志: %s\n", log)
  94. fmt.Fprintf(w, "data: %s\n\n", log)
  95. flusher.Flush()
  96. case <-r.Context().Done():
  97. // 客户端断开连接
  98. return
  99. case <-time.After(30 * time.Second):
  100. // 发送心跳保持连接
  101. fmt.Fprintf(w, ": heartbeat\n\n")
  102. flusher.Flush()
  103. }
  104. }
  105. }
  106. // 包装TokenAuth中间件
  107. return authbase.TokenAuth(http.HandlerFunc(handler)).ServeHTTP
  108. }