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

oapi req rsp

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