|
|
@ -8,7 +8,6 @@
|
|
|
|
package router
|
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
@ -26,7 +25,7 @@ import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption {
|
|
|
|
func newServerOption(optionList ...SetServerOptionFunc) *serverOption {
|
|
|
|
option := &serverOption{
|
|
|
|
option := &serverOption{
|
|
|
|
swaggerUiTheme: apiDocDefine.SwaggerUIThemeRedocFree,
|
|
|
|
swaggerUiTheme: apiDocDefine.SwaggerUIThemeRedocFree,
|
|
|
|
swaggerBaseUri: "/doc/swagger",
|
|
|
|
swaggerBaseUri: "/doc/swagger",
|
|
|
@ -49,7 +48,7 @@ func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption
|
|
|
|
},
|
|
|
|
},
|
|
|
|
serverList: []*apiDocDefine.ServerItem{
|
|
|
|
serverList: []*apiDocDefine.ServerItem{
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Url: fmt.Sprintf("http://127.0.0.1:%d", port),
|
|
|
|
Url: "",
|
|
|
|
Description: "测试服务器",
|
|
|
|
Description: "测试服务器",
|
|
|
|
Variables: nil,
|
|
|
|
Variables: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -64,16 +63,9 @@ func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption
|
|
|
|
return option
|
|
|
|
return option
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewServer server实例
|
|
|
|
// NewServer server实例, 传入optionList, 可以自定义一些配置
|
|
|
|
//
|
|
|
|
func NewServer(optionList ...SetServerOptionFunc) *server {
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
option := newServerOption(optionList...)
|
|
|
|
//
|
|
|
|
|
|
|
|
// Date : 18:20 2025/2/7
|
|
|
|
|
|
|
|
func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
|
|
|
|
|
|
|
if port < 80 {
|
|
|
|
|
|
|
|
panic("port should be greater than 80")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
option := newServerOption(port, optionList...)
|
|
|
|
|
|
|
|
globalMiddlewareList := make([]gin.HandlerFunc, 0)
|
|
|
|
globalMiddlewareList := make([]gin.HandlerFunc, 0)
|
|
|
|
if nil != option.initContextData {
|
|
|
|
if nil != option.initContextData {
|
|
|
|
globalMiddlewareList = append(globalMiddlewareList, option.initContextData) // 初始化一些全局的变量
|
|
|
|
globalMiddlewareList = append(globalMiddlewareList, option.initContextData) // 初始化一些全局的变量
|
|
|
@ -126,14 +118,12 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
|
|
|
return &server{
|
|
|
|
return &server{
|
|
|
|
router: r,
|
|
|
|
router: r,
|
|
|
|
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, option.swaggerUiTheme),
|
|
|
|
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, option.swaggerUiTheme),
|
|
|
|
port: port,
|
|
|
|
|
|
|
|
option: option,
|
|
|
|
option: option,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
type server struct {
|
|
|
|
router *gin.Engine
|
|
|
|
router *gin.Engine
|
|
|
|
port int
|
|
|
|
|
|
|
|
uiInstance *apiDoc.SwaggerUI
|
|
|
|
uiInstance *apiDoc.SwaggerUI
|
|
|
|
option *serverOption
|
|
|
|
option *serverOption
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -143,13 +133,12 @@ type server struct {
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Date : 18:31 2025/2/7
|
|
|
|
// Date : 18:31 2025/2/7
|
|
|
|
func (s *server) Start() {
|
|
|
|
func (s *server) Start(listenAddr string) {
|
|
|
|
// 注册文档
|
|
|
|
// 注册文档
|
|
|
|
s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
|
|
|
s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
|
|
|
gracefulServer := graceful.NewServer(fmt.Sprintf(":%d", s.port), s.Router())
|
|
|
|
gracefulServer := graceful.NewServer(listenAddr, s.Router())
|
|
|
|
if err := gracefulServer.ListenAndServe(); err != nil {
|
|
|
|
if err := gracefulServer.ListenAndServe(); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
fmt.Println("接收到退出指令, 服务平滑关闭")
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("服务启动监听失败" + err.Error())
|
|
|
|
panic("服务启动监听失败" + err.Error())
|
|
|
|