فهرست منبع

feat(transport): 添加 Method 字段支持自定义 HTTP 方法在 transport/middleware/option.go 中新增 Method 字段,用于定义 HTTP 请求方法。
在 route.go 中使用 net/http 包中的常量来设置默认的 HTTP 方法,并根据 annotations
规则匹配对应的请求方法。同时更新 unaryHandler逻辑以支持通过 reply 函数自定义
响应结构,提升接口灵活性和可扩展性。

dcsunny 1 ماه پیش
والد
کامیت
90df1bb418
4فایلهای تغییر یافته به همراه45 افزوده شده و 10 حذف شده
  1. 16 2
      transport/http/handler/route.go
  2. 5 8
      transport/http/handler/unary_handler.go
  3. 23 0
      transport/http/reply/reply.go
  4. 1 0
      transport/middleware/option.go

+ 16 - 2
transport/http/handler/route.go

@@ -3,6 +3,8 @@ package handler
 import (
 	"fmt"
 
+	http2 "net/http"
+
 	"git.ikuban.com/server/kratos-utils/v2/transport/middleware"
 	ku_annotations "git.ikuban.com/server/kubanapis/kuban/api/annotations"
 	"github.com/go-kratos/kratos/v2/transport/http"
@@ -31,9 +33,8 @@ func RegisterRoute(s *http.Server, srv any, svcDesc grpc.ServiceDesc) {
 		if !option.GenHttp {
 			continue
 		}
-
 		// 注册http接口
-		r.POST(option.Path, unaryHandler(srv, md, option))
+		r.Handle(option.Method, option.Path, unaryHandler(srv, md, option))
 	}
 
 	// 注册流式方法handler
@@ -97,11 +98,24 @@ func GetOptionByServiceDescriptor(sd *desc.ServiceDescriptor, serviceName, metho
 		switch httpRule := rule.GetPattern().(type) {
 		case *annotations.HttpRule_Post:
 			option.Path = httpRule.Post
+			option.Method = http2.MethodPost
+		case *annotations.HttpRule_Get:
+			option.Path = httpRule.Get
+			option.Method = http2.MethodGet
+		case *annotations.HttpRule_Delete:
+			option.Path = httpRule.Delete
+			option.Method = http2.MethodDelete
+		case *annotations.HttpRule_Put:
+			option.Path = httpRule.Put
+			option.Method = http2.MethodPut
 		}
 	}
 	if option.Path == "" {
 		option.Path = fmt.Sprintf("/%s/%s", serviceName, methodName)
 	}
+	if option.Method == "" {
+		option.Method = http2.MethodPost
+	}
 
 	return option
 }

+ 5 - 8
transport/http/handler/unary_handler.go

@@ -36,14 +36,11 @@ func unaryHandler(srv any, method grpc.MethodDesc, option *middleware.Option) ht
 		if err != nil {
 			return err
 		}
-
-		success := &reply.SuccessReply{
-			Code: 0,
-		}
-		if out != nil {
-			success.Data = out
+		f := reply.DefaultFunc(out)
+		if f != nil {
+			success := reply.GetReplyFunc()(out)
+			return ctx.Result(200, success)
 		}
-
-		return ctx.Result(200, success)
+		return ctx.Result(200, out)
 	}
 }

+ 23 - 0
transport/http/reply/reply.go

@@ -5,3 +5,26 @@ type SuccessReply struct {
 	Message string      `json:"message"`
 	Data    interface{} `json:"data"`
 }
+
+type ReplyFunc func(req any) any
+
+var reply = DefaultFunc
+
+func DefaultFunc(out any) any {
+	success := &SuccessReply{
+		Code: 0,
+	}
+
+	if out != nil {
+		success.Data = out
+	}
+	return success
+}
+
+func SetReplyFunc(f ReplyFunc) {
+	reply = f
+}
+
+func GetReplyFunc() ReplyFunc {
+	return reply
+}

+ 1 - 0
transport/middleware/option.go

@@ -7,6 +7,7 @@ type Option struct {
 	NotAuth         bool
 	OperationRecord bool
 	Path            string
+	Method          string
 }
 
 type httpOption struct{}