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

update

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