Explorar o código

http_binding添加限制

yjp hai 5 meses
pai
achega
0d08f9f26a
Modificáronse 3 ficheiros con 14 adicións e 0 borrados
  1. 9 0
      http_binding/http_binding.go
  2. 1 0
      http_binding_test.go
  3. 4 0
      mqtt_binding_test.go

+ 9 - 0
http_binding/http_binding.go

@@ -8,6 +8,7 @@ import (
 	"git.sxidc.com/go-tools/api_binding/http_binding/response"
 	"git.sxidc.com/go-tools/api_binding/utils"
 	"git.sxidc.com/service-supports/fserr"
+	"git.sxidc.com/service-supports/fslog"
 	"github.com/gin-gonic/gin"
 	"net/http"
 	"reflect"
@@ -56,6 +57,14 @@ func (binding *Binding) AddHandler(method string, relativePath string, handleFun
 		return errors.New("没有传递处理函数")
 	}
 
+	routeInfos := routerInstance.Routes()
+	for _, routeInfo := range routeInfos {
+		if routeInfo.Method == method && strings.TrimPrefix(routeInfo.Path, binding.routerGroup.BasePath()+"/") == relativePath {
+			fslog.Error("添加的路径已存在: " + routeInfo.Path)
+			return nil
+		}
+	}
+
 	// 给单个路由增加中间件
 	ginHandleFunctions := make([]gin.HandlerFunc, 0)
 	for _, handleFunction := range handleFunctions {

+ 1 - 0
http_binding_test.go

@@ -15,6 +15,7 @@ func TestHttpBinding(t *testing.T) {
 	defer http_binding.Destroy()
 
 	testBinding := http_binding.NewBinding("test")
+
 	http_binding.GetBind(testBinding, &http_binding.SimpleBindItem[any, map[string]interface{}]{
 		Path:         "/ping",
 		ResponseFunc: response.SendMapResponse,

+ 4 - 0
mqtt_binding_test.go

@@ -21,6 +21,10 @@ func (responseIdentifier *ResponseIdentifier) Identifier() string {
 	return responseIdentifier.ResponseIdentifier
 }
 
+func (responseIdentifier *ResponseIdentifier) Copy() response.Identifier {
+	return &ResponseIdentifier{}
+}
+
 var bindingResponseIdentifier = &request.CallerIdentifierRequest{CallerIdentifier: "binding"}
 
 var itemResponseIdentifier = &ResponseIdentifier{ResponseIdentifier: "item"}