提交 3e568ff7 authored 作者: mooncake9527's avatar mooncake9527

oapi req rsp

上级 6c3a1bfa
...@@ -18,17 +18,21 @@ const ( ...@@ -18,17 +18,21 @@ const (
GinContextKey = "ginContext" GinContextKey = "ginContext"
HeaderXTimestampKey = "Timestamp" HeaderXTimestampKey = "Timestamp"
KeyReqBody = "reqBody" KeyReqBody = "reqBody"
KeyAppName = "appName"
KeyApiStartTime = "apiStartTime"
KeyRspBody = "rspBody" KeyRspBody = "rspBody"
KeyClientIP = "clientIP"
KeyUID = "userId" KeyOApiReqBody = "oApiReqBody%s"
KeyUType = "userType" KeyOApiRspBody = "oApiRspBody%s"
KeyCompanyID = "companyId"
KeyShopID = "shopID" KeyAppName = "appName"
KeyUName = "uname" KeyApiStartTime = "apiStartTime"
KeyToken = "token" KeyClientIP = "clientIP"
KeyCost = "X-Request-Cost" KeyUID = "userId"
KeyUType = "userType"
KeyCompanyID = "companyId"
KeyShopID = "shopID"
KeyUName = "uname"
KeyToken = "token"
KeyCost = "X-Request-Cost"
) )
var ( var (
......
...@@ -2,6 +2,7 @@ package middleware ...@@ -2,6 +2,7 @@ package middleware
import ( import (
"bytes" "bytes"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity" "gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io" "io"
"net/http" "net/http"
...@@ -175,7 +176,7 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -175,7 +176,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
} }
// print input information before processing // print input information before processing
buf := bytes.Buffer{} buf := &bytes.Buffer{}
_, _ = buf.ReadFrom(c.Request.Body) _, _ = buf.ReadFrom(c.Request.Body)
fields := []zap.Field{ fields := []zap.Field{
...@@ -186,16 +187,12 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -186,16 +187,12 @@ func Logging(opts ...Option) gin.HandlerFunc {
if c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut || c.Request.Method == http.MethodPatch || c.Request.Method == http.MethodDelete { if c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut || c.Request.Method == http.MethodPatch || c.Request.Method == http.MethodDelete {
fields = append(fields, fields = append(fields,
zap.Int("size", buf.Len()), zap.Int("size", buf.Len()),
zap.ByteString("body", getRequestBody(&buf, o.maxLength)), zap.ByteString("body", getRequestBody(buf, o.maxLength)),
) )
} }
copyHttpReq := &entity.CopyHttpReq{} SetCopyReq(c, buf)
copyHttpReq.Method = c.Request.Method
copyHttpReq.URL = c.Request.URL.String()
copyHttpReq.Headers = c.Request.Header
copyHttpReq.Body = buf.String()
c.Set(ctxUtils.KeyReqBody, copyHttpReq)
c.Set(ctxUtils.KeyAppName, o.appName) c.Set(ctxUtils.KeyAppName, o.appName)
reqID := "" reqID := ""
...@@ -212,7 +209,7 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -212,7 +209,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
} }
o.log.Info("<<<<<<<<<req", fields...) o.log.Info("<<<<<<<<<req", fields...)
c.Request.Body = io.NopCloser(&buf) c.Request.Body = io.NopCloser(buf)
//replace writer //replace writer
newWriter := &bodyLogWriter{body: &bytes.Buffer{}, ResponseWriter: c.Writer} newWriter := &bodyLogWriter{body: &bytes.Buffer{}, ResponseWriter: c.Writer}
...@@ -244,6 +241,15 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -244,6 +241,15 @@ func Logging(opts ...Option) gin.HandlerFunc {
} }
} }
func SetCopyReq(c *gin.Context, buf *bytes.Buffer) {
copyHttpReq := &entity.CopyHttpReq{}
copyHttpReq.Method = c.Request.Method
copyHttpReq.URL = c.Request.URL.String()
copyHttpReq.Headers = c.Request.Header
copyHttpReq.Body = buf.String()
c.Set(ctxUtils.KeyReqBody, copyHttpReq)
}
func GetCopyReq(c *gin.Context) *entity.CopyHttpReq { func GetCopyReq(c *gin.Context) *entity.CopyHttpReq {
if val, isExist := c.Get(ctxUtils.KeyReqBody); isExist { if val, isExist := c.Get(ctxUtils.KeyReqBody); isExist {
if req, ok := val.(*entity.CopyHttpReq); ok { if req, ok := val.(*entity.CopyHttpReq); ok {
...@@ -253,6 +259,13 @@ func GetCopyReq(c *gin.Context) *entity.CopyHttpReq { ...@@ -253,6 +259,13 @@ func GetCopyReq(c *gin.Context) *entity.CopyHttpReq {
return nil return nil
} }
func SetCopyRsp(c *gin.Context, buf *bytes.Buffer) {
copyHttpRsp := &entity.CopyHttpRsp{}
copyHttpRsp.Headers = c.Writer.Header()
copyHttpRsp.Body = buf.String()
c.Set(ctxUtils.KeyRspBody, copyHttpRsp)
}
func GetCopyRsp(c *gin.Context) *entity.CopyHttpRsp { func GetCopyRsp(c *gin.Context) *entity.CopyHttpRsp {
if val, isExist := c.Get(ctxUtils.KeyRspBody); isExist { if val, isExist := c.Get(ctxUtils.KeyRspBody); isExist {
if rsp, ok := val.(*entity.CopyHttpRsp); ok { if rsp, ok := val.(*entity.CopyHttpRsp); ok {
...@@ -262,6 +275,40 @@ func GetCopyRsp(c *gin.Context) *entity.CopyHttpRsp { ...@@ -262,6 +275,40 @@ func GetCopyRsp(c *gin.Context) *entity.CopyHttpRsp {
return nil return nil
} }
func SetCopyApiReq(c *gin.Context, buf *bytes.Buffer) {
copyHttpReq := &entity.CopyHttpReq{}
copyHttpReq.Method = c.Request.Method
copyHttpReq.URL = c.Request.URL.String()
copyHttpReq.Headers = c.Request.Header
copyHttpReq.Body = buf.String()
c.Set(fmt.Sprintf(ctxUtils.KeyOApiReqBody, ctxUtils.GetGinCtxTraceID(c)), copyHttpReq)
}
func GetCopyApiReq(c *gin.Context) *entity.CopyHttpReq {
if val, isExist := c.Get(fmt.Sprintf(ctxUtils.KeyOApiReqBody, ctxUtils.GetGinCtxTraceID(c))); isExist {
if req, ok := val.(*entity.CopyHttpReq); ok {
return req
}
}
return nil
}
func SetCopyApiRsp(c *gin.Context, buf *bytes.Buffer) {
copyHttpRsp := &entity.CopyHttpRsp{}
copyHttpRsp.Headers = c.Writer.Header()
copyHttpRsp.Body = buf.String()
c.Set(fmt.Sprintf(ctxUtils.KeyOApiRspBody, ctxUtils.GetGinCtxTraceID(c)), copyHttpRsp)
}
func GetCopyApiRsp(c *gin.Context) *entity.CopyHttpRsp {
if val, isExist := c.Get(fmt.Sprintf(ctxUtils.KeyOApiRspBody, ctxUtils.GetGinCtxTraceID(c))); isExist {
if rsp, ok := val.(*entity.CopyHttpRsp); ok {
return rsp
}
}
return nil
}
// SimpleLog print response info // SimpleLog print response info
func SimpleLog(opts ...Option) gin.HandlerFunc { func SimpleLog(opts ...Option) gin.HandlerFunc {
o := defaultOptions() o := defaultOptions()
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
package response package response
import ( import (
"bytes"
"encoding/json" "encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity" "gitlab.wanzhuangkj.com/tush/xpkg/gin/middleware"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
...@@ -61,12 +62,8 @@ func writeJSON(c *gin.Context, code int, res interface{}) { ...@@ -61,12 +62,8 @@ func writeJSON(c *gin.Context, code int, res interface{}) {
writeContentType(c.Writer, jsonContentType) writeContentType(c.Writer, jsonContentType)
if res != nil { if res != nil {
resBytes, _ := json.Marshal(res) resBytes, _ := json.Marshal(res)
buf := bytes.NewBuffer(resBytes)
copyHttpRsp := &entity.CopyHttpRsp{} middleware.SetCopyRsp(c, buf)
copyHttpRsp.Headers = c.Writer.Header()
copyHttpRsp.Body = string(resBytes)
c.Set(ctxUtils.KeyRspBody, copyHttpRsp)
_, _ = c.Writer.Write(resBytes) _, _ = c.Writer.Write(resBytes)
} }
//err := json.NewEncoder(c.Writer).Encode(res) //err := json.NewEncoder(c.Writer).Encode(res)
......
...@@ -297,7 +297,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) ...@@ -297,7 +297,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
} }
if buf != nil { if buf != nil {
requestID := ctxUtils.CtxRequestID(ctx) requestID := ctxUtils.CtxRequestID(ctx)
ctxUtils.SetVal(ctx, fmt.Sprintf("oApiReq-%s", requestID), buf.Bytes()) ctxUtils.SetVal(ctx, fmt.Sprintf(ctxUtils.KeyOApiReqBody, requestID), buf.Bytes())
} }
if req.customRequest != nil { if req.customRequest != nil {
req.customRequest(req.request, buf) req.customRequest(req.request, buf)
...@@ -339,7 +339,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) ...@@ -339,7 +339,7 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
} }
bodyBuf := bytes.NewBuffer(body) bodyBuf := bytes.NewBuffer(body)
logger.Info("httpCli rsp", logger.Any("header", resp.Header), logger.Any("body", bodyBuf.String()), ctxUtils.CtxTraceIDField(ctx)) logger.Info("httpCli rsp", logger.Any("header", resp.Header), logger.Any("body", bodyBuf.String()), ctxUtils.CtxTraceIDField(ctx))
ctxUtils.SetVal(ctx, fmt.Sprintf("oApiRsp-%s", ctxUtils.CtxRequestID(ctx)), bodyBuf.Bytes()) ctxUtils.SetVal(ctx, fmt.Sprintf(ctxUtils.KeyOApiRspBody, ctxUtils.CtxRequestID(ctx)), bodyBuf.Bytes())
if body != nil { if body != nil {
resp.Body = io.NopCloser(bodyBuf) resp.Body = io.NopCloser(bodyBuf)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论