|
@@ -20,12 +20,14 @@ type Binding struct {
|
|
|
routerGroup *gin.RouterGroup
|
|
|
}
|
|
|
|
|
|
+// NewBinding 创建版本对应的binding
|
|
|
func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
|
|
|
apiPrefix := urlPrefix + "/api"
|
|
|
if utils.IsStringNotEmpty(apiVersion) && apiVersion != "root" {
|
|
|
apiPrefix += "/" + apiVersion
|
|
|
}
|
|
|
|
|
|
+ // 把binding.Middleware转换为gin的中间件函数
|
|
|
ginMiddlewares := make([]gin.HandlerFunc, 0)
|
|
|
for _, m := range middlewares {
|
|
|
innerM := m
|
|
@@ -34,6 +36,7 @@ func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ // 返回创建的路由组
|
|
|
return &Binding{routerGroup: routerInstance.Group(apiPrefix, ginMiddlewares...)}
|
|
|
}
|
|
|
|
|
@@ -61,11 +64,12 @@ func Static(b *Binding, item *StaticBindItem) {
|
|
|
item.bind(b.routerGroup)
|
|
|
}
|
|
|
|
|
|
+// SimpleBindItem 路由条目
|
|
|
type SimpleBindItem[I any, O any] struct {
|
|
|
- Path string
|
|
|
- ResponseFunc response.SendFunc[O]
|
|
|
- BusinessFunc BusinessFunc[I, O]
|
|
|
- OptionalBindingFunc BindingFunc[O]
|
|
|
+ Path string // 请求路径
|
|
|
+ ResponseFunc response.SendFunc[O] // 响应泛型函数
|
|
|
+ BusinessFunc BusinessFunc[I, O] // 业务泛型函数
|
|
|
+ OptionalBindingFunc BindingFunc[O] //
|
|
|
}
|
|
|
|
|
|
func (item *SimpleBindItem[I, O]) bind(routerGroup *gin.RouterGroup, method string, middlewares ...middleware.Func) {
|
|
@@ -77,6 +81,7 @@ func (item *SimpleBindItem[I, O]) bind(routerGroup *gin.RouterGroup, method stri
|
|
|
bindingItem.bind(routerGroup, middlewares...)
|
|
|
}
|
|
|
|
|
|
+// BindItem 路由条目结构
|
|
|
type BindItem[I any, O any] struct {
|
|
|
Method string
|
|
|
*SimpleBindItem[I, O]
|
|
@@ -107,6 +112,7 @@ func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...mi
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 给单个路由增加中间件
|
|
|
ginHandleFunctions := make([]gin.HandlerFunc, 0)
|
|
|
for _, m := range middlewares {
|
|
|
innerM := m
|
|
@@ -119,7 +125,9 @@ func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...mi
|
|
|
bindingContext := &binding_context.Context{Context: c}
|
|
|
var inputModel I
|
|
|
|
|
|
+ // 请求的结构类型不为any
|
|
|
if inputType != nil {
|
|
|
+ // 将请求数据解析到inputModel中
|
|
|
if item.OptionalBindingFunc != nil {
|
|
|
ok := item.OptionalBindingFunc(bindingContext, &inputModel, item.ResponseFunc)
|
|
|
if !ok {
|
|
@@ -145,6 +153,7 @@ func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...mi
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 执行业务函数
|
|
|
if item.BusinessFunc != nil {
|
|
|
statusCode := http.StatusOK
|
|
|
outputModel, err := item.BusinessFunc(bindingContext, inputModel)
|
|
@@ -157,9 +166,11 @@ func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...mi
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+ // 所有的函数加入到执行链中
|
|
|
routerGroup.Handle(item.Method, item.Path, ginHandleFunctions...)
|
|
|
}
|
|
|
|
|
|
+// StaticBindItem 静态路由item
|
|
|
type StaticBindItem struct {
|
|
|
RelativePath string
|
|
|
Root string
|