优化server初始化的方式
This commit is contained in:
parent
5d19efa8cf
commit
a50883b734
@ -32,7 +32,7 @@ type Test struct {
|
|||||||
|
|
||||||
func Test_parseController(t *testing.T) {
|
func Test_parseController(t *testing.T) {
|
||||||
SetValidateErrTag("err_msg")
|
SetValidateErrTag("err_msg")
|
||||||
r := gin.Default()
|
s := NewServer(8080, nil)
|
||||||
Group(r, "test", nil, TestController{})
|
s.Group("test", nil, TestController{})
|
||||||
r.Run(":8080")
|
s.Start()
|
||||||
}
|
}
|
||||||
|
@ -4,23 +4,53 @@
|
|||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 2025-01-27 19:33
|
// Date : 2025-02-07 18:19
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewServer server实例
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 18:20 2025/2/7
|
||||||
|
func NewServer(port int, option any) *server {
|
||||||
|
if port < 80 {
|
||||||
|
panic("port should be greater than 80")
|
||||||
|
}
|
||||||
|
return &server{
|
||||||
|
router: gin.Default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type server struct {
|
||||||
|
router *gin.Engine
|
||||||
|
port int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start 启动服务
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 18:31 2025/2/7
|
||||||
|
func (s *server) Start() {
|
||||||
|
if err := s.router.Run(fmt.Sprintf(":%d", s.port)); err != nil {
|
||||||
|
panic("服务启动监听失败" + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Group 注册接口路由
|
// Group 注册接口路由
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 19:35 2025/1/27
|
// Date : 19:35 2025/1/27
|
||||||
func Group(router *gin.Engine, routerPrefix string, middlewareList []gin.HandlerFunc, cList ...any) error {
|
func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cList ...any) {
|
||||||
g := router.Group(routerPrefix)
|
g := s.router.Group(routerPrefix)
|
||||||
g.Use(middlewareList...)
|
g.Use(middlewareList...)
|
||||||
cParser := controller{}
|
cParser := controller{}
|
||||||
for _, c := range cList {
|
for _, c := range cList {
|
||||||
@ -43,9 +73,8 @@ func Group(router *gin.Engine, routerPrefix string, middlewareList []gin.Handler
|
|||||||
case http.MethodOptions:
|
case http.MethodOptions:
|
||||||
g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||||
case http.MethodTrace:
|
case http.MethodTrace:
|
||||||
return errors.New(`method Trace is not supported`)
|
panic(`method Trace is not supported`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user