支持custom context #10

Merged
zhangdeman merged 5 commits from feature/upgrade_context into master 2025-04-13 15:46:22 +08:00
5 changed files with 49 additions and 76 deletions
Showing only changes of commit 5df4fbab16 - Show all commits

View File

@ -8,8 +8,13 @@
package define package define
import ( import (
"fmt"
"strings"
"time" "time"
networkUtil "git.zhangdeman.cn/zhangdeman/network/util"
"git.zhangdeman.cn/zhangdeman/wrapper"
"git.zhangdeman.cn/zhangdeman/trace" "git.zhangdeman.cn/zhangdeman/trace"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -21,3 +26,43 @@ type Context struct {
RequestID string RequestID string
RequestTime time.Time 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
}

View File

@ -13,7 +13,6 @@ import (
"git.zhangdeman.cn/zhangdeman/gin/define" "git.zhangdeman.cn/zhangdeman/gin/define"
"git.zhangdeman.cn/zhangdeman/gin/request/parse_body" "git.zhangdeman.cn/zhangdeman/gin/request/parse_body"
"git.zhangdeman.cn/zhangdeman/gin/router"
"git.zhangdeman.cn/zhangdeman/wrapper" "git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -297,5 +296,5 @@ func (wh *wrapperHandle) GetLogicAfterResponse(ctx *gin.Context) *define.LogicAf
// GetCustomContext 获取自定义context // GetCustomContext 获取自定义context
func (wh *wrapperHandle) GetCustomContext(ctx *gin.Context) *define.Context { func (wh *wrapperHandle) GetCustomContext(ctx *gin.Context) *define.Context {
return router.NewContext(ctx) return define.NewContext(ctx)
} }

View File

@ -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
}

View File

@ -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 启用全局跨域 // WithEnableCors 启用全局跨域
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

View File

@ -12,6 +12,8 @@ import (
"net/http" "net/http"
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/gin/define"
apiDoc "git.zhangdeman.cn/gateway/api-doc" apiDoc "git.zhangdeman.cn/gateway/api-doc"
"git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/gin/middleware" "git.zhangdeman.cn/zhangdeman/gin/middleware"
@ -76,7 +78,7 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
globalMiddlewareList, globalMiddlewareList,
func(ctx *gin.Context) { func(ctx *gin.Context) {
// 初始化上下文以及基础信息 // 初始化上下文以及基础信息
_ = NewContext(ctx) _ = define.NewContext(ctx)
}, },
) )
if nil != option.loggerCfg { if nil != option.loggerCfg {