123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- 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)
- }
- }
|