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