提交 55456d37 authored 作者: mooncake9527's avatar mooncake9527

update

上级 63caa729
......@@ -11,8 +11,8 @@ import (
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/encoding"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
xslice "gitlab.wanzhuangkj.com/tush/xpkg/utils/sliceUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
)
......
package ctxUtils
import (
"context"
"errors"
"fmt"
"strings"
"time"
xsf "gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"go.uber.org/zap"
)
const (
GinContextKey = "ginContext"
HeaderXTimestampKey = "Timestamp"
KeyReqBody = "reqBody"
KeyRspBody = "rspBody"
KeyRspCode = "rspCode"
KeyOApiReqBody = "oApiReqBody%s"
KeyOApiRspBody = "oApiRspBody%s"
KeyAppName = "appName"
KeyUser = "user"
KeyApiStartTime = "apiStartTime"
KeyClientIP = "clientIP"
KeyUID = "uid"
KeyUType = "uType"
KeyCompanyID = "companyID"
KeyShopID = "shopID"
KeyUName = "uname"
KeyToken = "token"
KeyCost = "X-Request-Cost"
KeyRspBodyMax = "rsp-body-max"
)
var (
HeaderXRequestIDKey = "X-Request-ID"
ContextTraceIDKey = "X-Request-ID"
)
var (
ErrorGinContextNotFound = errors.New("gin context not found")
)
func Set(c *gin.Context, key string, val any) {
c.Set(key, val)
}
func SetRspBodyMax(ctx context.Context, max int) {
if c, _ := ToGinCtx(ctx); c != nil {
Set(c, KeyRspBodyMax, max)
}
}
func Get[T any](c *gin.Context, key string) (T, bool) {
if val, ok := c.Get(key); ok {
if v, ok1 := val.(T); ok1 {
return v, true
}
}
var a T
return a, false
}
// GetGinCtxTraceID get request id from gin.Context
func GetGinCtxTraceID(c *gin.Context) string {
if v, isExist := c.Get(ContextTraceIDKey); isExist {
if requestID, ok := v.(string); ok {
return requestID
}
}
return ""
}
// GinTraceIDField get request id field from gin.Context
func GinTraceIDField(c *gin.Context) zap.Field {
return zap.String(ContextTraceIDKey, GetGinCtxTraceID(c))
}
// CtxRequestID get request id from context.Context
func CtxRequestID(c context.Context) string {
v := c.Value(ContextTraceIDKey)
if str, ok := v.(string); ok {
return str
}
return ""
}
// CtxXRequestID get request id from context.Context
func CtxXRequestID(c context.Context) string {
v := c.Value(HeaderXRequestIDKey)
if str, ok := v.(string); ok {
return str
}
return ""
}
func CtxGetGinCtx(c context.Context) *gin.Context {
if str, ok := c.Value(GinContextKey).(*gin.Context); ok {
return str
}
return nil
}
// CtxTraceIDField get request id field from context.Context
func CtxTraceIDField(c context.Context) zap.Field {
return zap.String(ContextTraceIDKey, CtxRequestID(c))
}
func NewEmptyCtx() context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, GenerateTid())
}
func NewCtx(ctx context.Context) context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, ctx.Value(ContextTraceIDKey))
}
func GetGinUserID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyUID)
}
func GetGinShopID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyShopID)
}
func GetCtxUserID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyUID)
}
func GetCtxShopID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyShopID)
}
func GetCtxString(c context.Context, key string) string {
val := c.Value(key)
if val != nil {
return cast.ToString(val)
}
return ""
}
func GetCtxUserToken(c context.Context) string {
token := c.Value(KeyToken)
if token != nil {
return cast.ToString(token)
}
return ""
}
func GetGinCtxTid(c *gin.Context) string {
tid := c.GetString(ContextTraceIDKey)
if tid == "" {
tid = GenerateTid()
c.Set(ContextTraceIDKey, tid)
}
return tid
}
func GenerateTid() string {
return xsf.GenerateID().String()
}
func GetCtxTid(ctx context.Context) string {
tid := ""
tidVal := ctx.Value(ContextTraceIDKey)
if tidVal != nil {
if str, ok := tidVal.(string); ok {
tid = str
}
}
if tid == "" {
tid = GenerateTid()
}
return tid
}
// GetClientIP get ip from context.Context, the client ip is set in gin.Context
func GetClientIP(ctx context.Context) string {
ip := ""
ipVal := ctx.Value(KeyClientIP)
if ipVal != nil {
if str, ok := ipVal.(string); ok {
ip = str
}
}
return ip
}
func WrapCtx(c *gin.Context) context.Context {
ctx := context.WithValue(c.Request.Context(), ContextTraceIDKey, c.GetString(ContextTraceIDKey))
for k, v := range c.Keys {
ctx = context.WithValue(ctx, k, v)
}
ctx = context.WithValue(ctx, GinContextKey, c) //nolint
return ctx
}
func ToGinCtx(ctx context.Context) (*gin.Context, error) {
if c, ok := ctx.Value(GinContextKey).(*gin.Context); ok {
return c, nil
}
return nil, ErrorGinContextNotFound
}
func GetGinCtx(ctx context.Context) (*gin.Context, error) {
if c, ok := ctx.Value(GinContextKey).(*gin.Context); ok {
return c, nil
}
return nil, ErrorGinContextNotFound
}
func GetCtxUID(c context.Context) uint {
return getCtxVal[uint](c, KeyUID)
}
func GetCtxUType(c context.Context) uint {
return getCtxVal[uint](c, KeyUType)
}
func GetCtxCompanyID(c context.Context) uint {
return getCtxVal[uint](c, KeyCompanyID)
}
func getGinVal[T any](c *gin.Context, key string) T {
val, exists := c.Get(key)
if exists {
if ret, ok := val.(T); ok {
return ret
}
}
var t T
return t
}
func getCtxVal[T any](c context.Context, key string) T {
val := c.Value(key)
if val != nil {
if ret, ok := val.(T); ok {
return ret
}
}
var t T
return t
}
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 {
return ret
}
}
return nil
}
func SetVal(ctx context.Context, key string, val interface{}) context.Context {
if ginCtx, err := GetGinCtx(ctx); err == nil && ginCtx != nil {
ginCtx.Set(key, val)
}
return context.WithValue(ctx, key, val)
}
func AddApiCost(c *gin.Context, appName string, start time.Time) {
cost := c.Writer.Header().Get(KeyCost)
currentCost := fmt.Sprintf("%s:%s", appName, formatDuration(time.Since(start)))
if cost == "" {
cost = currentCost
} else {
cost = fmt.Sprintf("%s,%s", currentCost, cost)
}
c.Writer.Header().Set(KeyCost, cost)
}
func formatDuration(d time.Duration) string {
ms := float64(d.Nanoseconds()) / 1e6
return fmt.Sprintf("%.2fms", ms)
}
func SetApiCost(ctx context.Context, header map[string][]string) {
if ginCtx, _ := GetGinCtx(ctx); ginCtx != nil && len(header) > 0 {
cost := ""
for k, v := range header {
if k == KeyCost {
cost = strings.Join(v, ",")
}
}
if cost != "" {
ginCtx.Writer.Header().Set(KeyCost, cost)
}
}
}
......@@ -6,10 +6,10 @@ import (
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/jwt"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
)
......
......@@ -8,8 +8,8 @@ import (
"strings"
"time"
ctxUtils "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/ips"
ctxUtils "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
......
......@@ -7,11 +7,11 @@ import (
"github.com/gin-gonic/gin"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/response"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
)
func init() {
......
......@@ -3,7 +3,7 @@ package middleware
import (
"net/http"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
......
......@@ -3,7 +3,7 @@ package middleware
import (
"context"
"github.com/gin-gonic/gin"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"go.uber.org/zap"
"net/http"
"time"
......@@ -20,8 +20,8 @@ type requestIDOptions struct {
func defaultRequestIDOptions() *requestIDOptions {
return &requestIDOptions{
contextRequestIDKey: ctxUtil.ContextTraceIDKey,
headerXRequestIDKey: ctxUtil.HeaderXRequestIDKey,
contextRequestIDKey: ctxUtils.ContextTraceIDKey,
headerXRequestIDKey: ctxUtils.HeaderXRequestIDKey,
}
}
......@@ -32,11 +32,11 @@ func (o *requestIDOptions) apply(opts ...RequestIDOption) {
}
func (o *requestIDOptions) setRequestIDKey() {
if o.contextRequestIDKey != ctxUtil.ContextTraceIDKey {
ctxUtil.ContextTraceIDKey = o.contextRequestIDKey
if o.contextRequestIDKey != ctxUtils.ContextTraceIDKey {
ctxUtils.ContextTraceIDKey = o.contextRequestIDKey
}
if o.headerXRequestIDKey != ctxUtil.HeaderXRequestIDKey {
ctxUtil.HeaderXRequestIDKey = o.headerXRequestIDKey
if o.headerXRequestIDKey != ctxUtils.HeaderXRequestIDKey {
ctxUtils.HeaderXRequestIDKey = o.headerXRequestIDKey
}
}
......@@ -64,7 +64,7 @@ func WithHeaderRequestIDKey(key string) RequestIDOption {
type CtxKeyString string
// RequestIDKey request_id
var RequestIDKey = CtxKeyString(ctxUtil.ContextTraceIDKey)
var RequestIDKey = CtxKeyString(ctxUtils.ContextTraceIDKey)
// -------------------------------------------------------------------------------------------
......@@ -76,21 +76,21 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
o.setRequestIDKey()
return func(c *gin.Context) {
requestID := c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
requestID := c.Request.Header.Get(ctxUtils.HeaderXRequestIDKey)
// Create request id
if requestID == "" {
requestID = ctxUtil.GenerateTid()
c.Request.Header.Set(ctxUtil.HeaderXRequestIDKey, requestID)
requestID = ctxUtils.GenerateTid()
c.Request.Header.Set(ctxUtils.HeaderXRequestIDKey, requestID)
}
st := time.Now()
// Expose it for use in the application
c.Set(ctxUtil.ContextTraceIDKey, requestID)
c.Set(ctxUtil.KeyApiStartTime, st)
c.Set(ctxUtils.ContextTraceIDKey, requestID)
c.Set(ctxUtils.KeyApiStartTime, st)
// Set X-Request-Id header
c.Writer.Header().Set(ctxUtil.HeaderXRequestIDKey, requestID)
c.Writer.Header().Set(ctxUtils.HeaderXRequestIDKey, requestID)
c.Next()
}
......@@ -98,12 +98,12 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
// HeaderRequestID get request id from the header
func HeaderRequestID(c *gin.Context) string {
return c.Request.Header.Get(ctxUtil.HeaderXRequestIDKey)
return c.Request.Header.Get(ctxUtils.HeaderXRequestIDKey)
}
// HeaderRequestIDField get request id field from header
func HeaderRequestIDField(c *gin.Context) zap.Field {
return zap.String(ctxUtil.HeaderXRequestIDKey, HeaderRequestID(c))
return zap.String(ctxUtils.HeaderXRequestIDKey, HeaderRequestID(c))
}
// -------------------------------------------------------------------------------------------
......@@ -111,22 +111,12 @@ func HeaderRequestIDField(c *gin.Context) zap.Field {
// RequestHeaderKey request header key
var RequestHeaderKey = "request_header_key"
// WrapCtx wrap context, put the Keys and Header of gin.Context into context
func WrapCtx(c *gin.Context) context.Context {
ctx := context.WithValue(c.Request.Context(), ctxUtil.ContextTraceIDKey, c.GetString(ctxUtil.ContextTraceIDKey)) //nolint
for k, v := range c.Keys {
ctx = context.WithValue(ctx, k, v) //nolint
}
ctx = context.WithValue(ctx, ctxUtil.GinContextKey, c) //nolint
return ctx
}
func WrapAsyncCtx(c *gin.Context) context.Context {
ctx := context.WithValue(context.Background(), ctxUtil.ContextTraceIDKey, c.GetString(ctxUtil.ContextTraceIDKey)) //nolint
ctx := context.WithValue(context.Background(), ctxUtils.ContextTraceIDKey, c.GetString(ctxUtils.ContextTraceIDKey)) //nolint
for k, v := range c.Keys {
ctx = context.WithValue(ctx, k, v) //nolint
}
ctx = context.WithValue(ctx, ctxUtil.GinContextKey, c) //nolint
ctx = context.WithValue(ctx, ctxUtils.GinContextKey, c) //nolint
return ctx
}
......@@ -134,7 +124,7 @@ func WrapAsyncCtx(c *gin.Context) context.Context {
func AdaptCtx(ctx context.Context) (*gin.Context, context.Context) {
c, ok := ctx.(*gin.Context)
if ok {
ctx = WrapCtx(c)
ctx = ctxUtils.WrapCtx(c)
}
return c, ctx
}
......
......@@ -4,7 +4,7 @@ import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"go.uber.org/zap"
"strconv"
"sync"
......
......@@ -14,8 +14,8 @@ import (
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/errcode"
ctxUtils "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtils "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
)
......
......@@ -7,8 +7,8 @@ import (
"runtime"
"time"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
......
......@@ -5,7 +5,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
)
type CopyHttpReq struct {
......
......@@ -17,8 +17,8 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"github.com/duke-git/lancet/v2/retry"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
)
const defaultTimeout = 30 * time.Second
......
package ctxUtils
//
//func AddApiCost(c *gin.Context, appName string, start time.Time) {
// cost := c.Writer.Header().Get(ctxUtil.KeyCost)
// cost += fmt.Sprintf("%s:%d", appName, time.Since(start).Milliseconds())
// c.Writer.Header().Set(ctxUtil.KeyCost, cost)
//}
import (
"context"
"errors"
"fmt"
"strings"
"time"
xsf "gitlab.wanzhuangkj.com/tush/xpkg/utils/xsf"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"go.uber.org/zap"
)
const (
GinContextKey = "ginContext"
HeaderXTimestampKey = "Timestamp"
KeyReqBody = "reqBody"
KeyRspBody = "rspBody"
KeyRspCode = "rspCode"
KeyOApiReqBody = "oApiReqBody%s"
KeyOApiRspBody = "oApiRspBody%s"
KeyAppName = "appName"
KeyUser = "user"
KeyApiStartTime = "apiStartTime"
KeyClientIP = "clientIP"
KeyUID = "uid"
KeyUType = "uType"
KeyCompanyID = "companyID"
KeyShopID = "shopID"
KeyUName = "uname"
KeyToken = "token"
KeyCost = "X-Request-Cost"
KeyRspBodyMax = "rsp-body-max"
)
var (
HeaderXRequestIDKey = "X-Request-ID"
ContextTraceIDKey = "X-Request-ID"
)
var (
ErrorGinContextNotFound = errors.New("gin context not found")
)
func Set(c *gin.Context, key string, val any) {
c.Set(key, val)
}
func SetRspBodyMax(ctx context.Context, max int) {
if c, _ := ToGinCtx(ctx); c != nil {
Set(c, KeyRspBodyMax, max)
}
}
func Get[T any](c *gin.Context, key string) (T, bool) {
if val, ok := c.Get(key); ok {
if v, ok1 := val.(T); ok1 {
return v, true
}
}
var a T
return a, false
}
// GetGinCtxTraceID get request id from gin.Context
func GetGinCtxTraceID(c *gin.Context) string {
if v, isExist := c.Get(ContextTraceIDKey); isExist {
if requestID, ok := v.(string); ok {
return requestID
}
}
return ""
}
// GinTraceIDField get request id field from gin.Context
func GinTraceIDField(c *gin.Context) zap.Field {
return zap.String(ContextTraceIDKey, GetGinCtxTraceID(c))
}
// CtxRequestID get request id from context.Context
func CtxRequestID(c context.Context) string {
v := c.Value(ContextTraceIDKey)
if str, ok := v.(string); ok {
return str
}
return ""
}
// CtxXRequestID get request id from context.Context
func CtxXRequestID(c context.Context) string {
v := c.Value(HeaderXRequestIDKey)
if str, ok := v.(string); ok {
return str
}
return ""
}
func CtxGetGinCtx(c context.Context) *gin.Context {
if str, ok := c.Value(GinContextKey).(*gin.Context); ok {
return str
}
return nil
}
// CtxTraceIDField get request id field from context.Context
func CtxTraceIDField(c context.Context) zap.Field {
return zap.String(ContextTraceIDKey, CtxRequestID(c))
}
func NewEmptyCtx() context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, GenerateTid())
}
func NewCtx(ctx context.Context) context.Context {
return context.WithValue(context.Background(), ContextTraceIDKey, ctx.Value(ContextTraceIDKey))
}
func GetGinUserID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyUID)
}
func GetGinShopID(c *gin.Context) xsf.ID {
return getGinVal[xsf.ID](c, KeyShopID)
}
func GetCtxUserID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyUID)
}
func GetCtxShopID(c context.Context) xsf.ID {
return getCtxVal[xsf.ID](c, KeyShopID)
}
func GetCtxString(c context.Context, key string) string {
val := c.Value(key)
if val != nil {
return cast.ToString(val)
}
return ""
}
func GetCtxUserToken(c context.Context) string {
token := c.Value(KeyToken)
if token != nil {
return cast.ToString(token)
}
return ""
}
func GetGinCtxTid(c *gin.Context) string {
tid := c.GetString(ContextTraceIDKey)
if tid == "" {
tid = GenerateTid()
c.Set(ContextTraceIDKey, tid)
}
return tid
}
func GenerateTid() string {
return xsf.GenerateID().String()
}
func GetCtxTid(ctx context.Context) string {
tid := ""
tidVal := ctx.Value(ContextTraceIDKey)
if tidVal != nil {
if str, ok := tidVal.(string); ok {
tid = str
}
}
if tid == "" {
tid = GenerateTid()
}
return tid
}
// GetClientIP get ip from context.Context, the client ip is set in gin.Context
func GetClientIP(ctx context.Context) string {
ip := ""
ipVal := ctx.Value(KeyClientIP)
if ipVal != nil {
if str, ok := ipVal.(string); ok {
ip = str
}
}
return ip
}
func WrapCtx(c *gin.Context) context.Context {
ctx := context.WithValue(c.Request.Context(), ContextTraceIDKey, c.GetString(ContextTraceIDKey))
for k, v := range c.Keys {
ctx = context.WithValue(ctx, k, v)
}
ctx = context.WithValue(ctx, GinContextKey, c) //nolint
return ctx
}
func ToGinCtx(ctx context.Context) (*gin.Context, error) {
if c, ok := ctx.Value(GinContextKey).(*gin.Context); ok {
return c, nil
}
return nil, ErrorGinContextNotFound
}
func GetGinCtx(ctx context.Context) (*gin.Context, error) {
if c, ok := ctx.Value(GinContextKey).(*gin.Context); ok {
return c, nil
}
return nil, ErrorGinContextNotFound
}
func GetCtxUID(c context.Context) uint {
return getCtxVal[uint](c, KeyUID)
}
func GetCtxUType(c context.Context) uint {
return getCtxVal[uint](c, KeyUType)
}
func GetCtxCompanyID(c context.Context) uint {
return getCtxVal[uint](c, KeyCompanyID)
}
func getGinVal[T any](c *gin.Context, key string) T {
val, exists := c.Get(key)
if exists {
if ret, ok := val.(T); ok {
return ret
}
}
var t T
return t
}
func getCtxVal[T any](c context.Context, key string) T {
val := c.Value(key)
if val != nil {
if ret, ok := val.(T); ok {
return ret
}
}
var t T
return t
}
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 {
return ret
}
}
return nil
}
func SetVal(ctx context.Context, key string, val interface{}) context.Context {
if ginCtx, err := GetGinCtx(ctx); err == nil && ginCtx != nil {
ginCtx.Set(key, val)
}
return context.WithValue(ctx, key, val)
}
func AddApiCost(c *gin.Context, appName string, start time.Time) {
cost := c.Writer.Header().Get(KeyCost)
currentCost := fmt.Sprintf("%s:%s", appName, formatDuration(time.Since(start)))
if cost == "" {
cost = currentCost
} else {
cost = fmt.Sprintf("%s,%s", currentCost, cost)
}
c.Writer.Header().Set(KeyCost, cost)
}
func formatDuration(d time.Duration) string {
ms := float64(d.Nanoseconds()) / 1e6
return fmt.Sprintf("%.2fms", ms)
}
func SetApiCost(ctx context.Context, header map[string][]string) {
if ginCtx, _ := GetGinCtx(ctx); ginCtx != nil && len(header) > 0 {
cost := ""
for k, v := range header {
if k == KeyCost {
cost = strings.Join(v, ",")
}
}
if cost != "" {
ginCtx.Writer.Header().Set(KeyCost, cost)
}
}
}
......@@ -2,8 +2,8 @@ package wg
import (
"context"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/logger"
ctxUtil "gitlab.wanzhuangkj.com/tush/xpkg/utils/ctxUtils"
"gitlab.wanzhuangkj.com/tush/xpkg/xerrors/xerror"
"sync"
)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论