server初始化支持设置各种option
This commit is contained in:
parent
74dde8b41e
commit
1c2c5aa4ce
103
router/option.go
103
router/option.go
@ -6,3 +6,106 @@
|
||||
//
|
||||
// Date : 2025-02-18 17:26
|
||||
package router
|
||||
|
||||
import (
|
||||
apiDocDefine "git.zhangdeman.cn/gateway/api-doc/define"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SetServerOptionFunc func(so *serverOption)
|
||||
|
||||
// serverOption 获取server实例的选项
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:28 2025/2/18
|
||||
type serverOption struct {
|
||||
swaggerUiTheme string // swagger 主题
|
||||
swaggerBaseUri string // swagger基础path
|
||||
globalMiddlewareList []gin.HandlerFunc // 全局中间件列表
|
||||
disableSwaggerDoc bool // 禁用swagger文档, 特定环境不想展示文档, 可通过次方式禁用
|
||||
serverInfo *apiDocDefine.Info // 服务器信息
|
||||
serverList []*apiDocDefine.ServerItem // 服务器环境列表
|
||||
}
|
||||
|
||||
// WithSwaggerUITheme 设置swaggerUI主题
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:29 2025/2/18
|
||||
func WithSwaggerUITheme(uiTheme string) SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
uiTheme = strings.TrimSpace(uiTheme)
|
||||
if len(uiTheme) == 0 {
|
||||
return
|
||||
}
|
||||
so.swaggerUiTheme = uiTheme
|
||||
}
|
||||
}
|
||||
|
||||
// WithGlobalMiddlewareList 设置全局中间件
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:33 2025/2/18
|
||||
func WithGlobalMiddlewareList(middlewareList ...gin.HandlerFunc) SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
so.globalMiddlewareList = middlewareList
|
||||
}
|
||||
}
|
||||
|
||||
// WithSwaggerBaseUri ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:05 2025/2/18
|
||||
func WithSwaggerBaseUri(baseUri string) SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
baseUri = strings.TrimSpace(baseUri)
|
||||
if len(baseUri) == 0 {
|
||||
return
|
||||
}
|
||||
baseUri = "/" + strings.TrimLeft(baseUri, "/")
|
||||
so.swaggerBaseUri = baseUri
|
||||
}
|
||||
}
|
||||
|
||||
// WithDisableSwaggerDoc 禁用swagger文档
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:35 2025/2/18
|
||||
func WithDisableSwaggerDoc() SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
so.disableSwaggerDoc = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithServerInfo 设置serverInfo
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:51 2025/2/18
|
||||
func WithServerInfo(serverInfo *apiDocDefine.Info) SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
if nil == serverInfo {
|
||||
return
|
||||
}
|
||||
so.serverInfo = serverInfo
|
||||
}
|
||||
}
|
||||
|
||||
// WithServerList 设置服务器列表
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:52 2025/2/18
|
||||
func WithServerList(serverList []*apiDocDefine.ServerItem) SetServerOptionFunc {
|
||||
return func(so *serverOption) {
|
||||
if len(serverList) == 0 {
|
||||
return
|
||||
}
|
||||
so.serverList = serverList
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ package router
|
||||
import (
|
||||
"fmt"
|
||||
apiDoc "git.zhangdeman.cn/gateway/api-doc"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -18,32 +19,72 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption {
|
||||
option := &serverOption{
|
||||
swaggerUiTheme: apiDocDefine.SwaggerUIThemeRedocFree,
|
||||
swaggerBaseUri: "/doc/swagger",
|
||||
globalMiddlewareList: nil,
|
||||
disableSwaggerDoc: false,
|
||||
serverInfo: &apiDocDefine.Info{
|
||||
Description: "这是一个微服务,提供一些必要的的数据接口功能",
|
||||
Title: "微服务接口文档",
|
||||
TermsOfService: "",
|
||||
Contact: &apiDocDefine.Contact{
|
||||
Name: "开发人员",
|
||||
Url: "",
|
||||
Email: "developer@example.com",
|
||||
},
|
||||
License: &apiDocDefine.License{
|
||||
Name: consts.LicenseApache20,
|
||||
Url: consts.LicenseUrlTable[consts.LicenseApache20],
|
||||
},
|
||||
Version: "0.0.1",
|
||||
},
|
||||
serverList: []*apiDocDefine.ServerItem{
|
||||
{
|
||||
Url: fmt.Sprintf("http://127.0.0.1:%d", port),
|
||||
Description: "测试服务器",
|
||||
Variables: nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, opt := range optionList {
|
||||
if nil == opt {
|
||||
continue
|
||||
}
|
||||
opt(option)
|
||||
}
|
||||
return option
|
||||
}
|
||||
|
||||
// NewServer server实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:20 2025/2/7
|
||||
func NewServer(port int, option any) *server {
|
||||
func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
if port < 80 {
|
||||
panic("port should be greater than 80")
|
||||
}
|
||||
option := newServerOption(port, optionList...)
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(
|
||||
// 初始化请求
|
||||
middleware.InitRequest(),
|
||||
middleware.InitRequest(), // 初始化请求
|
||||
/*request_cors.New(request_cors.Config{
|
||||
AllowAllOrigins: true,
|
||||
}), */// middleware.Ac
|
||||
)
|
||||
if nil != option.globalMiddlewareList {
|
||||
// 启用全局中间件
|
||||
r.Use(option.globalMiddlewareList...)
|
||||
}
|
||||
return &server{
|
||||
router: r,
|
||||
uiInstance: apiDoc.NewSwaggerUI(nil, []*apiDocDefine.ServerItem{
|
||||
{
|
||||
Url: fmt.Sprintf("http://127.0.0.1:%d", port),
|
||||
Description: "测试服务器",
|
||||
},
|
||||
}, apiDocDefine.SwaggerUIThemeRedocFree), // TODO : 配置相关信息
|
||||
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, option.swaggerUiTheme),
|
||||
port: port,
|
||||
option: option,
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +92,7 @@ type server struct {
|
||||
router *gin.Engine
|
||||
port int
|
||||
uiInstance *apiDoc.SwaggerUI
|
||||
option *serverOption
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
@ -60,12 +102,21 @@ type server struct {
|
||||
// Date : 18:31 2025/2/7
|
||||
func (s *server) Start() {
|
||||
// 注册文档
|
||||
s.uiInstance.RegisterHandler(s.router, "/doc/swagger")
|
||||
s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
||||
if err := s.router.Run(fmt.Sprintf(":%d", s.port)); err != nil {
|
||||
panic("服务启动监听失败" + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Router 对外访问路由实例, 不建议直接用
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:35 2025/2/18
|
||||
func (s *server) Router() *gin.Engine {
|
||||
return s.router
|
||||
}
|
||||
|
||||
// Group 注册接口路由
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
Loading…
x
Reference in New Issue
Block a user