fix(router): 修复SC银行资质校验接口的请求体转发问题并添加详细日志
新增formatHeaders工具函数以格式化日志中的HTTP头 预先读取完整请求体到缓冲区,修复重复读取请求流导致的转发body为空问题 完善转发日志,新增打印请求查询参数、请求头、请求体、响应头和响应体
This commit is contained in:
parent
dadb388d79
commit
2954ab5fc5
|
|
@ -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 路由配置
|
||||
|
|
|
|||
Loading…
Reference in New Issue