|
|
@@ -12,38 +12,44 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
-var globalInfrastructure *infrastructure.Infrastructure
|
|
|
+type Binder struct {
|
|
|
+ router api.Router
|
|
|
+ i *infrastructure.Infrastructure
|
|
|
+}
|
|
|
|
|
|
-func SetGlobalInfrastructure(i *infrastructure.Infrastructure) {
|
|
|
- globalInfrastructure = i
|
|
|
+func NewBinder(router api.Router, i *infrastructure.Infrastructure) *Binder {
|
|
|
+ return &Binder{
|
|
|
+ router: router,
|
|
|
+ i: i,
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func PostBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
- item.bind(router, http.MethodPost, middlewares...)
|
|
|
+func PostBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
+ item.bind(binder, http.MethodPost, middlewares...)
|
|
|
}
|
|
|
|
|
|
-func DeleteBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
- item.bind(router, http.MethodDelete, middlewares...)
|
|
|
+func DeleteBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
+ item.bind(binder, http.MethodDelete, middlewares...)
|
|
|
}
|
|
|
|
|
|
-func PutBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
- item.bind(router, http.MethodPut, middlewares...)
|
|
|
+func PutBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
+ item.bind(binder, http.MethodPut, middlewares...)
|
|
|
}
|
|
|
|
|
|
-func GetBind[O any](router api.Router, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
- item.bind(router, http.MethodGet, middlewares...)
|
|
|
+func GetBind[O any](binder *Binder, item *SimpleBindItem[O], middlewares ...api.Handler) {
|
|
|
+ item.bind(binder, http.MethodGet, middlewares...)
|
|
|
}
|
|
|
|
|
|
-func Bind[O any](router api.Router, item *BindItem[O], middlewares ...api.Handler) {
|
|
|
- item.bind(router, middlewares...)
|
|
|
+func Bind[O any](binder *Binder, item *BindItem[O], middlewares ...api.Handler) {
|
|
|
+ item.bind(binder, middlewares...)
|
|
|
}
|
|
|
|
|
|
-func Static(router api.Router, item *StaticBindItem) {
|
|
|
- item.bind(router)
|
|
|
+func Static(binder *Binder, item *StaticBindItem) {
|
|
|
+ item.bind(binder)
|
|
|
}
|
|
|
|
|
|
-func StaticFile(router api.Router, item *StaticFileBindItem) {
|
|
|
- item.bind(router)
|
|
|
+func StaticFile(binder *Binder, item *StaticFileBindItem) {
|
|
|
+ item.bind(binder)
|
|
|
}
|
|
|
|
|
|
type DTOBindFunc func(c *api.Context, dto DTO) error
|
|
|
@@ -87,13 +93,13 @@ type SimpleBindItem[O any] struct {
|
|
|
ResponseFunc ResponseFunc[O]
|
|
|
}
|
|
|
|
|
|
-func (item *SimpleBindItem[O]) bind(router api.Router, method string, middlewares ...api.Handler) {
|
|
|
+func (item *SimpleBindItem[O]) bind(binder *Binder, method string, middlewares ...api.Handler) {
|
|
|
bindingItem := &BindItem[O]{
|
|
|
Method: method,
|
|
|
SimpleBindItem: item,
|
|
|
}
|
|
|
|
|
|
- bindingItem.bind(router, middlewares...)
|
|
|
+ bindingItem.bind(binder, middlewares...)
|
|
|
}
|
|
|
|
|
|
// BindItem 路由条目结构
|
|
|
@@ -102,7 +108,7 @@ type BindItem[O any] struct {
|
|
|
*SimpleBindItem[O]
|
|
|
}
|
|
|
|
|
|
-func (item *BindItem[O]) bind(router api.Router, middlewares ...api.Handler) {
|
|
|
+func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
|
|
|
if strutils.IsStringEmpty(item.Path) {
|
|
|
panic("需要指定路径")
|
|
|
}
|
|
|
@@ -204,7 +210,7 @@ func (item *BindItem[O]) bind(router api.Router, middlewares ...api.Handler) {
|
|
|
// 执行业务函数
|
|
|
statusCode := http.StatusOK
|
|
|
|
|
|
- i := globalInfrastructure
|
|
|
+ i := binder.i
|
|
|
if item.Infrastructure != nil {
|
|
|
i = item.Infrastructure
|
|
|
}
|
|
|
@@ -222,7 +228,7 @@ func (item *BindItem[O]) bind(router api.Router, middlewares ...api.Handler) {
|
|
|
handlers = append(handlers, middlewares...)
|
|
|
|
|
|
// 所有的函数加入到执行链中
|
|
|
- router.AddRoute(item.Method, item.Path, handlers...)
|
|
|
+ binder.router.AddRoute(item.Method, item.Path, handlers...)
|
|
|
}
|
|
|
|
|
|
// StaticBindItem 静态路由item
|
|
|
@@ -232,11 +238,11 @@ type StaticBindItem struct {
|
|
|
WithBasePath bool
|
|
|
}
|
|
|
|
|
|
-func (item *StaticBindItem) bind(router api.Router) {
|
|
|
+func (item *StaticBindItem) bind(binder *Binder) {
|
|
|
if item.WithBasePath {
|
|
|
- router.Static(strings.TrimPrefix(item.RelativePath, router.BasePath()), item.Root)
|
|
|
+ binder.router.Static(strings.TrimPrefix(item.RelativePath, binder.router.BasePath()), item.Root)
|
|
|
} else {
|
|
|
- router.Static(item.RelativePath, item.Root)
|
|
|
+ binder.router.Static(item.RelativePath, item.Root)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -246,10 +252,10 @@ type StaticFileBindItem struct {
|
|
|
WithBasePath bool
|
|
|
}
|
|
|
|
|
|
-func (item *StaticFileBindItem) bind(router api.Router) {
|
|
|
+func (item *StaticFileBindItem) bind(binder *Binder) {
|
|
|
if item.WithBasePath {
|
|
|
- router.StaticFile(strings.TrimPrefix(item.RelativePath, router.BasePath()), item.FilePath)
|
|
|
+ binder.router.StaticFile(strings.TrimPrefix(item.RelativePath, binder.router.BasePath()), item.FilePath)
|
|
|
} else {
|
|
|
- router.StaticFile(item.RelativePath, item.FilePath)
|
|
|
+ binder.router.StaticFile(item.RelativePath, item.FilePath)
|
|
|
}
|
|
|
}
|