优化注册全局中间件逻辑
This commit is contained in:
parent
75cc21c494
commit
b9a7f97342
@ -28,6 +28,8 @@ type serverOption struct {
|
|||||||
serverInfo *apiDocDefine.Info // 服务器信息
|
serverInfo *apiDocDefine.Info // 服务器信息
|
||||||
serverList []*apiDocDefine.ServerItem // 服务器环境列表
|
serverList []*apiDocDefine.ServerItem // 服务器环境列表
|
||||||
enablePprof bool // 启用pprof
|
enablePprof bool // 启用pprof
|
||||||
|
enableRequestInit bool // 初始化请求,生成trace_id 设置请求时间等
|
||||||
|
enableCors bool // 启动跨域支持
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithSwaggerUITheme 设置swaggerUI主题
|
// WithSwaggerUITheme 设置swaggerUI主题
|
||||||
@ -121,3 +123,25 @@ func WithPprofEnable() SetServerOptionFunc {
|
|||||||
so.enablePprof = true
|
so.enablePprof = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 14:56 2025/2/22
|
||||||
|
func WithEnableCors() SetServerOptionFunc {
|
||||||
|
return func(so *serverOption) {
|
||||||
|
so.enableCors = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
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"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/gin/middleware/request_cors"
|
||||||
"github.com/gin-contrib/pprof"
|
"github.com/gin-contrib/pprof"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -68,19 +69,24 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
|||||||
panic("port should be greater than 80")
|
panic("port should be greater than 80")
|
||||||
}
|
}
|
||||||
option := newServerOption(port, optionList...)
|
option := newServerOption(port, optionList...)
|
||||||
|
globalMiddlewareList := make([]gin.HandlerFunc, 0)
|
||||||
|
if option.enableRequestInit {
|
||||||
|
globalMiddlewareList = append(globalMiddlewareList, middleware.InitRequest())
|
||||||
|
}
|
||||||
|
if option.enableCors {
|
||||||
|
globalMiddlewareList = append(globalMiddlewareList, request_cors.New(request_cors.Config{
|
||||||
|
AllowAllOrigins: true,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
if len(option.globalMiddlewareList) > 0 {
|
||||||
|
// 自定义全局中间件追加
|
||||||
|
globalMiddlewareList = append(globalMiddlewareList, option.globalMiddlewareList...)
|
||||||
|
}
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.Use(
|
// 注册全局中间件
|
||||||
// 初始化请求
|
r.Use(globalMiddlewareList...)
|
||||||
middleware.InitRequest(), // 初始化请求
|
|
||||||
/*request_cors.New(request_cors.Config{
|
|
||||||
AllowAllOrigins: true,
|
|
||||||
}), */// middleware.Ac
|
|
||||||
)
|
|
||||||
if nil != option.globalMiddlewareList {
|
|
||||||
// 启用全局中间件
|
|
||||||
r.Use(option.globalMiddlewareList...)
|
|
||||||
}
|
|
||||||
// 启用pprof, 注册相关路由
|
// 启用pprof, 注册相关路由
|
||||||
if option.enablePprof {
|
if option.enablePprof {
|
||||||
pprof.Register(r)
|
pprof.Register(r)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user