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 PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, opts...) } func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, opts...) } func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, opts...) } func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, opts...) } func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("createUserId"))...) } func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("deleteUserId"))...) } func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("updateUserId"))...) } 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"))...) } func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithUserIDParamsName("createUserId"))...) } func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithUserIDParamsName("userId"))...) } 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) { 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() }, options.middlewares...) } 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 WithTenantIDParamsName(tenantIDParamsName string) Option { return func(options *Options) { options.tenantIDParamsName = tenantIDParamsName } } func WithUserIDParamsName(userIDParamsName string) Option { return func(options *Options) { options.userIDParamsName = userIDParamsName } } func WithBeforeRequestCallback(callback RequestBuilderCallback) Option { return func(options *Options) { options.beforeRequestCallback = callback } } func WithAfterRequestCallback(callback RequestBuilderCallback) Option { return func(options *Options) { options.afterRequestCallback = callback } } 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 } }