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

cp req rsp

上级 96974b25
package entity
type CopyHttpReq struct {
Method string `json:"method"`
URL string `json:"url"`
Headers map[string][]string `json:"headers"`
Body string `json:"body"`
}
type CopyHttpRsp struct {
Headers map[string][]string `json:"headers"`
Body string `json:"body"`
}
...@@ -2,8 +2,7 @@ package middleware ...@@ -2,8 +2,7 @@ package middleware
import ( import (
"bytes" "bytes"
"encoding/json" "gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/uriUtils"
"io" "io"
"net/http" "net/http"
"time" "time"
...@@ -185,14 +184,20 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -185,14 +184,20 @@ func Logging(opts ...Option) gin.HandlerFunc {
) )
} }
c.Set(ctxUtil.KeyReqBody, buf.Bytes()) copyHttpReq := &entity.CopyHttpReq{}
if c.Request.Method == http.MethodGet && buf.Len() == 0 { copyHttpReq.Method = c.Request.Method
paramMap, err := uriUtils.URLValuePairsToMap(c.Request.URL.RawQuery) copyHttpReq.URL = c.Request.URL.String()
if err == nil { copyHttpReq.Headers = c.Request.Header
val, _ := json.Marshal(paramMap) copyHttpReq.Body = buf.String()
c.Set(ctxUtil.KeyReqBody, val) c.Set(ctxUtil.KeyReqBody, copyHttpReq)
}
} //if c.Request.Method == http.MethodGet && buf.Len() == 0 {
// paramMap, err := uriUtils.URLValuePairsToMap(c.Request.URL.RawQuery)
// if err == nil {
// val, _ := json.Marshal(paramMap)
// c.Set(ctxUtil.KeyReqBody, val)
// }
//}
reqID := "" reqID := ""
if o.traceIDFrom == 1 { if o.traceIDFrom == 1 {
...@@ -236,10 +241,33 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -236,10 +241,33 @@ func Logging(opts ...Option) gin.HandlerFunc {
if reqID != "" { if reqID != "" {
fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID)) fields = append(fields, zap.String(ctxUtil.ContextTraceIDKey, reqID))
} }
//copyHttpRsp := CopyHttpRsp{}
//copyHttpRsp.Headers = c.Writer.Header()
//copyHttpRsp.Body = newWriter.body.Bytes()
//c.Set(ctxUtil.KeyRspBody, jsonUtils.ToJsonBytes(copyHttpRsp))
o.log.Info(">>>>>>>>>rsp", fields...) o.log.Info(">>>>>>>>>rsp", fields...)
} }
} }
func GetCopyReq(c *gin.Context) *entity.CopyHttpReq {
if val, isExist := c.Get(ctxUtil.KeyReqBody); isExist {
if req, ok := val.(*entity.CopyHttpReq); ok {
return req
}
}
return nil
}
func GetCopyRsp(c *gin.Context) *entity.CopyHttpRsp {
if val, isExist := c.Get(ctxUtil.KeyRspBody); 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()
......
...@@ -3,6 +3,7 @@ package response ...@@ -3,6 +3,7 @@ package response
import ( import (
"encoding/json" "encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"net/http" "net/http"
"time" "time"
...@@ -59,8 +60,13 @@ func writeJSON(c *gin.Context, code int, res interface{}) { ...@@ -59,8 +60,13 @@ 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)
c.Set(ctxUtils.KeyRspBody, resBytes)
c.Writer.Write(resBytes) copyHttpRsp := &entity.CopyHttpRsp{}
copyHttpRsp.Headers = c.Writer.Header()
copyHttpRsp.Body = string(resBytes)
c.Set(ctxUtils.KeyRspBody, copyHttpRsp)
_, _ = c.Writer.Write(resBytes)
} }
//err := json.NewEncoder(c.Writer).Encode(res) //err := json.NewEncoder(c.Writer).Encode(res)
//if err != nil { //if err != nil {
......
package jsonUtils
import (
"encoding/json"
"errors"
)
func ToJsonString(v any) string {
return string(ToJsonBytes(v))
}
func ToJsonBytes(v any) []byte {
if v == nil {
return []byte{}
}
bytes, _ := json.Marshal(v)
return bytes
}
func Unmarshal(data []byte, v any) error {
err := json.Unmarshal(data, v)
if err != nil {
return errors.New(err.Error())
}
return nil
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论