提交 d1238b46 authored 作者: mooncake9527's avatar mooncake9527

oapi req rsp

上级 3e568ff7
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"
"fmt" "gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io" "io"
"net/http" "net/http"
"time" "time"
...@@ -191,7 +190,7 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -191,7 +190,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
) )
} }
SetCopyReq(c, buf) entity.SetCopyReq(c, buf)
c.Set(ctxUtils.KeyAppName, o.appName) c.Set(ctxUtils.KeyAppName, o.appName)
...@@ -241,74 +240,6 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -241,74 +240,6 @@ 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 {
return req
}
}
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 {
return rsp
}
}
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 // SimpleLog print response info
func SimpleLog(opts ...Option) gin.HandlerFunc { func SimpleLog(opts ...Option) gin.HandlerFunc {
o := defaultOptions() o := defaultOptions()
......
...@@ -4,7 +4,7 @@ package response ...@@ -4,7 +4,7 @@ package response
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/middleware" "gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
...@@ -63,7 +63,7 @@ func writeJSON(c *gin.Context, code int, res interface{}) { ...@@ -63,7 +63,7 @@ func writeJSON(c *gin.Context, code int, res interface{}) {
if res != nil { if res != nil {
resBytes, _ := json.Marshal(res) resBytes, _ := json.Marshal(res)
buf := bytes.NewBuffer(resBytes) buf := bytes.NewBuffer(resBytes)
middleware.SetCopyRsp(c, buf) entity.SetCopyRsp(c, buf)
_, _ = c.Writer.Write(resBytes) _, _ = c.Writer.Write(resBytes)
} }
//err := json.NewEncoder(c.Writer).Encode(res) //err := json.NewEncoder(c.Writer).Encode(res)
......
package entity
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
)
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"`
}
func SetCopyReq(c *gin.Context, buf *bytes.Buffer) {
copyHttpReq := &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) *CopyHttpReq {
if val, isExist := c.Get(ctxUtils.KeyReqBody); isExist {
if req, ok := val.(*CopyHttpReq); ok {
return req
}
}
return nil
}
func SetCopyRsp(c *gin.Context, buf *bytes.Buffer) {
copyHttpRsp := &CopyHttpRsp{}
copyHttpRsp.Headers = c.Writer.Header()
copyHttpRsp.Body = buf.String()
c.Set(ctxUtils.KeyRspBody, copyHttpRsp)
}
func GetCopyRsp(c *gin.Context) *CopyHttpRsp {
if val, isExist := c.Get(ctxUtils.KeyRspBody); isExist {
if rsp, ok := val.(*CopyHttpRsp); ok {
return rsp
}
}
return nil
}
func SetCopyApiReq(c *gin.Context, buf *bytes.Buffer) {
copyHttpReq := &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) *CopyHttpReq {
if val, isExist := c.Get(fmt.Sprintf(ctxUtils.KeyOApiReqBody, ctxUtils.GetGinCtxTraceID(c))); isExist {
if req, ok := val.(*CopyHttpReq); ok {
return req
}
}
return nil
}
func SetCopyApiRsp(c *gin.Context, buf *bytes.Buffer) {
copyHttpRsp := &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) *CopyHttpRsp {
if val, isExist := c.Get(fmt.Sprintf(ctxUtils.KeyOApiRspBody, ctxUtils.GetGinCtxTraceID(c))); isExist {
if rsp, ok := val.(*CopyHttpRsp); ok {
return rsp
}
}
return nil
}
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
...@@ -255,7 +256,6 @@ func (req *Request) push(ctx context.Context) (*Response, error) { ...@@ -255,7 +256,6 @@ func (req *Request) push(ctx context.Context) (*Response, error) {
buf = bytes.NewBufferString(req.body) buf = bytes.NewBufferString(req.body)
} }
//logger.Info("httpCli req", logger.String("body", buf.String()), ctxUtils.CtxTraceIDField(ctx))
return req.send(ctx, buf, buf) return req.send(ctx, buf, buf)
} }
...@@ -296,8 +296,9 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) ...@@ -296,8 +296,9 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
return nil, req.err return nil, req.err
} }
if buf != nil { if buf != nil {
requestID := ctxUtils.CtxRequestID(ctx) if c, _ := ctxUtils.GetGinCtx(ctx); c != nil {
ctxUtils.SetVal(ctx, fmt.Sprintf(ctxUtils.KeyOApiReqBody, requestID), buf.Bytes()) entity.SetCopyApiReq(c, buf)
}
} }
if req.customRequest != nil { if req.customRequest != nil {
req.customRequest(req.request, buf) req.customRequest(req.request, buf)
...@@ -338,8 +339,10 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) ...@@ -338,8 +339,10 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
return xerror.New(err.Error()) return xerror.New(err.Error())
} }
bodyBuf := bytes.NewBuffer(body) bodyBuf := bytes.NewBuffer(body)
if c, _ := ctxUtils.GetGinCtx(ctx); c != nil {
entity.SetCopyApiRsp(c, buf)
}
logger.Info("httpCli rsp", logger.Any("header", resp.Header), logger.Any("body", bodyBuf.String()), ctxUtils.CtxTraceIDField(ctx)) logger.Info("httpCli rsp", logger.Any("header", resp.Header), logger.Any("body", bodyBuf.String()), ctxUtils.CtxTraceIDField(ctx))
ctxUtils.SetVal(ctx, fmt.Sprintf(ctxUtils.KeyOApiRspBody, ctxUtils.CtxRequestID(ctx)), bodyBuf.Bytes())
if body != nil { if body != nil {
resp.Body = io.NopCloser(bodyBuf) resp.Body = io.NopCloser(bodyBuf)
} }
...@@ -377,7 +380,9 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer) ...@@ -377,7 +380,9 @@ func (req *Request) send(ctx context.Context, body io.Reader, buf *bytes.Buffer)
} }
bodyBuf := bytes.NewBuffer(body) bodyBuf := bytes.NewBuffer(body)
logger.Info("httpCli rsp", logger.Any("header", resp.Header), logger.Any("body", bodyBuf.String()), ctxUtils.CtxTraceIDField(ctx)) 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()) if c, _ := ctxUtils.GetGinCtx(ctx); c != nil {
entity.SetCopyApiRsp(c, buf)
}
if body != nil { if body != nil {
resp.Body = io.NopCloser(bodyBuf) resp.Body = io.NopCloser(bodyBuf)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论