|
|
@@ -7,8 +7,10 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+// Handler 网关API处理函数
|
|
|
type Handler func(requestBuilder *RequestBuilder)
|
|
|
|
|
|
+// Builder 网关API构建器
|
|
|
type Builder struct {
|
|
|
router api.Router
|
|
|
httpClient *http_client.Client
|
|
|
@@ -32,118 +34,309 @@ func newBuilder(router api.Router, httpClient *http_client.Client, httpTimeoutSe
|
|
|
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.AddRouteWithTenantIDAndUserID(http.MethodPost, relativePath, "tenantId", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodDelete, relativePath, "tenantId", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodPut, relativePath, "tenantId", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "tenantId", "userId", 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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(http.MethodPost, relativePath, "", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodDelete, relativePath, "", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodPut, relativePath, "", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodGet, relativePath, "", "userId", 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.AddRouteWithTenantIDAndUserID(http.MethodPost, relativePath, tenantIDField, "userId", handler, middlewares...)
|
|
|
+ builder.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(http.MethodPut, relativePath, tenantIDField, userIDField, 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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(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.AddRouteWithTenantIDAndUserID(http.MethodPut, relativePath, "", userIDField, 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.AddRouteWithTenantIDAndUserID(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)
|
|
|
|
|
|
@@ -155,6 +348,15 @@ func (builder *Builder) AddRoute(httpMethod string, relativePath string, handler
|
|
|
builder.router.AddRoute(httpMethod, relativePath, apiHandlers...)
|
|
|
}
|
|
|
|
|
|
+// AddRouteWithTenantIDAndUserID 通用添加API函数,会在请求参数(POST和PUT是JsonBody,GET和DELETE是查询参数)添加租户ID和用户ID字段,字段名由调用者指定
|
|
|
+// 参数:
|
|
|
+// - httpMethod: HTTP方法
|
|
|
+// - relativePath: 该网关API的相对URL
|
|
|
+// - tenantIDField: 租户ID字段名
|
|
|
+// - userIDField: 用户ID字段名
|
|
|
+// - handler: 该网关API处理函数
|
|
|
+// - middlewares: 该网关API的中间件
|
|
|
+// 返回值: 无
|
|
|
func (builder *Builder) AddRouteWithTenantIDAndUserID(httpMethod string, relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) {
|
|
|
apiHandlers := make([]api.Handler, len(middlewares)+1)
|
|
|
|