yjp 1 жил өмнө
parent
commit
6f363d5af5

+ 259 - 71
convenient/gwtools/pass_through/pass_through.go

@@ -2,120 +2,308 @@ package pass_through
 
 import (
 	"git.sxidc.com/go-framework/baize/framework/gateway"
+	"git.sxidc.com/go-tools/utils/http_client"
+	"git.sxidc.com/go-tools/utils/strutils"
 	"net/http"
 )
 
-func Post(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodPost, middlewares...)
+func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPost, opts...)
 }
 
-func Delete(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodDelete, middlewares...)
+func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodDelete, opts...)
 }
 
-func Put(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodPut, middlewares...)
+func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPut, opts...)
 }
 
-func Get(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodGet, middlewares...)
+func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, opts...)
 }
 
-func PostPassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodPost, middlewares...)
+func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPost, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("userId"))...)
 }
 
-func DeletePassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodDelete, middlewares...)
+func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPost, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("createUserId"))...)
 }
 
-func PutPassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodPut, middlewares...)
+func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodDelete, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("userId"))...)
 }
 
-func GetPassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...gateway.Handler) {
-	params.passThrough(builder, http.MethodGet, middlewares...)
+func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodDelete, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("deleteUserId"))...)
 }
 
-type PassThroughRequestItem struct {
-	Request                 gateway.Request
-	RequestResponseCallback gateway.RequestResponseCallback
+func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPut, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("userId"))...)
 }
 
-type SimplePassThroughParams struct {
-	RelativePath string
-	Request      gateway.Request
+func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPut, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("updateUserId"))...)
 }
 
-func (params *SimplePassThroughParams) passThrough(builder *gateway.Builder, httpMethod string, middlewares ...gateway.Handler) {
-	passThroughParams := &PassThroughParams{
-		HttpMethod:              httpMethod,
-		SimplePassThroughParams: params,
-	}
+func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("userId"))...)
+}
+
+func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("createUserId"))...)
+}
+
+func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("deleteUserId"))...)
+}
+
+func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithTenantIDParamsName("tenantId"),
+		WithUserIDParamsName("updateUserId"))...)
+}
+
+func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPost, append(opts,
+		WithTenantIDParamsName("tenantId"))...)
+}
+
+func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodDelete, append(opts,
+		WithTenantIDParamsName("tenantId"))...)
+}
+
+func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPut, append(opts,
+		WithTenantIDParamsName("tenantId"))...)
+}
+
+func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithTenantIDParamsName("tenantId"))...)
+}
+
+func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPost, append(opts,
+		WithUserIDParamsName("userId"))...)
+}
 
-	passThroughParams.passThrough(builder, middlewares...)
+func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPost, append(opts,
+		WithUserIDParamsName("createUserId"))...)
 }
 
-type PassThroughParams struct {
-	HttpMethod string
-	*SimplePassThroughParams
+func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodDelete, append(opts,
+		WithUserIDParamsName("userId"))...)
 }
 
-func (params *PassThroughParams) passThrough(builder *gateway.Builder, middlewares ...gateway.Handler) {
-	builder.AddRoute(params.HttpMethod, params.RelativePath,
+func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodDelete, append(opts,
+		WithUserIDParamsName("deleteUserId"))...)
+}
+
+func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPut, append(opts,
+		WithUserIDParamsName("userId"))...)
+}
+
+func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodPut, append(opts,
+		WithUserIDParamsName("updateUserId"))...)
+}
+
+func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithUserIDParamsName("userId"))...)
+}
+
+func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithUserIDParamsName("createUserId"))...)
+}
+
+func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithUserIDParamsName("deleteUserId"))...)
+}
+
+func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
+	params.passThrough(builder, http.MethodGet, append(opts,
+		WithUserIDParamsName("updateUserId"))...)
+}
+
+type Simple struct {
+	RelativePath string
+	ServiceUrl   string
+}
+
+func (params *Simple) passThrough(builder *gateway.Builder, httpMethod string, opts ...Option) {
+	options := new(Options)
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	builder.AddRoute(httpMethod, params.RelativePath,
 		func(requestBuilder *gateway.RequestBuilder) {
-			switch req := params.Request.(type) {
-			case *gateway.PostRequest:
-				requestBuilder.Post(req)
-			case *gateway.DeleteRequest:
-				requestBuilder.Delete(req)
-			case *gateway.PutRequest:
-				requestBuilder.Put(req)
-			case *gateway.GetRequest:
-				requestBuilder.Get(req)
+			if options.responseSuccessCallback != nil {
+				requestBuilder.ResponseSuccessCallback(options.responseSuccessCallback)
+			}
+
+			if options.responseErrorCallback != nil {
+				requestBuilder.ResponseErrorCallback(options.responseErrorCallback)
+			}
+
+			if strutils.IsStringNotEmpty(options.tenantIDParamsName) ||
+				strutils.IsStringNotEmpty(options.userIDParamsName) {
+				if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
+					err := gateway.AddJsonBodyTenantIDAndUserID(requestBuilder, options.tenantIDParamsName, options.userIDParamsName)
+					if err != nil {
+						requestBuilder.ResponseError(err)
+						return
+					}
+				}
+
+				if httpMethod == http.MethodDelete || httpMethod == http.MethodGet {
+					err := gateway.AddQueryParamsTenantIDAndUserID(requestBuilder, options.tenantIDParamsName, options.userIDParamsName)
+					if err != nil {
+						requestBuilder.ResponseError(err)
+						return
+					}
+				}
+			}
+
+			if options.beforeRequestCallback != nil {
+				err := options.beforeRequestCallback(requestBuilder)
+				if err != nil {
+					requestBuilder.ResponseError(err)
+					return
+				}
+			}
+
+			requestOptions := make([]gateway.RequestOption, 0)
+			if options.afterRequestCallback != nil {
+				requestOptions = append(requestOptions, gateway.WithRequestResponseCallback(
+					func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
+						err := options.afterRequestCallback(requestBuilder)
+						if err != nil {
+							return err
+						}
+
+						return nil
+					}))
+			}
+
+			switch httpMethod {
+			case http.MethodPost:
+				requestBuilder.Post(&gateway.PostRequest{
+					Url: params.ServiceUrl,
+				}, requestOptions...)
+			case http.MethodDelete:
+				requestBuilder.Delete(&gateway.DeleteRequest{
+					Url: params.ServiceUrl,
+				}, requestOptions...)
+			case http.MethodPut:
+				requestBuilder.Put(&gateway.PutRequest{
+					Url: params.ServiceUrl,
+				}, requestOptions...)
+			case http.MethodGet:
+				requestBuilder.Get(&gateway.GetRequest{
+					Url: params.ServiceUrl,
+				}, requestOptions...)
 			default:
 				panic("不支持的请求类型")
 			}
 
 			requestBuilder.Request()
-		}, middlewares...)
+		}, options.middlewares...)
 }
 
