提交 6bcf8762 authored 作者: mooncake9527's avatar mooncake9527

update

上级 739d10d6
......@@ -183,6 +183,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
)
}
c.Set(ctxUtil.KeyReqBody, buf.Bytes())
reqID := ""
if o.traceIDFrom == 1 {
if v, isExist := c.Get(ctxUtil.ContextTraceIDKey); isExist {
......@@ -209,6 +211,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests
c.Next()
//c.Set(ctxUtil.KeyRspBody, newWriter.body.Bytes())
// print return message after processing
fields = []zap.Field{
zap.Int("code", c.Writer.Status()),
......
......@@ -3,14 +3,12 @@ package response
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/xctx"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
......@@ -32,7 +30,7 @@ func newResp(c *gin.Context, code int, msg string, data interface{}) *Result {
Code: code,
Msg: msg,
TimeStamp: time.Now().Unix(),
TraceID: xctx.GetGinCtxTid(c),
TraceID: ctxUtil.GetGinCtxTid(c),
}
// ensure that the data field is not nil on return, note that it is not nil when resp.data=[]interface {}, it is serialized to null
......@@ -59,14 +57,15 @@ func writeContentType(w http.ResponseWriter, value []string) {
func writeJSON(c *gin.Context, code int, res interface{}) {
c.Writer.WriteHeader(code)
writeContentType(c.Writer, jsonContentType)
//if res != nil {
// resBytes, _ := json.Marshal(res)
// c.Set(xctx.KeyResp, resBytes)
//}
err := json.NewEncoder(c.Writer).Encode(res)
if err != nil {
fmt.Printf("json encode error, err = %s\n", err.Error())
if res != nil {
resBytes, _ := json.Marshal(res)
c.Set(ctxUtil.KeyRspBody, resBytes)
c.Writer.Write(resBytes)
}
//err := json.NewEncoder(c.Writer).Encode(res)
//if err != nil {
// fmt.Printf("json encode error, err = %s\n", err.Error())
//}
}
func respJSONWithStatusCode(c *gin.Context, code int, msg string, data ...interface{}) {
......
......@@ -23,6 +23,8 @@ var (
HeaderXTimestampKey = "Timestamp"
KeyReqBody = "req-body"
KeyRspBody = "rsp-body"
KeyTid = "tid"
KeyClientIP = "client_ip"
KeyUID = "userId"
......@@ -33,7 +35,7 @@ var (
KeyToken = "token"
KeyUser = "user"
KeyResp = "resp"
//KeyResp = "resp"
)
var (
......@@ -205,8 +207,19 @@ func getCtxVal[T any](c context.Context, key string) T {
return t
}
func GetResp(c *gin.Context) []byte {
v, ok1 := c.Get(KeyResp)
func GetGinReq(c *gin.Context) []byte {
v, ok1 := c.Get(KeyReqBody)
if ok1 {
ret, ok2 := v.([]byte)
if ok2 {
return ret
}
}
return nil
}
func GetGinRsp(c *gin.Context) []byte {
v, ok1 := c.Get(KeyRspBody)
if ok1 {
ret, ok2 := v.([]byte)
if ok2 {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论