Bläddra i källkod

修改binding

yjp 1 år sedan
förälder
incheckning
cfd6a25c75
4 ändrade filer med 59 tillägg och 38 borttagningar
  1. 3 3
      api/router.go
  2. 13 0
      application/application.go
  3. 34 28
      binding/binding.go
  4. 9 7
      examples/binding/main.go

+ 3 - 3
api/router.go

@@ -10,7 +10,7 @@ type Handler func(c *Context)
 type Router interface {
 type Router interface {
 	AddMiddlewares(middlewares ...Handler) Router
 	AddMiddlewares(middlewares ...Handler) Router
 	RegisterVersionedRouter(version string, middlewares ...Handler) Router
 	RegisterVersionedRouter(version string, middlewares ...Handler) Router
-	GetVersionedRouter(version string) Router
+	VersionedRouter(version string) Router
 	AddRoute(method string, relativePath string, middlewares ...Handler) Router
 	AddRoute(method string, relativePath string, middlewares ...Handler) Router
 	Static(relativePath string, root string)
 	Static(relativePath string, root string)
 	StaticFile(relativePath string, filepath string)
 	StaticFile(relativePath string, filepath string)
@@ -53,7 +53,7 @@ func (r *RootRouter) BasePath() string {
 	return r.engine.BasePath()
 	return r.engine.BasePath()
 }
 }
 
 
-func (r *RootRouter) GetVersionedRouter(version string) Router {
+func (r *RootRouter) VersionedRouter(version string) Router {
 	return r.versioned[version]
 	return r.versioned[version]
 }
 }
 
 
@@ -94,7 +94,7 @@ func (r *PrefixRouter) RegisterVersionedRouter(version string, middlewares ...Ha
 	return r.versioned[version]
 	return r.versioned[version]
 }
 }
 
 
-func (r *PrefixRouter) GetVersionedRouter(version string) Router {
+func (r *PrefixRouter) VersionedRouter(version string) Router {
 	return r.versioned[version]
 	return r.versioned[version]
 }
 }
 
 

+ 13 - 0
application/application.go

@@ -2,6 +2,7 @@ package application
 
 
 import (
 import (
 	"git.sxidc.com/go-framework/baize/api"
 	"git.sxidc.com/go-framework/baize/api"
+	"git.sxidc.com/go-framework/baize/binding"
 	"git.sxidc.com/go-framework/baize/infrastructure"
 	"git.sxidc.com/go-framework/baize/infrastructure"
 	"git.sxidc.com/go-framework/baize/infrastructure/logger"
 	"git.sxidc.com/go-framework/baize/infrastructure/logger"
 )
 )
@@ -59,3 +60,15 @@ func (app *App) Infrastructure() *infrastructure.Infrastructure {
 func (app *App) Logger() *logger.Logger {
 func (app *App) Logger() *logger.Logger {
 	return app.loggerInstance
 	return app.loggerInstance
 }
 }
+
+// Binder 创建Binder
+func (app *App) Binder(routerVersion string) *binding.Binder {
+	var router api.Router
+	if routerVersion != "" {
+		router = app.apiInstance.RootRouter()
+	} else {
+		router = app.apiInstance.PrefixRouter().VersionedRouter(routerVersion)
+	}
+
+	return binding.NewBinder(router, app.infrastructureInstance)
+}

+ 34 - 28
binding/binding.go

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

+ 9 - 7
examples/binding/main.go

@@ -133,12 +133,14 @@ func main() {
 		panic(err)
 		panic(err)
 	}
 	}
 
 
-	v1Router := app.Api().PrefixRouter().RegisterVersionedRouter("v1")
+	app.Api().
+		PrefixRouter().
+		RegisterVersionedRouter("v1")
 
 
-	binding.SetGlobalInfrastructure(app.Infrastructure())
+	binder := app.Binder("v1")
 
 
 	// 创建班级
 	// 创建班级
-	binding.PostBind(v1Router, &binding.SimpleBindItem[string]{
+	binding.PostBind(binder, &binding.SimpleBindItem[string]{
 		Path:         "/class/create",
 		Path:         "/class/create",
 		ResponseFunc: binding.SendIDResponse[string],
 		ResponseFunc: binding.SendIDResponse[string],
 		DTO:          &CreateClassJsonBody{},
 		DTO:          &CreateClassJsonBody{},
@@ -157,7 +159,7 @@ func main() {
 	})
 	})
 
 
 	// 删除班级
 	// 删除班级
-	binding.DeleteBind(v1Router, &binding.SimpleBindItem[any]{
+	binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
 		Path:         "/class/:id/delete",
 		Path:         "/class/:id/delete",
 		ResponseFunc: binding.SendMsgResponse,
 		ResponseFunc: binding.SendMsgResponse,
 		DTO:          &DeleteClassPathParams{},
 		DTO:          &DeleteClassPathParams{},
@@ -175,7 +177,7 @@ func main() {
 	})
 	})
 
 
 	// 修改班级
 	// 修改班级
-	binding.PutBind(v1Router, &binding.SimpleBindItem[any]{
+	binding.PutBind(binder, &binding.SimpleBindItem[any]{
 		Path:         "/class/update",
 		Path:         "/class/update",
 		ResponseFunc: binding.SendMsgResponse,
 		ResponseFunc: binding.SendMsgResponse,
 		DTO:          &UpdateClassJsonBody{},
 		DTO:          &UpdateClassJsonBody{},
@@ -215,7 +217,7 @@ func main() {
 	})
 	})
 
 
 	// 查询班级
 	// 查询班级
-	binding.GetBind(v1Router, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
+	binding.GetBind(binder, &binding.SimpleBindItem[binding.InfosData[ClassInfo]]{
 		Path:         "/class/query",
 		Path:         "/class/query",
 		ResponseFunc: binding.SendInfosResponse[ClassInfo],
 		ResponseFunc: binding.SendInfosResponse[ClassInfo],
 		DTO:          &QueryClassesQueryParams{},
 		DTO:          &QueryClassesQueryParams{},
@@ -260,7 +262,7 @@ func main() {
 	})
 	})
 
 
 	// 通过ID获取班级
 	// 通过ID获取班级
-	binding.GetBind(v1Router, &binding.SimpleBindItem[*ClassInfo]{
+	binding.GetBind(binder, &binding.SimpleBindItem[*ClassInfo]{
 		Path:         "/class/get",
 		Path:         "/class/get",
 		ResponseFunc: binding.SendInfoResponse[*ClassInfo],
 		ResponseFunc: binding.SendInfoResponse[*ClassInfo],
 		DTO:          &GetClassQueryParams{},
 		DTO:          &GetClassQueryParams{},