-type SimpleOnePassThroughParams struct {
-	RelativePath string
-	RequestItem  *PassThroughRequestItem
+type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
+
+type Option func(options *Options)
+
+type Options struct {
+	// tenantIDParamsName 租户ID请求参数名称
+	tenantIDParamsName string
+
+	// userIDParamsName 用户ID请求参数名称
+	userIDParamsName string
+
+	// beforeRequestCallback 请求前回调
+	beforeRequestCallback RequestBuilderCallback
+
+	// afterRequestCallback 请求后回调
+	afterRequestCallback RequestBuilderCallback
+
+	// responseSuccessCallback 成功响应回调
+	responseSuccessCallback gateway.ResponseSuccessCallback
+
+	// responseErrorCallback 失败响应回调
+	responseErrorCallback gateway.ResponseErrorCallback
+
+	// 中间件
+	middlewares []gateway.Handler
 }
 
-func (params *SimpleOnePassThroughParams) passThrough(builder *gateway.Builder, httpMethod string, middlewares ...gateway.Handler) {
-	onePassThroughParams := &OnePassThroughParams{
-		HttpMethod:                 httpMethod,
-		SimpleOnePassThroughParams: params,
+func WithTenantIDParamsName(tenantIDParamsName string) Option {
+	return func(options *Options) {
+		options.tenantIDParamsName = tenantIDParamsName
 	}
+}
 
-	onePassThroughParams.passThrough(builder, middlewares...)
+func WithUserIDParamsName(userIDParamsName string) Option {
+	return func(options *Options) {
+		options.userIDParamsName = userIDParamsName
+	}
 }
 
-type OnePassThroughParams struct {
-	HttpMethod string
-	*SimpleOnePassThroughParams
+func WithBeforeRequestCallback(callback RequestBuilderCallback) Option {
+	return func(options *Options) {
+		options.beforeRequestCallback = callback
+	}
 }
 
-func (params *OnePassThroughParams) passThrough(builder *gateway.Builder, middlewares ...gateway.Handler) {
-	builder.AddRoute(params.HttpMethod, params.RelativePath,
-		func(requestBuilder *gateway.RequestBuilder) {
-			switch req := params.RequestItem.Request.(type) {
-			case *gateway.PostRequest:
-				requestBuilder.Post(req, gateway.WithRequestResponseCallback(params.RequestItem.RequestResponseCallback))
-			case *gateway.DeleteRequest:
-				requestBuilder.Delete(req, gateway.WithRequestResponseCallback(params.RequestItem.RequestResponseCallback))
-			case *gateway.PutRequest:
-				requestBuilder.Put(req, gateway.WithRequestResponseCallback(params.RequestItem.RequestResponseCallback))
-			case *gateway.GetRequest:
-				requestBuilder.Get(req, gateway.WithRequestResponseCallback(params.RequestItem.RequestResponseCallback))
-			default:
-				panic("不支持的请求类型")
-			}
+func WithAfterRequestCallback(callback RequestBuilderCallback) Option {
+	return func(options *Options) {
+		options.afterRequestCallback = callback
+	}
+}
 
-			requestBuilder.Request()
-		}, middlewares...)
+func WithResponseSuccessCallback(callback gateway.ResponseSuccessCallback) Option {
+	return func(options *Options) {
+		options.responseSuccessCallback = callback
+	}
+}
+
+func WithResponseErrorCallback(callback gateway.ResponseErrorCallback) Option {
+	return func(options *Options) {
+		options.responseErrorCallback = callback
+	}
+}
+
+func WithMiddlewares(middlewares ...gateway.Handler) Option {
+	return func(options *Options) {
+		options.middlewares = middlewares
+	}
 }

+ 60 - 0
framework/gateway/builder.go

@@ -144,6 +144,36 @@ func (builder *Builder) GetRouteWithTenantIDAndUserID(relativePath string, handl
 	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "tenantId", "userId", handler, middlewares...)
 }
 
+// GetRouteWithTenantIDAndCreateUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
+// 参数:
+// - relativePath: 该网关API的相对URL
+// - handler: 该网关API处理函数
+// - middlewares: 该网关API的中间件
+// 返回值: 无
+func (builder *Builder) GetRouteWithTenantIDAndCreateUserID(relativePath string, handler Handler, middlewares ...Handler) {
+	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "tenantId", "createUserId", handler, middlewares...)
+}
+
+// GetRouteWithTenantIDAndDeleteUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
+// 参数:
+// - relativePath: 该网关API的相对URL
+// - handler: 该网关API处理函数
+// - middlewares: 该网关API的中间件
+// 返回值: 无
+func (builder *Builder) GetRouteWithTenantIDAndDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) {
+	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "tenantId", "deleteUserId", handler, middlewares...)
+}
+
+// GetRouteWithTenantIDAndUpdateUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
+// 参数:
+// - relativePath: 该网关API的相对URL
+// - handler: 该网关API处理函数
+// - middlewares: 该网关API的中间件
+// 返回值: 无
+func (builder *Builder) GetRouteWithTenantIDAndUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) {
+	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "tenantId", "updateUserId", handler, middlewares...)
+}
+
 // PostRouteWithTenantID 创建POST网关API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
 // 参数:
 // - relativePath: 该网关API的相对URL
