feat: router.server -> router.Server
This commit is contained in:
@@ -21,19 +21,19 @@ import (
|
||||
type GetCommonParam func(ctx *gin.Context) (any, error)
|
||||
|
||||
// AddCommonParamRule 添加公共参数注入规则
|
||||
func (s *server) AddCommonParamRule(fieldName string, getParamFunc GetCommonParam) {
|
||||
func (s *Server) AddCommonParamRule(fieldName string, getParamFunc GetCommonParam) {
|
||||
s.commonParam[fieldName] = getParamFunc
|
||||
}
|
||||
|
||||
// AddCommonParamRules 批量添加公共参数注入规则
|
||||
func (s *server) AddCommonParamRules(rules map[string]GetCommonParam) {
|
||||
func (s *Server) AddCommonParamRules(rules map[string]GetCommonParam) {
|
||||
for fieldName, rule := range rules {
|
||||
s.AddCommonParamRule(fieldName, rule)
|
||||
}
|
||||
}
|
||||
|
||||
// injectCommonParam 注入公共参数
|
||||
func (s *server) injectCommonParam(ctx *gin.Context, formValue any) error {
|
||||
func (s *Server) injectCommonParam(ctx *gin.Context, formValue any) error {
|
||||
innerCtx := util.GinCtxToContext(ctx)
|
||||
var (
|
||||
val any
|
||||
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
"github.com/mcuadros/go-defaults"
|
||||
)
|
||||
|
||||
func (s *server) getFormInitValue(ctx *gin.Context, uriCfg define.UriConfig) (any, error) {
|
||||
func (s *Server) getFormInitValue(ctx *gin.Context, uriCfg define.UriConfig) (any, error) {
|
||||
var (
|
||||
formParam reflect.Value
|
||||
formValue any
|
||||
@@ -58,7 +58,7 @@ func (s *server) getFormInitValue(ctx *gin.Context, uriCfg define.UriConfig) (an
|
||||
return formValue, nil
|
||||
}
|
||||
|
||||
func (s *server) initRequest(ctx *gin.Context, uriCfg define.UriConfig) (any, reflect.Value, exception.IException) {
|
||||
func (s *Server) initRequest(ctx *gin.Context, uriCfg define.UriConfig) (any, reflect.Value, exception.IException) {
|
||||
var (
|
||||
err error
|
||||
formValue any
|
||||
@@ -92,7 +92,7 @@ func (s *server) initRequest(ctx *gin.Context, uriCfg define.UriConfig) (any, re
|
||||
}
|
||||
|
||||
// callFunc 调用方法
|
||||
func (s *server) callFunc(ctx *gin.Context, uriCfg define.UriConfig, inputValue reflect.Value) (any, exception.IException) {
|
||||
func (s *Server) callFunc(ctx *gin.Context, uriCfg define.UriConfig, inputValue reflect.Value) (any, exception.IException) {
|
||||
|
||||
var (
|
||||
firstParam = reflect.ValueOf(ctx)
|
||||
@@ -108,7 +108,7 @@ func (s *server) callFunc(ctx *gin.Context, uriCfg define.UriConfig, inputValue
|
||||
}
|
||||
|
||||
// formatError 格式化错误
|
||||
func (s *server) formatError(ctx *gin.Context, err any) exception.IException {
|
||||
func (s *Server) formatError(ctx *gin.Context, err any) exception.IException {
|
||||
if nil == err {
|
||||
return nil
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func (s *server) formatError(ctx *gin.Context, err any) exception.IException {
|
||||
}
|
||||
|
||||
// RequestHandler 获取请求处理方法
|
||||
func (s *server) RequestHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
func (s *Server) RequestHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
defer s.hook(ctx, uriCfg)
|
||||
var (
|
||||
@@ -164,7 +164,7 @@ func (s *server) RequestHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// SseHandler sse连接请求
|
||||
func (s *server) SseHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
func (s *Server) SseHandler(uriCfg define.UriConfig) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
// hook 执行hook逻辑
|
||||
func (s *server) hook(ctx *gin.Context, uriCfg define.UriConfig) {
|
||||
func (s *Server) hook(ctx *gin.Context, uriCfg define.UriConfig) {
|
||||
var (
|
||||
exists bool
|
||||
isSuccess any
|
||||
@@ -47,7 +47,7 @@ func (s *server) hook(ctx *gin.Context, uriCfg define.UriConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *server) hookAfter(ctx *gin.Context, uriCfg define.UriConfig, hookInstance *define.LogicAfterResponse, success bool) {
|
||||
func (s *Server) hookAfter(ctx *gin.Context, uriCfg define.UriConfig, hookInstance *define.LogicAfterResponse, success bool) {
|
||||
innerContext := util.GinCtxToContext(ctx)
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
|
||||
@@ -51,7 +51,7 @@ func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption
|
||||
}
|
||||
|
||||
// NewServer server实例
|
||||
func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
func NewServer(port int, optionList ...SetServerOptionFunc) *Server {
|
||||
if port < 80 {
|
||||
panic("port should be greater than 80")
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
}
|
||||
openapi.DocManager.NewOpenApiDoc(option.docConfig.Flag, optionFuncList...)
|
||||
}
|
||||
s := &server{
|
||||
s := &Server{
|
||||
router: gin.Default(),
|
||||
port: port,
|
||||
option: option,
|
||||
@@ -99,7 +99,7 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
|
||||
return s
|
||||
}
|
||||
|
||||
type server struct {
|
||||
type Server struct {
|
||||
router *gin.Engine
|
||||
port int
|
||||
option *serverOption
|
||||
@@ -111,7 +111,7 @@ type server struct {
|
||||
uriTable map[string]define.UriConfig // uri配置表
|
||||
}
|
||||
|
||||
func (s *server) getGlobalMiddlewareList(option *serverOption) {
|
||||
func (s *Server) getGlobalMiddlewareList(option *serverOption) {
|
||||
// 全局 panic 捕获
|
||||
s.globalMiddlewareList = append(s.globalMiddlewareList, middleware.CustomRecover())
|
||||
// 全局流控中间件
|
||||
@@ -151,7 +151,7 @@ func (s *server) getGlobalMiddlewareList(option *serverOption) {
|
||||
}
|
||||
|
||||
// RegisterDocHandler 注册文档路由
|
||||
func (s *server) RegisterDocHandler() {
|
||||
func (s *Server) RegisterDocHandler() {
|
||||
if !s.option.docConfig.Enable {
|
||||
return
|
||||
}
|
||||
@@ -188,7 +188,7 @@ func (s *server) RegisterDocHandler() {
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
func (s *server) Start() {
|
||||
func (s *Server) Start() {
|
||||
// 注册文档
|
||||
s.RegisterDocHandler()
|
||||
// s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
||||
@@ -207,17 +207,17 @@ func (s *server) Start() {
|
||||
}
|
||||
|
||||
// Router 对外访问路由实例, 不建议直接用
|
||||
func (s *server) Router() *gin.Engine {
|
||||
func (s *Server) Router() *gin.Engine {
|
||||
return s.router
|
||||
}
|
||||
|
||||
// Handler404 注册404的处理方法
|
||||
func (s *server) Handler404(f gin.HandlerFunc) {
|
||||
func (s *Server) Handler404(f gin.HandlerFunc) {
|
||||
s.router.NoRoute(f)
|
||||
}
|
||||
|
||||
// SetCustomRouter 自定义路由处理
|
||||
func (s *server) SetCustomRouter(f func(r *gin.Engine)) {
|
||||
func (s *Server) SetCustomRouter(f func(r *gin.Engine)) {
|
||||
if nil == f {
|
||||
return
|
||||
}
|
||||
@@ -225,7 +225,7 @@ func (s *server) SetCustomRouter(f func(r *gin.Engine)) {
|
||||
}
|
||||
|
||||
// Group 注册接口路由
|
||||
func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, controllerList ...any) {
|
||||
func (s *Server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, controllerList ...any) {
|
||||
routerGroup := s.router.Group(routerPrefix)
|
||||
routerGroup.Use(middlewareList...)
|
||||
parser := controllerParser{}
|
||||
@@ -281,7 +281,7 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co
|
||||
}
|
||||
|
||||
// registerRouter 注册路由
|
||||
func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, itemUriCfg define.UriConfig, handleFunc gin.HandlerFunc) {
|
||||
func (s *Server) registerRouter(routerGroup *gin.RouterGroup, method string, itemUriCfg define.UriConfig, handleFunc gin.HandlerFunc) {
|
||||
funcList := []gin.HandlerFunc{
|
||||
middleware.Timeout(itemUriCfg.MaxExecTime), // 超时处理
|
||||
handleFunc,
|
||||
@@ -309,7 +309,7 @@ func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, ite
|
||||
}
|
||||
|
||||
// getMiddlewareDescList 获取中间件路径的描述列表
|
||||
func (s *server) getMiddlewareDescList(middlewareList []gin.HandlerFunc) []string {
|
||||
func (s *Server) getMiddlewareDescList(middlewareList []gin.HandlerFunc) []string {
|
||||
middlewareDescList := []string{}
|
||||
for _, itemMiddleware := range middlewareList {
|
||||
middlewareValue := reflect.ValueOf(itemMiddleware)
|
||||
@@ -325,7 +325,7 @@ func (s *server) getMiddlewareDescList(middlewareList []gin.HandlerFunc) []strin
|
||||
}
|
||||
|
||||
// TableOutput 表格输出
|
||||
func (s *server) TableOutput(header []any, footer []any) {
|
||||
func (s *Server) TableOutput(header []any, footer []any) {
|
||||
// 帮助函数
|
||||
anySlice2TableRow := func(input []any) table.Row {
|
||||
tableRowData := make(table.Row, 0, len(input))
|
||||
|
||||
Reference in New Issue
Block a user