提交 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
import (
"bytes"
"encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/uriUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io"
"net/http"
"time"
......@@ -185,14 +184,20 @@ func Logging(opts ...Option) gin.HandlerFunc {
)
}
c.Set(ctxUtil.KeyReqBody, buf.Bytes())
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)
}
}
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(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 := ""
if o.traceIDFrom == 1 {
......@@ -236,10 +241,33 @@ func Logging(opts ...Option) gin.HandlerFunc {
if 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...)
}
}
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
func SimpleLog(opts ...Option) gin.HandlerFunc {
o := defaultOptions()
......
......@@ -3,6 +3,7 @@ package response
import (
"encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"net/http"
"time"
......@@ -59,8 +60,13 @@ func writeJSON(c *gin.Context, code int, res interface{}) {
writeContentType(c.Writer, jsonContentType)
if res != nil {
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)
//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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论