@@ -254,6 +284,36 @@ func (builder *Builder) GetRouteWithUserID(relativePath string, handler Handler,
 	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "", "userId", handler, middlewares...)
 }
 
+// GetRouteWithCreateUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名分别为createUserId
+// 参数:
+// - relativePath: 该网关API的相对URL
+// - handler: 该网关API处理函数
+// - middlewares: 该网关API的中间件
+// 返回值: 无
+func (builder *Builder) GetRouteWithCreateUserID(relativePath string, handler Handler, middlewares ...Handler) {
+	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "", "createUserId", handler, middlewares...)
+}
+
+// GetRouteWithDeleteUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
+// 参数:
+// - relativePath: 该网关API的相对URL
+// - handler: 该网关API处理函数
+// - middlewares: 该网关API的中间件
+// 返回值: 无
+func (builder *Builder) GetRouteWithDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) {
+	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "", "deleteUserId", handler, middlewares...)
+}
+
+// GetRouteWithUpdateUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名分别为updateUserId
+// 参数:
+// - relativePath: 该网关API的相对URL
+// - handler: 该网关API处理函数
+// - middlewares: 该网关API的中间件
+// 返回值: 无
+func (builder *Builder) GetRouteWithUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) {
+	builder.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "", "updateUserId", handler, middlewares...)
+}
+
 // PostRouteWithTenantIDAndUserIDCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定
 // 参数:
 // - relativePath: 该网关API的相对URL

+ 4 - 4
framework/gateway/request_builder.go

@@ -93,7 +93,7 @@ func (builder *RequestBuilder) SetResultMapValue(key string, value any) {
 	builder.resultMap[key] = value
 }
 
-// Post 发送POST请求
+// Post 定义POST请求
 // 参数:
 // - request: PostRequest
 // - opts: 请求选项
@@ -103,7 +103,7 @@ func (builder *RequestBuilder) Post(request *PostRequest, opts ...RequestOption)
 	return builder.request(request, opts...)
 }
 
-// Delete 发送Delete请求
+// Delete 定义Delete请求
 // 参数:
 // - request: DeleteRequest
 // - opts: 请求选项
@@ -113,7 +113,7 @@ func (builder *RequestBuilder) Delete(request *DeleteRequest, opts ...RequestOpt
 	return builder.request(request, opts...)
 }
 
-// Put 发送Put请求
+// Put 定义Put请求
 // 参数:
 // - request: PutRequest
 // - opts: 请求选项
@@ -123,7 +123,7 @@ func (builder *RequestBuilder) Put(request *PutRequest, opts ...RequestOption) *
 	return builder.request(request, opts...)
 }
 
-// Get 发送Get请求
+// Get 定义Get请求
 // 参数:
 // - request: GetRequest
 // - opts: 请求选项