feat: remove custom context
This commit is contained in:
@ -1,12 +0,0 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-04-12 20:18
|
||||
package define
|
||||
|
||||
const (
|
||||
CustomContextKey = "_CUSTOM_CONTEXT" // 自定义context
|
||||
)
|
||||
@ -1,68 +0,0 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-04-12 20:57
|
||||
package define
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
networkUtil "git.zhangdeman.cn/zhangdeman/network/util"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/op_string"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/trace"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Context *gin.Context // 继承 gin context
|
||||
Trace *trace.Runtime // trace 实例
|
||||
TraceID string
|
||||
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), ".", ""),
|
||||
op_string.Md5(op_string.Random(32, "")),
|
||||
)
|
||||
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{
|
||||
Context: 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
|
||||
}
|
||||
@ -12,10 +12,6 @@ import "strings"
|
||||
var defaultValidateErrTag = "err"
|
||||
|
||||
// SetValidateErrTag 设置验证失败时, 获取错误信息的tag字段
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:42 2025/2/7
|
||||
func SetValidateErrTag(tagName string) {
|
||||
tagName = strings.TrimSpace(tagName)
|
||||
if tagName == "" {
|
||||
|
||||
@ -13,10 +13,6 @@ import (
|
||||
)
|
||||
|
||||
// controller 解析controller有哪些方法要注册为接口
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:30 2025/1/27
|
||||
type controller struct {
|
||||
}
|
||||
|
||||
@ -71,7 +67,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
}
|
||||
// 第一个参数必须是 *gin.Context 或者 *define.Context
|
||||
paramOne := methodType.In(1).String()
|
||||
if paramOne != GinContextType && paramOne != CustomContextType {
|
||||
if paramOne != GinContextType {
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
@ -107,7 +103,6 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
|
||||
}
|
||||
}
|
||||
// 解析meta信息
|
||||
cfg.CtxType = paramOne
|
||||
cfg.Path = metaField.Tag.Get(TagNamePath)
|
||||
cfg.RequestMethod = metaField.Tag.Get(TagNameMethod)
|
||||
cfg.Desc = metaField.Tag.Get(TagNameDesc)
|
||||
|
||||
@ -15,7 +15,6 @@ const (
|
||||
PrefixFuncName = "RouterPrefix" // 路由前缀函数名称
|
||||
MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称
|
||||
GinContextType = "*gin.Context" // gin context 类型名称
|
||||
CustomContextType = "*define.Context" // custom context 类型名称
|
||||
ErrorType = "error" // error类型
|
||||
ErrorInterfaceFuncName = "Error" // error接口需要实现的方法名称
|
||||
)
|
||||
@ -42,7 +41,6 @@ type UriConfig struct {
|
||||
TagList []string `json:"tag_list"` // 接口分组
|
||||
Desc string `json:"desc"` // 接口描述
|
||||
OutputStrict bool `json:"output_strict"` // 接口是否为严格模式 : 不配置,可返回任意类型, 配置, 必须返回结构体或者map
|
||||
CtxType string `json:"ctx_type"` // ctx参数类型
|
||||
FormDataType reflect.Type `json:"-"` // 表单数据类型
|
||||
ResultDataType reflect.Type `json:"-"` // 返回值数据类型
|
||||
ApiStructValue reflect.Value `json:"-"` // 逻辑函数所属结构体取值
|
||||
|
||||
@ -114,12 +114,8 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
if uriCfg.FormDataType.Kind() != reflect.Ptr {
|
||||
inputValue = inputValue.Elem()
|
||||
}
|
||||
if uriCfg.CtxType == CustomContextType {
|
||||
customCtx := ctx.MustGet(define.CustomContextKey)
|
||||
firstParam = reflect.ValueOf(customCtx)
|
||||
} else {
|
||||
|
||||
firstParam = reflect.ValueOf(ctx)
|
||||
}
|
||||
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, firstParam, inputValue})
|
||||
if resList[1].IsNil() {
|
||||
// 请求成功
|
||||
|
||||
@ -13,7 +13,8 @@ package router
|
||||
//
|
||||
// method: 请求方法: get/post 等
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:40 2024/7/20
|
||||
// 使用示例如下:
|
||||
// type RequestForm struct {
|
||||
// Meta `json:"-" path:"接口路由" method:"get,post" sse:"1" tag:"接口分组标签" summary:"接口描述"`
|
||||
// }
|
||||
type Meta struct{}
|
||||
|
||||
Reference in New Issue
Block a user