Merge pull request 'server初始化支持设置各种option' (#5) from feature/support_server_option into master
Reviewed-on: #5
This commit is contained in:
commit
26476ee23b
103
router/option.go
103
router/option.go
@ -6,3 +6,106 @@
|
|||||||
//
|
//
|
||||||
// Date : 2025-02-18 17:26
|
// Date : 2025-02-18 17:26
|
||||||
package router
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
apiDoc "git.zhangdeman.cn/gateway/api-doc"
|
apiDoc "git.zhangdeman.cn/gateway/api-doc"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
"git.zhangdeman.cn/zhangdeman/gin/middleware"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -18,32 +19,72 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"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实例
|
// NewServer server实例
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:20 2025/2/7
|
// Date : 18:20 2025/2/7
|
||||||
func NewServer(port int, option any) *server {
|
func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||||
if port < 80 {
|
if port < 80 {
|
||||||
panic("port should be greater than 80")
|
panic("port should be greater than 80")
|
||||||
}
|
}
|
||||||
|
option := newServerOption(port, optionList...)
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.Use(
|
r.Use(
|
||||||
// 初始化请求
|
// 初始化请求
|
||||||
middleware.InitRequest(),
|
middleware.InitRequest(), // 初始化请求
|
||||||
/*request_cors.New(request_cors.Config{
|
/*request_cors.New(request_cors.Config{
|
||||||
AllowAllOrigins: true,
|
AllowAllOrigins: true,
|
||||||
}), */// middleware.Ac
|
}), */// middleware.Ac
|
||||||
)
|
)
|
||||||
|
if nil != option.globalMiddlewareList {
|
||||||
|
// 启用全局中间件
|
||||||
|
r.Use(option.globalMiddlewareList...)
|
||||||
|
}
|
||||||
return &server{
|
return &server{
|
||||||
router: r,
|
router: r,
|
||||||
uiInstance: apiDoc.NewSwaggerUI(nil, []*apiDocDefine.ServerItem{
|
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, option.swaggerUiTheme),
|
||||||
{
|
port: port,
|
||||||
Url: fmt.Sprintf("http://127.0.0.1:%d", port),
|
option: option,
|
||||||
Description: "测试服务器",
|
|
||||||
},
|
|
||||||
}, apiDocDefine.SwaggerUIThemeRedocFree), // TODO : 配置相关信息
|
|
||||||
port: port,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,6 +92,7 @@ type server struct {
|
|||||||
router *gin.Engine
|
router *gin.Engine
|
||||||
port int
|
port int
|
||||||
uiInstance *apiDoc.SwaggerUI
|
uiInstance *apiDoc.SwaggerUI
|
||||||
|
option *serverOption
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start 启动服务
|
// Start 启动服务
|
||||||
@ -60,12 +102,21 @@ type server struct {
|
|||||||
// Date : 18:31 2025/2/7
|
// Date : 18:31 2025/2/7
|
||||||
func (s *server) Start() {
|
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 {
|
if err := s.router.Run(fmt.Sprintf(":%d", s.port)); err != nil {
|
||||||
panic("服务启动监听失败" + err.Error())
|
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 注册接口路由
|
// Group 注册接口路由
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user