feat: 优化控制台输出 + 修复请求BUG
This commit is contained in:
143
router/server.go
143
router/server.go
@@ -10,10 +10,15 @@ package router
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/graceful"
|
||||
"github.com/jedib0t/go-pretty/v6/table"
|
||||
"github.com/jedib0t/go-pretty/v6/text"
|
||||
|
||||
apiDoc "git.zhangdeman.cn/zhangdeman/api-doc"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
@@ -69,7 +74,41 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
if port < 80 {
|
||||
panic("port should be greater than 80")
|
||||
}
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
option := newServerOption(port, optionList...)
|
||||
|
||||
s := &server{
|
||||
router: gin.Default(),
|
||||
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
|
||||
port: port,
|
||||
option: option,
|
||||
lock: &sync.RWMutex{},
|
||||
commonParam: map[string]GetCommonParam{},
|
||||
consoleOutput: [][]any{},
|
||||
globalMiddlewareDescList: []string{},
|
||||
}
|
||||
globalMiddlewareList := s.getGlobalMiddlewareList(option)
|
||||
// 注册全局中间件
|
||||
s.router.Use(globalMiddlewareList...)
|
||||
// 启用pprof, 注册相关路由
|
||||
if option.enablePprof {
|
||||
pprof.Register(s.router)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
type server struct {
|
||||
router *gin.Engine
|
||||
port int
|
||||
uiInstance *apiDoc.SwaggerUI
|
||||
option *serverOption
|
||||
commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
|
||||
lock *sync.RWMutex
|
||||
consoleOutput [][]any // 控制台输出的提示信息
|
||||
globalMiddlewareDescList []string // 全局中间件描述列表
|
||||
}
|
||||
|
||||
func (s *server) getGlobalMiddlewareList(option *serverOption) []gin.HandlerFunc {
|
||||
globalMiddlewareList := make([]gin.HandlerFunc, 0)
|
||||
if nil != option.initContextData {
|
||||
globalMiddlewareList = append(globalMiddlewareList, option.initContextData) // 初始化一些全局的变量
|
||||
@@ -102,32 +141,8 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
// 自定义全局中间件追加
|
||||
globalMiddlewareList = append(globalMiddlewareList, option.globalMiddlewareList...)
|
||||
}
|
||||
r := gin.Default()
|
||||
|
||||
// 注册全局中间件
|
||||
r.Use(globalMiddlewareList...)
|
||||
|
||||
// 启用pprof, 注册相关路由
|
||||
if option.enablePprof {
|
||||
pprof.Register(r)
|
||||
}
|
||||
return &server{
|
||||
router: r,
|
||||
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
|
||||
port: port,
|
||||
option: option,
|
||||
lock: &sync.RWMutex{},
|
||||
commonParam: map[string]GetCommonParam{},
|
||||
}
|
||||
}
|
||||
|
||||
type server struct {
|
||||
router *gin.Engine
|
||||
port int
|
||||
uiInstance *apiDoc.SwaggerUI
|
||||
option *serverOption
|
||||
commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
|
||||
lock *sync.RWMutex
|
||||
s.globalMiddlewareDescList = s.getMiddlewareDescList(globalMiddlewareList)
|
||||
return globalMiddlewareList
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
@@ -138,6 +153,7 @@ func (s *server) Start() {
|
||||
defer func() {
|
||||
_ = gracefulServer.Close()
|
||||
}()
|
||||
s.TableOutput(nil, nil) // 控制台打印路由表格
|
||||
if err := gracefulServer.ListenAndServe(); err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
fmt.Println("接收到退出指令, 服务平滑关闭")
|
||||
@@ -170,9 +186,21 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
routerGroup := s.router.Group(routerPrefix)
|
||||
routerGroup.Use(middlewareList...)
|
||||
parser := controllerParser{}
|
||||
middlewareDescList := append([]string{}, s.globalMiddlewareDescList...)
|
||||
middlewareDescList = append(middlewareDescList, s.getMiddlewareDescList(middlewareList)...)
|
||||
for _, itemController := range controllerList {
|
||||
urlTable := parser.Parse(itemController)
|
||||
for _, itemUriCfg := range urlTable {
|
||||
// 设置代理请求函数描述
|
||||
apiMiddlewareList := append([]string{}, middlewareDescList...)
|
||||
if itemUriCfg.IsSse {
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(reflect.ValueOf(s.SseHandler).Pointer()).Name())
|
||||
} else {
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(reflect.ValueOf(s.RequestHandler).Pointer()).Name())
|
||||
}
|
||||
// 设置 logic 函数描述
|
||||
apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(itemUriCfg.ApiLogicFunc.Func.Pointer()).Name())
|
||||
|
||||
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
||||
// 普通 HTTP 请求
|
||||
handleFunc := s.RequestHandler(itemUriCfg)
|
||||
@@ -182,6 +210,14 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
}
|
||||
// 一个接口支持注册多种请求方法
|
||||
for _, method := range itemUriCfg.RequestMethod {
|
||||
if len(routerPrefix) > 0 {
|
||||
routerPrefix = "/" + strings.TrimSuffix(strings.TrimPrefix(routerPrefix, "/"), "/")
|
||||
}
|
||||
s.consoleOutput = append(s.consoleOutput, []any{
|
||||
routerPrefix + "/" + strings.TrimPrefix(itemUriCfg.Path, "/"),
|
||||
strings.ToUpper(method),
|
||||
strings.Join(apiMiddlewareList, "\n"),
|
||||
})
|
||||
s.registerRouter(routerGroup, method, itemUriCfg, handleFunc)
|
||||
}
|
||||
}
|
||||
@@ -211,3 +247,58 @@ func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, ite
|
||||
panic("method " + method + " is not support")
|
||||
}
|
||||
}
|
||||
|
||||
// getMiddlewareDescList 获取中间件路径的描述列表
|
||||
func (s *server) getMiddlewareDescList(middlewareList []gin.HandlerFunc) []string {
|
||||
middlewareDescList := []string{}
|
||||
for _, itemMiddleware := range middlewareList {
|
||||
middlewareValue := reflect.ValueOf(itemMiddleware)
|
||||
// 获取函数的指针
|
||||
funcPointer := middlewareValue.Pointer()
|
||||
// 通过 runtime 获取函数信息
|
||||
funcInfo := runtime.FuncForPC(funcPointer)
|
||||
// 获取完整的函数名称(包含包路径)
|
||||
fullName := funcInfo.Name()
|
||||
middlewareDescList = append(middlewareDescList, fullName)
|
||||
}
|
||||
return middlewareDescList
|
||||
}
|
||||
|
||||
// TableOutput 表格输出
|
||||
func (s *server) TableOutput(header []any, footer []any) {
|
||||
// 帮助函数
|
||||
anySlice2TableRow := func(input []any) table.Row {
|
||||
tableRowData := make(table.Row, 0, len(input))
|
||||
for _, d := range input {
|
||||
tableRowData = append(tableRowData, d)
|
||||
}
|
||||
return tableRowData
|
||||
}
|
||||
// 实例化一个表格
|
||||
t := table.NewWriter()
|
||||
t.SetColumnConfigs([]table.ColumnConfig{
|
||||
{
|
||||
Number: 1,
|
||||
AutoMerge: false,
|
||||
VAlign: text.VAlignMiddle,
|
||||
},
|
||||
{
|
||||
Number: 2,
|
||||
AutoMerge: false,
|
||||
VAlign: text.VAlignMiddle,
|
||||
},
|
||||
})
|
||||
// 标准输出
|
||||
t.SetOutputMirror(os.Stdout)
|
||||
if len(header) > 0 {
|
||||
t.AppendHeader(anySlice2TableRow(header))
|
||||
}
|
||||
for _, row := range s.consoleOutput {
|
||||
t.AppendRow(anySlice2TableRow(row))
|
||||
t.AppendSeparator()
|
||||
}
|
||||
if len(footer) > 0 {
|
||||
t.AppendFooter(anySlice2TableRow(footer))
|
||||
}
|
||||
t.Render()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user