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