From 2954ab5fc57aa6e1de3577195783cf4930fab752 Mon Sep 17 00:00:00 2001 From: fuzhongyun <15339891972@163.com> Date: Fri, 26 Jun 2026 11:07:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(router):=20=E4=BF=AE=E5=A4=8DSC=E9=93=B6?= =?UTF-8?q?=E8=A1=8C=E8=B5=84=E8=B4=A8=E6=A0=A1=E9=AA=8C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=9A=84=E8=AF=B7=E6=B1=82=E4=BD=93=E8=BD=AC=E5=8F=91=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=B9=B6=E6=B7=BB=E5=8A=A0=E8=AF=A6=E7=BB=86=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增formatHeaders工具函数以格式化日志中的HTTP头 预先读取完整请求体到缓冲区,修复重复读取请求流导致的转发body为空问题 完善转发日志,新增打印请求查询参数、请求头、请求体、响应头和响应体 --- internal/server/router/router.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/server/router/router.go b/internal/server/router/router.go index 8e12bd7..efeec7d 100644 --- a/internal/server/router/router.go +++ b/internal/server/router/router.go @@ -4,6 +4,7 @@ import ( errors "ai_scheduler/internal/data/error" "ai_scheduler/internal/gateway" "ai_scheduler/internal/services" + "bytes" "encoding/json" "fmt" "io" @@ -158,11 +159,14 @@ func forwardToPythonService(c *fiber.Ctx) error { } func forwardScbankQualification(c *fiber.Ctx) error { + requestBody := c.Body() + requestQuery := c.Context().QueryArgs().String() + // 调用外部权益接口前记录目标地址与关键请求信息,便于排查白名单和超时问题。 startAt := time.Now() - log.Printf("开始转发权益校验请求 | method=%s | path=%s | target=%s", c.Method(), c.Path(), scbankQualificationProxyURL) + log.Printf("开始转发权益校验请求 | method=%s | path=%s | target=%s | query=%s | headers=%s | body=%s", c.Method(), c.Path(), scbankQualificationProxyURL, requestQuery, formatHeaders(c.GetReqHeaders()), string(requestBody)) - req, err := http.NewRequestWithContext(c.Context(), c.Method(), scbankQualificationProxyURL, c.Context().Request.BodyStream()) + req, err := http.NewRequestWithContext(c.Context(), c.Method(), scbankQualificationProxyURL, bytes.NewReader(requestBody)) if err != nil { log.Printf("创建权益中转请求失败 | target=%s | err=%v", scbankQualificationProxyURL, err) return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) @@ -200,11 +204,22 @@ func forwardScbankQualification(c *fiber.Ctx) error { return c.Status(fiber.StatusInternalServerError).SendString(err.Error()) } - log.Printf("权益中转请求完成 | target=%s | status=%d | cost=%s", scbankQualificationProxyURL, resp.StatusCode, time.Since(startAt)) + log.Printf("权益中转请求完成 | target=%s | status=%d | cost=%s | headers=%s | body=%s", scbankQualificationProxyURL, resp.StatusCode, time.Since(startAt), formatHeaders(resp.Header), string(body)) c.Status(resp.StatusCode) return c.Send(body) } +func formatHeaders(headers map[string][]string) string { + if len(headers) == 0 { + return "{}" + } + data, err := json.Marshal(headers) + if err != nil { + return "{}" + } + return string(data) +} + func routerSocket(app *fiber.App, chatService *services.ChatService) { ws := app.Group("ws/v1/") // WebSocket 路由配置