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" ) // PostRoute POST直传API // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, opts...) } // DeleteRoute DELETE直传API // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, opts...) } // PutRoute PUT直传API // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, opts...) } // GetRoute GET直传API // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, opts...) } // PostRouteWithTenantIDAndUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } // PostRouteWithTenantIDAndCreateUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("createUserId"))...) } // DeleteRouteWithTenantIDAndUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } // DeleteRouteWithTenantIDAndDeleteUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("deleteUserId"))...) } // PutRouteWithTenantIDAndUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } // PutRouteWithTenantIDAndUpdateUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("updateUserId"))...) } // GetRouteWithTenantIDAndUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("userId"))...) } // GetRouteWithTenantIDAndCreateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("createUserId"))...) } // GetRouteWithTenantIDAndDeleteUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("deleteUserId"))...) } // GetRouteWithTenantIDAndUpdateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithTenantIDParamsName("tenantId"), WithUserIDParamsName("updateUserId"))...) } // PostRouteWithTenantID POST直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithTenantIDParamsName("tenantId"))...) } // DeleteRouteWithTenantID DELETE直传API,会在查询参数添加租户ID字段,字段名分别为tenantId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithTenantIDParamsName("tenantId"))...) } // PutRouteWithTenantID PUT直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithTenantIDParamsName("tenantId"))...) } // GetRouteWithTenantID GET直传API,会在查询参数添加租户ID字段,字段名分别为tenantId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithTenantIDParamsName("tenantId"))...) } // PostRouteWithUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithUserIDParamsName("userId"))...) } // PostRouteWithCreateUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPost, append(opts, WithUserIDParamsName("createUserId"))...) } // DeleteRouteWithUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithUserIDParamsName("userId"))...) } // DeleteRouteWithDeleteUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodDelete, append(opts, WithUserIDParamsName("deleteUserId"))...) } // PutRouteWithUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithUserIDParamsName("userId"))...) } // PutRouteWithUpdateUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodPut, append(opts, WithUserIDParamsName("updateUserId"))...) } // GetRouteWithUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为userId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithUserIDParamsName("userId"))...) } // GetRouteWithCreateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为createUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithUserIDParamsName("createUserId"))...) } // GetRouteWithDeleteUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithUserIDParamsName("deleteUserId"))...) } // GetRouteWithUpdateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为updateUserId // 参数: // - builder: 该网关API构建器 // - params: 网关直通参数 // - opts: 网关直通选项 // 返回值: 无 func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) { params.passThrough(builder, http.MethodGet, append(opts, WithUserIDParamsName("updateUserId"))...) } // Simple 参数 type Simple struct { // RelativePath 网关开放API的RelativePath RelativePath string // 服务的URL 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 } // WithTenantIDParamsName 设置请求参数中的租户ID参数的名称 func WithTenantIDParamsName(tenantIDParamsName string) Option { return func(options *Options) { options.tenantIDParamsName = tenantIDParamsName } } // WithUserIDParamsName 设置请求参数中的用户ID参数的名称 func WithUserIDParamsName(userIDParamsName string) Option { return func(options *Options) { options.userIDParamsName = userIDParamsName } } // WithBeforeRequestCallback 设置请求前回调 func WithBeforeRequestCallback(callback RequestBuilderCallback) Option { return func(options *Options) { options.beforeRequestCallback = callback } } // WithAfterRequestCallback 设置请求后回调 func WithAfterRequestCallback(callback RequestBuilderCallback) Option { return func(options *Options) { options.afterRequestCallback = callback } } // WithResponseSuccessCallback 设置成功响应回调,默认回调会将服务响应作为网关API的响应返回 func WithResponseSuccessCallback(callback gateway.ResponseSuccessCallback) Option { return func(options *Options) { options.responseSuccessCallback = callback } } // WithResponseErrorCallback 设置失败响应回调,默认回调会按照是否存在错误返回MsgResponse func WithResponseErrorCallback(callback gateway.ResponseErrorCallback) Option { return func(options *Options) { options.responseErrorCallback = callback } } // WithMiddlewares 设置中间件 func WithMiddlewares(middlewares ...gateway.Handler) Option { return func(options *Options) { options.middlewares = middlewares } }