package gateway import ( "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-tools/utils/http_client" "net/http" "time" ) // Handler 网关API处理函数 type Handler func(requestBuilder *RequestBuilder) // Builder 网关API构建器 type Builder struct { router api.Router httpClient *http_client.Client httpTimeoutSec int64 } func newBuilder(router api.Router, httpClient *http_client.Client, httpTimeoutSec int64, middlewares ...Handler) *Builder { builder := &Builder{ router: router, httpClient: httpClient, httpTimeoutSec: httpTimeoutSec, } apiMiddlewares := make([]api.Handler, len(middlewares)) for i, middleware := range middlewares { apiMiddlewares[i] = builder.formApiHandler(middleware) } builder.router.AddMiddlewares(apiMiddlewares...) return builder } // PostRoute 创建POST网关API // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRoute(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRoute(http.MethodPost, relativePath, handler, middlewares...) } // DeleteRoute 创建DELETE网关API // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRoute(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRoute(http.MethodDelete, relativePath, handler, middlewares...) } // PutRoute 创建PUT网关API // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRoute(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRoute(http.MethodPut, relativePath, handler, middlewares...) } // GetRoute 创建GET网关API // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRoute(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRoute(http.MethodGet, relativePath, handler, middlewares...) } // PostRouteWithTenantIDAndUserID 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "userId", "", handler, middlewares...) } // PostRouteWithTenantIDAndCreateUserID 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDAndCreateUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "createUserId", "", handler, middlewares...) } // DeleteRouteWithTenantIDAndUserID 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "userId", "", handler, middlewares...) } // DeleteRouteWithTenantIDAndDeleteUserID 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDAndDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "deleteUserId", "", handler, middlewares...) } // PutRouteWithTenantIDAndUserID 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "userId", "", handler, middlewares...) } // PutRouteWithTenantIDAndUpdateUserID 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDAndUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "updateUserId", "", handler, middlewares...) } // PostRouteWithTenantIDAndUserInfo 创建POST网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDAndUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "userId", "operatorUserName", handler, middlewares...) } // PostRouteWithTenantIDAndCreateUserInfo 创建POST网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDAndCreateUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "createUserId", "operatorUserName", handler, middlewares...) } // DeleteRouteWithTenantIDAndUserInfo 创建DELETE网关API,会在查询参数添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDAndUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "userId", "operatorUserName", handler, middlewares...) } // DeleteRouteWithTenantIDAndDeleteUserInfo 创建DELETE网关API,会在查询参数添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDAndDeleteUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "deleteUserId", "operatorUserName", handler, middlewares...) } // PutRouteWithTenantIDAndUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDAndUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "userId", "operatorUserName", handler, middlewares...) } // PutRouteWithTenantIDAndUpdateUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDAndUpdateUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "updateUserId", "operatorUserName", handler, middlewares...) } // GetRouteWithTenantIDAndUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(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.AddRouteWithTenantIDAndUserInfo(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.AddRouteWithTenantIDAndUserInfo(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.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "updateUserId", "", handler, middlewares...) } // PostRouteWithTenantID 创建POST网关API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "", "", handler, middlewares...) } // DeleteRouteWithTenantID 创建DELETE网关API,会在查询参数添加租户ID字段,字段名分别为tenantId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "", "", handler, middlewares...) } // PutRouteWithTenantID 创建PUT网关API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "", "", handler, middlewares...) } // GetRouteWithTenantID 创建GET网关API,会在查询参数添加租户ID字段,字段名分别为tenantId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "", "", handler, middlewares...) } // PostRouteWithUserID 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "userId", "", handler, middlewares...) } // PostRouteWithCreateUserID 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithCreateUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "createUserId", "", handler, middlewares...) } // DeleteRouteWithUserID 创建DELETE网关API,会在查询参数添加用户ID字段,字段名为userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "userId", "", handler, middlewares...) } // DeleteRouteWithDeleteUserID 创建DELETE网关API,会在查询参数添加用户ID字段,字段名分别为deleteUserId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "deleteUserId", "", handler, middlewares...) } // PutRouteWithUserID 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "userId", "", handler, middlewares...) } // PutRouteWithUpdateUserID 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "updateUserId", "", handler, middlewares...) } // PostRouteWithUserInfo 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId,还会添加操作者用户名,字段名为operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "userId", "operatorUserName", handler, middlewares...) } // PostRouteWithCreateUserInfo 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithCreateUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "createUserId", "operatorUserName", handler, middlewares...) } // DeleteRouteWithUserInfo 创建DELETE网关API,会在查询参数添加用户ID字段,字段名为userId,还会添加操作者用户名,字段名为operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "userId", "operatorUserName", handler, middlewares...) } // DeleteRouteWithDeleteUserInfo 创建DELETE网关API,会在查询参数添加用户ID字段,字段名分别为deleteUserId,还会添加操作者用户名,字段名为operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithDeleteUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "deleteUserId", "operatorUserName", handler, middlewares...) } // PutRouteWithUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId,还会添加操作者用户名,字段名为operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "userId", "operatorUserName", handler, middlewares...) } // PutRouteWithUpdateUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId,还会添加操作者用户名,字段名为operatorUserName // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithUpdateUserInfo(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "updateUserId", "operatorUserName", handler, middlewares...) } // GetRouteWithUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名为userId // 参数: // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(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.AddRouteWithTenantIDAndUserInfo(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.AddRouteWithTenantIDAndUserInfo(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.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", "updateUserId", "", handler, middlewares...) } // PostRouteWithTenantIDAndUserIDCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, tenantIDField, userIDField, "", handler, middlewares...) } // DeleteRouteWithTenantIDAndUserIDCommon 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, tenantIDField, userIDField, "", handler, middlewares...) } // PutRouteWithTenantIDAndUserIDCommon 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, tenantIDField, userIDField, "", handler, middlewares...) } // PostRouteWithTenantIDAndUserInfoCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及用户名字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDAndUserInfoCommon(relativePath string, tenantIDField string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, tenantIDField, userIDField, userNameField, handler, middlewares...) } // DeleteRouteWithTenantIDAndUserInfoCommon 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDAndUserInfoCommon(relativePath string, tenantIDField string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, tenantIDField, userIDField, userNameField, handler, middlewares...) } // PutRouteWithTenantIDAndUserInfoCommon 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDAndUserInfoCommon(relativePath string, tenantIDField string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, tenantIDField, userIDField, userNameField, handler, middlewares...) } // GetRouteWithTenantIDAndUserIDCommon 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, tenantIDField, userIDField, "", handler, middlewares...) } // PostRouteWithTenantIDCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, tenantIDField, "", "", handler, middlewares...) } // DeleteRouteWithTenantIDCommon 创建DELETE网关API,会在查询参数添加租户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, tenantIDField, "", "", handler, middlewares...) } // PutRouteWithTenantIDCommon 创建PUT网关API,请求Body是JsonBody,且会添加租户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, tenantIDField, "", "", handler, middlewares...) } // GetRouteWithTenantIDCommon 创建GET网关API,会在查询参数添加租户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, tenantIDField, "", "", handler, middlewares...) } // PostRouteWithUserIDCommon 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", userIDField, "", handler, middlewares...) } // DeleteRouteWithUserIDCommon 创建DELETE网关API,会在查询参数添加用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", userIDField, "", handler, middlewares...) } // PutRouteWithUserIDCommon 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", userIDField, "", handler, middlewares...) } // PostRouteWithUserInfoCommon 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段和用户名,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PostRouteWithUserInfoCommon(relativePath string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, userNameField, userIDField, "", handler, middlewares...) } // DeleteRouteWithUserInfoCommon 创建DELETE网关API,会在查询参数添加用户ID字段和用户名,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) DeleteRouteWithUserInfoCommon(relativePath string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", userIDField, userNameField, handler, middlewares...) } // PutRouteWithUserInfoCommon 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段和用户名,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) PutRouteWithUserInfoCommon(relativePath string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", userIDField, userNameField, handler, middlewares...) } // GetRouteWithUserIDCommon 创建GET网关API,会在查询参数添加用户ID字段,字段名由调用者指定 // 参数: // - relativePath: 该网关API的相对URL // - userIDField: 用户ID字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) GetRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) { builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", userIDField, "", handler, middlewares...) } // AddRoute 通用添加API函数 // 参数: // - httpMethod: HTTP方法 // - relativePath: 该网关API的相对URL // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) AddRoute(httpMethod string, relativePath string, handler Handler, middlewares ...Handler) { apiHandlers := make([]api.Handler, len(middlewares)+1) for i, middleware := range middlewares { apiHandlers[i] = builder.formApiHandler(middleware) } apiHandlers[len(middlewares)] = builder.formApiHandler(handler) builder.router.AddRoute(httpMethod, relativePath, apiHandlers...) } // AddRouteWithTenantIDAndUserInfo 通用添加API函数,会在请求参数(POST和PUT是JsonBody,GET和DELETE是查询参数)添加租户ID,用户ID以及操作者用户名字段,字段名由调用者指定 // 参数: // - httpMethod: HTTP方法 // - relativePath: 该网关API的相对URL // - tenantIDField: 租户ID字段名 // - userIDField: 用户ID字段名 // - operatorUserName: 操作者用户名字段名 // - handler: 该网关API处理函数 // - middlewares: 该网关API的中间件 // 返回值: 无 func (builder *Builder) AddRouteWithTenantIDAndUserInfo(httpMethod string, relativePath string, tenantIDField string, userIDField string, operatorUserNameField string, handler Handler, middlewares ...Handler) { apiHandlers := make([]api.Handler, len(middlewares)+1) for i, middleware := range middlewares { apiHandlers[i] = builder.formApiHandlerWithTenantIDAndUserInfo(httpMethod, tenantIDField, userIDField, operatorUserNameField, middleware) } apiHandlers[len(middlewares)] = builder.formApiHandlerWithTenantIDAndUserInfo(httpMethod, tenantIDField, userIDField, operatorUserNameField, handler) builder.router.AddRoute(httpMethod, relativePath, apiHandlers...) } func (builder *Builder) formApiHandler(handler Handler) api.Handler { return func(c *api.Context) { httpRequest := builder.httpClient.NewRequest(http_client.WithNewRequestTimeout(time.Duration(builder.httpTimeoutSec) * time.Second)) requestBuilder := newRequestBuilder(c, httpRequest) handler(requestBuilder) } } func (builder *Builder) formApiHandlerWithTenantIDAndUserInfo(httpMethod string, tenantIDParamsName string, userIDParamsName string, operatorUserNamePramsName string, handler Handler) api.Handler { return func(c *api.Context) { httpRequest := builder.httpClient.NewRequest(http_client.WithNewRequestTimeout(time.Duration(builder.httpTimeoutSec) * time.Second)) requestBuilder := newRequestBuilder(c, httpRequest) switch httpMethod { case http.MethodPost, http.MethodPut: err := AddJsonBodyTenantIDAndUserInfo(requestBuilder, tenantIDParamsName, userIDParamsName, operatorUserNamePramsName) if err != nil { panic(err) } case http.MethodDelete, http.MethodGet: err := AddQueryParamsTenantIDAndUserInfo(requestBuilder, tenantIDParamsName, userIDParamsName, operatorUserNamePramsName) if err != nil { panic(err) } default: panic("不支持的http方法s") } handler(requestBuilder) } }