浏览代码

代码注释

duyong-ubuntu 1 年之前
父节点
当前提交
dcfc0211c3

+ 2 - 0
http_binding/binding_context/binding_context.go

@@ -13,6 +13,7 @@ type Context struct {
 	*gin.Context
 }
 
+// GetPathParams 根据给定的路径参数名字获取参数值
 func (c *Context) GetPathParams(pathParamNames []string) ([]string, error) {
 	pathParamValues := make([]string, 0)
 	for _, paramName := range pathParamNames {
@@ -27,6 +28,7 @@ func (c *Context) GetPathParams(pathParamNames []string) ([]string, error) {
 	return pathParamValues, nil
 }
 
+// GetFileHeaderBytes 获取传递的文件名和文件内容
 func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
 	file, err := fileHeader.Open()
 	if err != nil {

+ 15 - 4
http_binding/http_binding.go

@@ -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

+ 3 - 3
http_binding/http_init.go

@@ -23,9 +23,9 @@ func Init(apiUrlPrefix string, port string) {
 
 	routerInstance = gin.New()
 
-	routerInstance.Use(fslog.GinLogger([]string{"/" + urlPrefix + "/api/version"}), fslog.GinRecovery(),
-		func(c *gin.Context) { middleware.Cors()(&binding_context.Context{Context: c}) },
-		func(c *gin.Context) { middleware.ReadRequestBodyData()(&binding_context.Context{Context: c}) })
+	routerInstance.Use(fslog.GinLogger([]string{"/" + urlPrefix + "/api/version"}),
+		fslog.GinRecovery(),
+		func(c *gin.Context) { middleware.Cors()(&binding_context.Context{Context: c}) })
 
 	serverInstance = &http.Server{
 		Addr:    ":" + port,

+ 0 - 32
http_binding/middleware/body.go

@@ -1,32 +0,0 @@
-package middleware
-
-import (
-	"bytes"
-	"git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
-	"git.sxidc.com/go-tools/api_binding/http_binding/response"
-	"io"
-	"net/http"
-)
-
-func ReadRequestBodyData() Func {
-	return func(c *binding_context.Context) {
-		respFunc := response.SendMsgResponse
-
-		contentType := c.Request.Header.Get("Content-Type")
-		if c.Request.Body != nil && contentType == "application/json" &&
-			(c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut) {
-			bodyData, err := io.ReadAll(c.Request.Body)
-			if err != nil {
-				respFunc(c, http.StatusBadRequest, nil, err)
-				c.Abort()
-				return
-			}
-
-			_ = c.Request.Body.Close()
-			c.Request.Body = nil
-			c.Request.Body = io.NopCloser(bytes.NewReader(bodyData))
-		}
-
-		c.Next()
-	}
-}

+ 1 - 0
http_binding/middleware/middleware.go

@@ -2,4 +2,5 @@ package middleware
 
 import "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
 
+// Func 中间件函数类型
 type Func func(c *binding_context.Context)

+ 0 - 58
http_binding_test.go

@@ -1,7 +1,6 @@
 package api_binding
 
 import (
-	"fmt"
 	"git.sxidc.com/go-tools/api_binding/http_binding"
 	"git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
 	"git.sxidc.com/go-tools/api_binding/http_binding/response"
@@ -62,60 +61,3 @@ func TestHttpBinding(t *testing.T) {
 		t.Fatal("响应错误")
 	}
 }
-
-func Auth(binding *binding_context.Context) {
-	fmt.Println("11111")
-}
-
-func TestHttpBinding2(t *testing.T) {
-	http_binding.Init("test", "10000")
-	defer http_binding.Destroy()
-
-	testBinding := http_binding.NewBinding("test", Auth)
-
-	http_binding.GetBind(testBinding, &http_binding.SimpleBindItem[any, map[string]interface{}]{
-		Path:         "/ping",
-		ResponseFunc: response.SendMapResponse,
-		BusinessFunc: func(c *binding_context.Context, inputModel any) (map[string]interface{}, error) {
-			return map[string]interface{}{
-				"result": "pong",
-			}, nil
-		},
-	})
-
-	resp, err := http.Get("http://localhost:10000/test/api/test/ping")
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	if resp.StatusCode != http.StatusOK {
-		t.Fatal("状态码错误")
-	}
-
-	body, err := io.ReadAll(resp.Body)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	defer func(Body io.ReadCloser) {
-		err := Body.Close()
-		if err != nil {
-			t.Fatal(err)
-		}
-	}(resp.Body)
-
-	respMap := make(map[string]interface{})
-	err = json.Unmarshal(body, &respMap)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	pong, ok := respMap["result"]
-	if !ok {
-		t.Fatal("响应错误")
-	}
-
-	if pong != "pong" {
-		t.Fatal("响应错误")
-	}
-}