支持custom context #10
@ -8,8 +8,13 @@
|
||||
package define
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
networkUtil "git.zhangdeman.cn/zhangdeman/network/util"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/trace"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -21,3 +26,43 @@ type Context struct {
|
||||
RequestID string
|
||||
RequestTime time.Time
|
||||
}
|
||||
|
||||
// NewContext 创建context
|
||||
func NewContext(ginCtx *gin.Context) *Context {
|
||||
existCtx, exist := ginCtx.Get(CustomContextKey)
|
||||
if exist && existCtx != nil {
|
||||
if c, ok := existCtx.(*Context); ok {
|
||||
return c
|
||||
}
|
||||
}
|
||||
traceID := fmt.Sprintf(
|
||||
"%v-%v-%v-%v",
|
||||
time.Now().UnixNano()/1e6,
|
||||
strings.ReplaceAll(networkUtil.IP.GetHostIP(), ".", ""),
|
||||
strings.ReplaceAll(networkUtil.IP.GetRemoteIP(ginCtx.Request), ".", ""),
|
||||
wrapper.StringFromRandom(32, "").Md5().Value,
|
||||
)
|
||||
getRequestID := func(ctx *gin.Context, traceID string) string {
|
||||
requestID := ctx.GetHeader("X-Forward-Request-Id")
|
||||
if len(requestID) > 0 {
|
||||
return requestID
|
||||
}
|
||||
if len(traceID) > 0 {
|
||||
return traceID
|
||||
}
|
||||
return traceID
|
||||
}
|
||||
ctx := &Context{
|
||||
Gin: ginCtx,
|
||||
Trace: trace.NewRuntime(traceID, 1),
|
||||
TraceID: traceID,
|
||||
RequestID: getRequestID(ginCtx, traceID),
|
||||
RequestTime: time.Now(),
|
||||
}
|
||||
httpHandleConfig := GetHttpHandleConfig()
|
||||
ginCtx.Set(CustomContextKey, ctx)
|
||||
ginCtx.Set(httpHandleConfig.TraceIDField, traceID)
|
||||
ginCtx.Set(httpHandleConfig.RequestIDField, ctx.RequestID)
|
||||
ginCtx.Set(httpHandleConfig.StartRequestTimeField, ctx.RequestTime.UnixMilli())
|
||||
return ctx
|
||||
}
|
||||
|
@ -13,7 +13,6 @@ import (
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/request/parse_body"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/router"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -297,5 +296,5 @@ func (wh *wrapperHandle) GetLogicAfterResponse(ctx *gin.Context) *define.LogicAf
|
||||
|
||||
// GetCustomContext 获取自定义context
|
||||
func (wh *wrapperHandle) GetCustomContext(ctx *gin.Context) *define.Context {
|
||||
return router.NewContext(ctx)
|
||||
return define.NewContext(ctx)
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
// Package router ...
|
||||
//
|
||||
// Description : request ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-04-12 18:44
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
networkUtil "git.zhangdeman.cn/zhangdeman/network/util"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/trace"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// NewContext 创建context
|
||||
func NewContext(ginCtx *gin.Context) *define.Context {
|
||||
existCtx, exist := ginCtx.Get(define.CustomContextKey)
|
||||
if exist && existCtx != nil {
|
||||
if c, ok := existCtx.(*define.Context); ok {
|
||||
return c
|
||||
}
|
||||
}
|
||||
traceID := fmt.Sprintf(
|
||||
"%v-%v-%v-%v",
|
||||
time.Now().UnixNano()/1e6,
|
||||
strings.ReplaceAll(networkUtil.IP.GetHostIP(), ".", ""),
|
||||
strings.ReplaceAll(networkUtil.IP.GetRemoteIP(ginCtx.Request), ".", ""),
|
||||
wrapper.StringFromRandom(32, "").Md5().Value,
|
||||
)
|
||||
getRequestID := func(ctx *gin.Context, traceID string) string {
|
||||
requestID := ctx.GetHeader("X-Forward-Request-Id")
|
||||
if len(requestID) > 0 {
|
||||
return requestID
|
||||
}
|
||||
if len(traceID) > 0 {
|
||||
return traceID
|
||||
}
|
||||
return traceID
|
||||
}
|
||||
ctx := &define.Context{
|
||||
Gin: ginCtx,
|
||||
Trace: trace.NewRuntime(traceID, 1),
|
||||
TraceID: traceID,
|
||||
RequestID: getRequestID(ginCtx, traceID),
|
||||
RequestTime: time.Now(),
|
||||
}
|
||||
httpHandleConfig := define.GetHttpHandleConfig()
|
||||
ginCtx.Set(define.CustomContextKey, ctx)
|
||||
ginCtx.Set(httpHandleConfig.TraceIDField, traceID)
|
||||
ginCtx.Set(httpHandleConfig.RequestIDField, ctx.RequestID)
|
||||
ginCtx.Set(httpHandleConfig.StartRequestTimeField, ctx.RequestTime.UnixMilli())
|
||||
return ctx
|
||||
}
|
@ -126,17 +126,6 @@ func WithPprofEnable() SetServerOptionFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnableRequestInit 全局配置初始化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:56 2025/2/22
|
||||
func WithEnableRequestInit() SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
so.enableRequestInit = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnableCors 启用全局跨域
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
@ -12,6 +12,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gin/define"
|
||||
|
||||
apiDoc "git.zhangdeman.cn/gateway/api-doc"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
||||
@ -76,7 +78,7 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
globalMiddlewareList,
|
||||
func(ctx *gin.Context) {
|
||||
// 初始化上下文以及基础信息
|
||||
_ = NewContext(ctx)
|
||||
_ = define.NewContext(ctx)
|
||||
},
|
||||
)
|
||||
if nil != option.loggerCfg {
|
||||
|
Loading…
x
Reference in New Issue
Block a user