builder.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "net/http"
  6. "time"
  7. )
  8. // Handler 网关API处理函数
  9. type Handler func(requestBuilder *RequestBuilder)
  10. // Builder 网关API构建器
  11. type Builder struct {
  12. router api.Router
  13. httpClient *http_client.Client
  14. httpTimeoutSec int64
  15. }
  16. func newBuilder(router api.Router, httpClient *http_client.Client, httpTimeoutSec int64, middlewares ...Handler) *Builder {
  17. builder := &Builder{
  18. router: router,
  19. httpClient: httpClient,
  20. httpTimeoutSec: httpTimeoutSec,
  21. }
  22. apiMiddlewares := make([]api.Handler, len(middlewares))
  23. for i, middleware := range middlewares {
  24. apiMiddlewares[i] = builder.formApiHandler(middleware)
  25. }
  26. builder.router.AddMiddlewares(apiMiddlewares...)
  27. return builder
  28. }
  29. // PostRoute 创建POST网关API
  30. // 参数:
  31. // - relativePath: 该网关API的相对URL
  32. // - handler: 该网关API处理函数
  33. // - middlewares: 该网关API的中间件
  34. // 返回值: 无
  35. func (builder *Builder) PostRoute(relativePath string, handler Handler, middlewares ...Handler) {
  36. builder.AddRoute(http.MethodPost, relativePath, handler, middlewares...)
  37. }
  38. // DeleteRoute 创建DELETE网关API
  39. // 参数:
  40. // - relativePath: 该网关API的相对URL
  41. // - handler: 该网关API处理函数
  42. // - middlewares: 该网关API的中间件
  43. // 返回值: 无
  44. func (builder *Builder) DeleteRoute(relativePath string, handler Handler, middlewares ...Handler) {
  45. builder.AddRoute(http.MethodDelete, relativePath, handler, middlewares...)
  46. }
  47. // PutRoute 创建PUT网关API
  48. // 参数:
  49. // - relativePath: 该网关API的相对URL
  50. // - handler: 该网关API处理函数
  51. // - middlewares: 该网关API的中间件
  52. // 返回值: 无
  53. func (builder *Builder) PutRoute(relativePath string, handler Handler, middlewares ...Handler) {
  54. builder.AddRoute(http.MethodPut, relativePath, handler, middlewares...)
  55. }
  56. // GetRoute 创建GET网关API
  57. // 参数:
  58. // - relativePath: 该网关API的相对URL
  59. // - handler: 该网关API处理函数
  60. // - middlewares: 该网关API的中间件
  61. // 返回值: 无
  62. func (builder *Builder) GetRoute(relativePath string, handler Handler, middlewares ...Handler) {
  63. builder.AddRoute(http.MethodGet, relativePath, handler, middlewares...)
  64. }
  65. // PostRouteWithTenantIDAndUserID 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  66. // 参数:
  67. // - relativePath: 该网关API的相对URL
  68. // - handler: 该网关API处理函数
  69. // - middlewares: 该网关API的中间件
  70. // 返回值: 无
  71. func (builder *Builder) PostRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) {
  72. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "userId", "", handler, middlewares...)
  73. }
  74. // PostRouteWithTenantIDAndCreateUserID 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  75. // 参数:
  76. // - relativePath: 该网关API的相对URL
  77. // - handler: 该网关API处理函数
  78. // - middlewares: 该网关API的中间件
  79. // 返回值: 无
  80. func (builder *Builder) PostRouteWithTenantIDAndCreateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  81. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "createUserId", "", handler, middlewares...)
  82. }
  83. // DeleteRouteWithTenantIDAndUserID 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  84. // 参数:
  85. // - relativePath: 该网关API的相对URL
  86. // - handler: 该网关API处理函数
  87. // - middlewares: 该网关API的中间件
  88. // 返回值: 无
  89. func (builder *Builder) DeleteRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) {
  90. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "userId", "", handler, middlewares...)
  91. }
  92. // DeleteRouteWithTenantIDAndDeleteUserID 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  93. // 参数:
  94. // - relativePath: 该网关API的相对URL
  95. // - handler: 该网关API处理函数
  96. // - middlewares: 该网关API的中间件
  97. // 返回值: 无
  98. func (builder *Builder) DeleteRouteWithTenantIDAndDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) {
  99. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "deleteUserId", "", handler, middlewares...)
  100. }
  101. // PutRouteWithTenantIDAndUserID 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  102. // 参数:
  103. // - relativePath: 该网关API的相对URL
  104. // - handler: 该网关API处理函数
  105. // - middlewares: 该网关API的中间件
  106. // 返回值: 无
  107. func (builder *Builder) PutRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) {
  108. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "userId", "", handler, middlewares...)
  109. }
  110. // PutRouteWithTenantIDAndUpdateUserID 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  111. // 参数:
  112. // - relativePath: 该网关API的相对URL
  113. // - handler: 该网关API处理函数
  114. // - middlewares: 该网关API的中间件
  115. // 返回值: 无
  116. func (builder *Builder) PutRouteWithTenantIDAndUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  117. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "updateUserId", "", handler, middlewares...)
  118. }
  119. // PostRouteWithTenantIDAndUserInfo 创建POST网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  120. // 参数:
  121. // - relativePath: 该网关API的相对URL
  122. // - handler: 该网关API处理函数
  123. // - middlewares: 该网关API的中间件
  124. // 返回值: 无
  125. func (builder *Builder) PostRouteWithTenantIDAndUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  126. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "userId", "operatorUserName", handler, middlewares...)
  127. }
  128. // PostRouteWithTenantIDAndCreateUserInfo 创建POST网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  129. // 参数:
  130. // - relativePath: 该网关API的相对URL
  131. // - handler: 该网关API处理函数
  132. // - middlewares: 该网关API的中间件
  133. // 返回值: 无
  134. func (builder *Builder) PostRouteWithTenantIDAndCreateUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  135. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "createUserId", "operatorUserName", handler, middlewares...)
  136. }
  137. // DeleteRouteWithTenantIDAndUserInfo 创建DELETE网关API,会在查询参数添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  138. // 参数:
  139. // - relativePath: 该网关API的相对URL
  140. // - handler: 该网关API处理函数
  141. // - middlewares: 该网关API的中间件
  142. // 返回值: 无
  143. func (builder *Builder) DeleteRouteWithTenantIDAndUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  144. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "userId", "operatorUserName", handler, middlewares...)
  145. }
  146. // DeleteRouteWithTenantIDAndDeleteUserInfo 创建DELETE网关API,会在查询参数添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  147. // 参数:
  148. // - relativePath: 该网关API的相对URL
  149. // - handler: 该网关API处理函数
  150. // - middlewares: 该网关API的中间件
  151. // 返回值: 无
  152. func (builder *Builder) DeleteRouteWithTenantIDAndDeleteUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  153. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "deleteUserId", "operatorUserName", handler, middlewares...)
  154. }
  155. // PutRouteWithTenantIDAndUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  156. // 参数:
  157. // - relativePath: 该网关API的相对URL
  158. // - handler: 该网关API处理函数
  159. // - middlewares: 该网关API的中间件
  160. // 返回值: 无
  161. func (builder *Builder) PutRouteWithTenantIDAndUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  162. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "userId", "operatorUserName", handler, middlewares...)
  163. }
  164. // PutRouteWithTenantIDAndUpdateUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  165. // 参数:
  166. // - relativePath: 该网关API的相对URL
  167. // - handler: 该网关API处理函数
  168. // - middlewares: 该网关API的中间件
  169. // 返回值: 无
  170. func (builder *Builder) PutRouteWithTenantIDAndUpdateUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  171. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "updateUserId", "operatorUserName", handler, middlewares...)
  172. }
  173. // GetRouteWithTenantIDAndUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  174. // 参数:
  175. // - relativePath: 该网关API的相对URL
  176. // - handler: 该网关API处理函数
  177. // - middlewares: 该网关API的中间件
  178. // 返回值: 无
  179. func (builder *Builder) GetRouteWithTenantIDAndUserID(relativePath string, handler Handler, middlewares ...Handler) {
  180. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "userId", "", handler, middlewares...)
  181. }
  182. // GetRouteWithTenantIDAndCreateUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  183. // 参数:
  184. // - relativePath: 该网关API的相对URL
  185. // - handler: 该网关API处理函数
  186. // - middlewares: 该网关API的中间件
  187. // 返回值: 无
  188. func (builder *Builder) GetRouteWithTenantIDAndCreateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  189. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "createUserId", "", handler, middlewares...)
  190. }
  191. // GetRouteWithTenantIDAndDeleteUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  192. // 参数:
  193. // - relativePath: 该网关API的相对URL
  194. // - handler: 该网关API处理函数
  195. // - middlewares: 该网关API的中间件
  196. // 返回值: 无
  197. func (builder *Builder) GetRouteWithTenantIDAndDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) {
  198. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "deleteUserId", "", handler, middlewares...)
  199. }
  200. // GetRouteWithTenantIDAndUpdateUserID 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  201. // 参数:
  202. // - relativePath: 该网关API的相对URL
  203. // - handler: 该网关API处理函数
  204. // - middlewares: 该网关API的中间件
  205. // 返回值: 无
  206. func (builder *Builder) GetRouteWithTenantIDAndUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  207. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "updateUserId", "", handler, middlewares...)
  208. }
  209. // PostRouteWithTenantID 创建POST网关API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  210. // 参数:
  211. // - relativePath: 该网关API的相对URL
  212. // - handler: 该网关API处理函数
  213. // - middlewares: 该网关API的中间件
  214. // 返回值: 无
  215. func (builder *Builder) PostRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) {
  216. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "tenantId", "", "", handler, middlewares...)
  217. }
  218. // DeleteRouteWithTenantID 创建DELETE网关API,会在查询参数添加租户ID字段,字段名分别为tenantId
  219. // 参数:
  220. // - relativePath: 该网关API的相对URL
  221. // - handler: 该网关API处理函数
  222. // - middlewares: 该网关API的中间件
  223. // 返回值: 无
  224. func (builder *Builder) DeleteRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) {
  225. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "tenantId", "", "", handler, middlewares...)
  226. }
  227. // PutRouteWithTenantID 创建PUT网关API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  228. // 参数:
  229. // - relativePath: 该网关API的相对URL
  230. // - handler: 该网关API处理函数
  231. // - middlewares: 该网关API的中间件
  232. // 返回值: 无
  233. func (builder *Builder) PutRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) {
  234. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "tenantId", "", "", handler, middlewares...)
  235. }
  236. // GetRouteWithTenantID 创建GET网关API,会在查询参数添加租户ID字段,字段名分别为tenantId
  237. // 参数:
  238. // - relativePath: 该网关API的相对URL
  239. // - handler: 该网关API处理函数
  240. // - middlewares: 该网关API的中间件
  241. // 返回值: 无
  242. func (builder *Builder) GetRouteWithTenantID(relativePath string, handler Handler, middlewares ...Handler) {
  243. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "tenantId", "", "", handler, middlewares...)
  244. }
  245. // PostRouteWithUserID 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId
  246. // 参数:
  247. // - relativePath: 该网关API的相对URL
  248. // - handler: 该网关API处理函数
  249. // - middlewares: 该网关API的中间件
  250. // 返回值: 无
  251. func (builder *Builder) PostRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) {
  252. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "userId", "", handler, middlewares...)
  253. }
  254. // PostRouteWithCreateUserID 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  255. // 参数:
  256. // - relativePath: 该网关API的相对URL
  257. // - handler: 该网关API处理函数
  258. // - middlewares: 该网关API的中间件
  259. // 返回值: 无
  260. func (builder *Builder) PostRouteWithCreateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  261. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "createUserId", "", handler, middlewares...)
  262. }
  263. // DeleteRouteWithUserID 创建DELETE网关API,会在查询参数添加用户ID字段,字段名为userId
  264. // 参数:
  265. // - relativePath: 该网关API的相对URL
  266. // - handler: 该网关API处理函数
  267. // - middlewares: 该网关API的中间件
  268. // 返回值: 无
  269. func (builder *Builder) DeleteRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) {
  270. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "userId", "", handler, middlewares...)
  271. }
  272. // DeleteRouteWithDeleteUserID 创建DELETE网关API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  273. // 参数:
  274. // - relativePath: 该网关API的相对URL
  275. // - handler: 该网关API处理函数
  276. // - middlewares: 该网关API的中间件
  277. // 返回值: 无
  278. func (builder *Builder) DeleteRouteWithDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) {
  279. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "deleteUserId", "", handler, middlewares...)
  280. }
  281. // PutRouteWithUserID 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId
  282. // 参数:
  283. // - relativePath: 该网关API的相对URL
  284. // - handler: 该网关API处理函数
  285. // - middlewares: 该网关API的中间件
  286. // 返回值: 无
  287. func (builder *Builder) PutRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) {
  288. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "userId", "", handler, middlewares...)
  289. }
  290. // PutRouteWithUpdateUserID 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId
  291. // 参数:
  292. // - relativePath: 该网关API的相对URL
  293. // - handler: 该网关API处理函数
  294. // - middlewares: 该网关API的中间件
  295. // 返回值: 无
  296. func (builder *Builder) PutRouteWithUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  297. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "updateUserId", "", handler, middlewares...)
  298. }
  299. // PostRouteWithUserInfo 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId,还会添加操作者用户名,字段名为operatorUserName
  300. // 参数:
  301. // - relativePath: 该网关API的相对URL
  302. // - handler: 该网关API处理函数
  303. // - middlewares: 该网关API的中间件
  304. // 返回值: 无
  305. func (builder *Builder) PostRouteWithUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  306. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "userId", "operatorUserName", handler, middlewares...)
  307. }
  308. // PostRouteWithCreateUserInfo 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
  309. // 参数:
  310. // - relativePath: 该网关API的相对URL
  311. // - handler: 该网关API处理函数
  312. // - middlewares: 该网关API的中间件
  313. // 返回值: 无
  314. func (builder *Builder) PostRouteWithCreateUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  315. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", "createUserId", "operatorUserName", handler, middlewares...)
  316. }
  317. // DeleteRouteWithUserInfo 创建DELETE网关API,会在查询参数添加用户ID字段,字段名为userId,还会添加操作者用户名,字段名为operatorUserName
  318. // 参数:
  319. // - relativePath: 该网关API的相对URL
  320. // - handler: 该网关API处理函数
  321. // - middlewares: 该网关API的中间件
  322. // 返回值: 无
  323. func (builder *Builder) DeleteRouteWithUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  324. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "userId", "operatorUserName", handler, middlewares...)
  325. }
  326. // DeleteRouteWithDeleteUserInfo 创建DELETE网关API,会在查询参数添加用户ID字段,字段名分别为deleteUserId,还会添加操作者用户名,字段名为operatorUserName
  327. // 参数:
  328. // - relativePath: 该网关API的相对URL
  329. // - handler: 该网关API处理函数
  330. // - middlewares: 该网关API的中间件
  331. // 返回值: 无
  332. func (builder *Builder) DeleteRouteWithDeleteUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  333. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", "deleteUserId", "operatorUserName", handler, middlewares...)
  334. }
  335. // PutRouteWithUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名为userId,还会添加操作者用户名,字段名为operatorUserName
  336. // 参数:
  337. // - relativePath: 该网关API的相对URL
  338. // - handler: 该网关API处理函数
  339. // - middlewares: 该网关API的中间件
  340. // 返回值: 无
  341. func (builder *Builder) PutRouteWithUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  342. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "userId", "operatorUserName", handler, middlewares...)
  343. }
  344. // PutRouteWithUpdateUserInfo 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId,还会添加操作者用户名,字段名为operatorUserName
  345. // 参数:
  346. // - relativePath: 该网关API的相对URL
  347. // - handler: 该网关API处理函数
  348. // - middlewares: 该网关API的中间件
  349. // 返回值: 无
  350. func (builder *Builder) PutRouteWithUpdateUserInfo(relativePath string, handler Handler, middlewares ...Handler) {
  351. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", "updateUserId", "operatorUserName", handler, middlewares...)
  352. }
  353. // GetRouteWithUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名为userId
  354. // 参数:
  355. // - relativePath: 该网关API的相对URL
  356. // - handler: 该网关API处理函数
  357. // - middlewares: 该网关API的中间件
  358. // 返回值: 无
  359. func (builder *Builder) GetRouteWithUserID(relativePath string, handler Handler, middlewares ...Handler) {
  360. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", "userId", "", handler, middlewares...)
  361. }
  362. // GetRouteWithCreateUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名分别为createUserId
  363. // 参数:
  364. // - relativePath: 该网关API的相对URL
  365. // - handler: 该网关API处理函数
  366. // - middlewares: 该网关API的中间件
  367. // 返回值: 无
  368. func (builder *Builder) GetRouteWithCreateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  369. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", "createUserId", "", handler, middlewares...)
  370. }
  371. // GetRouteWithDeleteUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  372. // 参数:
  373. // - relativePath: 该网关API的相对URL
  374. // - handler: 该网关API处理函数
  375. // - middlewares: 该网关API的中间件
  376. // 返回值: 无
  377. func (builder *Builder) GetRouteWithDeleteUserID(relativePath string, handler Handler, middlewares ...Handler) {
  378. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", "deleteUserId", "", handler, middlewares...)
  379. }
  380. // GetRouteWithUpdateUserID 创建GET网关API,会在查询参数添加用户ID字段,字段名分别为updateUserId
  381. // 参数:
  382. // - relativePath: 该网关API的相对URL
  383. // - handler: 该网关API处理函数
  384. // - middlewares: 该网关API的中间件
  385. // 返回值: 无
  386. func (builder *Builder) GetRouteWithUpdateUserID(relativePath string, handler Handler, middlewares ...Handler) {
  387. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", "updateUserId", "", handler, middlewares...)
  388. }
  389. // PostRouteWithTenantIDAndUserIDCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定
  390. // 参数:
  391. // - relativePath: 该网关API的相对URL
  392. // - tenantIDField: 租户ID字段名
  393. // - userIDField: 用户ID字段名
  394. // - handler: 该网关API处理函数
  395. // - middlewares: 该网关API的中间件
  396. // 返回值: 无
  397. func (builder *Builder) PostRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) {
  398. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, tenantIDField, userIDField, "", handler, middlewares...)
  399. }
  400. // DeleteRouteWithTenantIDAndUserIDCommon 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名由调用者指定
  401. // 参数:
  402. // - relativePath: 该网关API的相对URL
  403. // - tenantIDField: 租户ID字段名
  404. // - userIDField: 用户ID字段名
  405. // - handler: 该网关API处理函数
  406. // - middlewares: 该网关API的中间件
  407. // 返回值: 无
  408. func (builder *Builder) DeleteRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) {
  409. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, tenantIDField, userIDField, "", handler, middlewares...)
  410. }
  411. // PutRouteWithTenantIDAndUserIDCommon 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定
  412. // 参数:
  413. // - relativePath: 该网关API的相对URL
  414. // - tenantIDField: 租户ID字段名
  415. // - userIDField: 用户ID字段名
  416. // - handler: 该网关API处理函数
  417. // - middlewares: 该网关API的中间件
  418. // 返回值: 无
  419. func (builder *Builder) PutRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) {
  420. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, tenantIDField, userIDField, "", handler, middlewares...)
  421. }
  422. // PostRouteWithTenantIDAndUserInfoCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID,用户ID以及用户名字段,字段名由调用者指定
  423. // 参数:
  424. // - relativePath: 该网关API的相对URL
  425. // - tenantIDField: 租户ID字段名
  426. // - userIDField: 用户ID字段名
  427. // - handler: 该网关API处理函数
  428. // - middlewares: 该网关API的中间件
  429. // 返回值: 无
  430. func (builder *Builder) PostRouteWithTenantIDAndUserInfoCommon(relativePath string, tenantIDField string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) {
  431. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, tenantIDField, userIDField, userNameField, handler, middlewares...)
  432. }
  433. // DeleteRouteWithTenantIDAndUserInfoCommon 创建DELETE网关API,会在查询参数添加租户ID和用户ID字段,字段名由调用者指定
  434. // 参数:
  435. // - relativePath: 该网关API的相对URL
  436. // - tenantIDField: 租户ID字段名
  437. // - userIDField: 用户ID字段名
  438. // - handler: 该网关API处理函数
  439. // - middlewares: 该网关API的中间件
  440. // 返回值: 无
  441. func (builder *Builder) DeleteRouteWithTenantIDAndUserInfoCommon(relativePath string, tenantIDField string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) {
  442. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, tenantIDField, userIDField, userNameField, handler, middlewares...)
  443. }
  444. // PutRouteWithTenantIDAndUserInfoCommon 创建PUT网关API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名由调用者指定
  445. // 参数:
  446. // - relativePath: 该网关API的相对URL
  447. // - tenantIDField: 租户ID字段名
  448. // - userIDField: 用户ID字段名
  449. // - handler: 该网关API处理函数
  450. // - middlewares: 该网关API的中间件
  451. // 返回值: 无
  452. func (builder *Builder) PutRouteWithTenantIDAndUserInfoCommon(relativePath string, tenantIDField string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) {
  453. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, tenantIDField, userIDField, userNameField, handler, middlewares...)
  454. }
  455. // GetRouteWithTenantIDAndUserIDCommon 创建GET网关API,会在查询参数添加租户ID和用户ID字段,字段名由调用者指定
  456. // 参数:
  457. // - relativePath: 该网关API的相对URL
  458. // - tenantIDField: 租户ID字段名
  459. // - userIDField: 用户ID字段名
  460. // - handler: 该网关API处理函数
  461. // - middlewares: 该网关API的中间件
  462. // 返回值: 无
  463. func (builder *Builder) GetRouteWithTenantIDAndUserIDCommon(relativePath string, tenantIDField string, userIDField string, handler Handler, middlewares ...Handler) {
  464. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, tenantIDField, userIDField, "", handler, middlewares...)
  465. }
  466. // PostRouteWithTenantIDCommon 创建POST网关API,请求Body是JsonBody,且会添加租户ID字段,字段名由调用者指定
  467. // 参数:
  468. // - relativePath: 该网关API的相对URL
  469. // - tenantIDField: 租户ID字段名
  470. // - handler: 该网关API处理函数
  471. // - middlewares: 该网关API的中间件
  472. // 返回值: 无
  473. func (builder *Builder) PostRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) {
  474. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, tenantIDField, "", "", handler, middlewares...)
  475. }
  476. // DeleteRouteWithTenantIDCommon 创建DELETE网关API,会在查询参数添加租户ID字段,字段名由调用者指定
  477. // 参数:
  478. // - relativePath: 该网关API的相对URL
  479. // - tenantIDField: 租户ID字段名
  480. // - handler: 该网关API处理函数
  481. // - middlewares: 该网关API的中间件
  482. // 返回值: 无
  483. func (builder *Builder) DeleteRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) {
  484. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, tenantIDField, "", "", handler, middlewares...)
  485. }
  486. // PutRouteWithTenantIDCommon 创建PUT网关API,请求Body是JsonBody,且会添加租户ID字段,字段名由调用者指定
  487. // 参数:
  488. // - relativePath: 该网关API的相对URL
  489. // - tenantIDField: 租户ID字段名
  490. // - handler: 该网关API处理函数
  491. // - middlewares: 该网关API的中间件
  492. // 返回值: 无
  493. func (builder *Builder) PutRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) {
  494. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, tenantIDField, "", "", handler, middlewares...)
  495. }
  496. // GetRouteWithTenantIDCommon 创建GET网关API,会在查询参数添加租户ID字段,字段名由调用者指定
  497. // 参数:
  498. // - relativePath: 该网关API的相对URL
  499. // - tenantIDField: 租户ID字段名
  500. // - handler: 该网关API处理函数
  501. // - middlewares: 该网关API的中间件
  502. // 返回值: 无
  503. func (builder *Builder) GetRouteWithTenantIDCommon(relativePath string, tenantIDField string, handler Handler, middlewares ...Handler) {
  504. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, tenantIDField, "", "", handler, middlewares...)
  505. }
  506. // PostRouteWithUserIDCommon 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段,字段名由调用者指定
  507. // 参数:
  508. // - relativePath: 该网关API的相对URL
  509. // - userIDField: 用户ID字段名
  510. // - handler: 该网关API处理函数
  511. // - middlewares: 该网关API的中间件
  512. // 返回值: 无
  513. func (builder *Builder) PostRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) {
  514. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, "", userIDField, "", handler, middlewares...)
  515. }
  516. // DeleteRouteWithUserIDCommon 创建DELETE网关API,会在查询参数添加用户ID字段,字段名由调用者指定
  517. // 参数:
  518. // - relativePath: 该网关API的相对URL
  519. // - userIDField: 用户ID字段名
  520. // - handler: 该网关API处理函数
  521. // - middlewares: 该网关API的中间件
  522. // 返回值: 无
  523. func (builder *Builder) DeleteRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) {
  524. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", userIDField, "", handler, middlewares...)
  525. }
  526. // PutRouteWithUserIDCommon 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段,字段名由调用者指定
  527. // 参数:
  528. // - relativePath: 该网关API的相对URL
  529. // - userIDField: 用户ID字段名
  530. // - handler: 该网关API处理函数
  531. // - middlewares: 该网关API的中间件
  532. // 返回值: 无
  533. func (builder *Builder) PutRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) {
  534. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", userIDField, "", handler, middlewares...)
  535. }
  536. // PostRouteWithUserInfoCommon 创建POST网关API,请求Body是JsonBody,且会添加用户ID字段和用户名,字段名由调用者指定
  537. // 参数:
  538. // - relativePath: 该网关API的相对URL
  539. // - userIDField: 用户ID字段名
  540. // - handler: 该网关API处理函数
  541. // - middlewares: 该网关API的中间件
  542. // 返回值: 无
  543. func (builder *Builder) PostRouteWithUserInfoCommon(relativePath string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) {
  544. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPost, relativePath, userNameField, userIDField, "", handler, middlewares...)
  545. }
  546. // DeleteRouteWithUserInfoCommon 创建DELETE网关API,会在查询参数添加用户ID字段和用户名,字段名由调用者指定
  547. // 参数:
  548. // - relativePath: 该网关API的相对URL
  549. // - userIDField: 用户ID字段名
  550. // - handler: 该网关API处理函数
  551. // - middlewares: 该网关API的中间件
  552. // 返回值: 无
  553. func (builder *Builder) DeleteRouteWithUserInfoCommon(relativePath string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) {
  554. builder.AddRouteWithTenantIDAndUserInfo(http.MethodDelete, relativePath, "", userIDField, userNameField, handler, middlewares...)
  555. }
  556. // PutRouteWithUserInfoCommon 创建PUT网关API,请求Body是JsonBody,且会添加用户ID字段和用户名,字段名由调用者指定
  557. // 参数:
  558. // - relativePath: 该网关API的相对URL
  559. // - userIDField: 用户ID字段名
  560. // - handler: 该网关API处理函数
  561. // - middlewares: 该网关API的中间件
  562. // 返回值: 无
  563. func (builder *Builder) PutRouteWithUserInfoCommon(relativePath string, userIDField string, userNameField string, handler Handler, middlewares ...Handler) {
  564. builder.AddRouteWithTenantIDAndUserInfo(http.MethodPut, relativePath, "", userIDField, userNameField, handler, middlewares...)
  565. }
  566. // GetRouteWithUserIDCommon 创建GET网关API,会在查询参数添加用户ID字段,字段名由调用者指定
  567. // 参数:
  568. // - relativePath: 该网关API的相对URL
  569. // - userIDField: 用户ID字段名
  570. // - handler: 该网关API处理函数
  571. // - middlewares: 该网关API的中间件
  572. // 返回值: 无
  573. func (builder *Builder) GetRouteWithUserIDCommon(relativePath string, userIDField string, handler Handler, middlewares ...Handler) {
  574. builder.AddRouteWithTenantIDAndUserInfo(http.MethodGet, relativePath, "", userIDField, "", handler, middlewares...)
  575. }
  576. // AddRoute 通用添加API函数
  577. // 参数:
  578. // - httpMethod: HTTP方法
  579. // - relativePath: 该网关API的相对URL
  580. // - handler: 该网关API处理函数
  581. // - middlewares: 该网关API的中间件
  582. // 返回值: 无
  583. func (builder *Builder) AddRoute(httpMethod string, relativePath string, handler Handler, middlewares ...Handler) {
  584. apiHandlers := make([]api.Handler, len(middlewares)+1)
  585. for i, middleware := range middlewares {
  586. apiHandlers[i] = builder.formApiHandler(middleware)
  587. }
  588. apiHandlers[len(middlewares)] = builder.formApiHandler(handler)
  589. builder.router.AddRoute(httpMethod, relativePath, apiHandlers...)
  590. }
  591. // AddRouteWithTenantIDAndUserInfo 通用添加API函数,会在请求参数(POST和PUT是JsonBody,GET和DELETE是查询参数)添加租户ID,用户ID以及操作者用户名字段,字段名由调用者指定
  592. // 参数:
  593. // - httpMethod: HTTP方法
  594. // - relativePath: 该网关API的相对URL
  595. // - tenantIDField: 租户ID字段名
  596. // - userIDField: 用户ID字段名
  597. // - operatorUserName: 操作者用户名字段名
  598. // - handler: 该网关API处理函数
  599. // - middlewares: 该网关API的中间件
  600. // 返回值: 无
  601. func (builder *Builder) AddRouteWithTenantIDAndUserInfo(httpMethod string, relativePath string, tenantIDField string, userIDField string, operatorUserNameField string, handler Handler, middlewares ...Handler) {
  602. apiHandlers := make([]api.Handler, len(middlewares)+1)
  603. for i, middleware := range middlewares {
  604. apiHandlers[i] = builder.formApiHandlerWithTenantIDAndUserInfo(httpMethod, tenantIDField, userIDField, operatorUserNameField, middleware)
  605. }
  606. apiHandlers[len(middlewares)] = builder.formApiHandlerWithTenantIDAndUserInfo(httpMethod, tenantIDField, userIDField, operatorUserNameField, handler)
  607. builder.router.AddRoute(httpMethod, relativePath, apiHandlers...)
  608. }
  609. func (builder *Builder) formApiHandler(handler Handler) api.Handler {
  610. return func(c *api.Context) {
  611. httpRequest := builder.httpClient.NewRequest(http_client.WithNewRequestTimeout(time.Duration(builder.httpTimeoutSec) * time.Second))
  612. requestBuilder := newRequestBuilder(c, httpRequest)
  613. handler(requestBuilder)
  614. }
  615. }
  616. func (builder *Builder) formApiHandlerWithTenantIDAndUserInfo(httpMethod string, tenantIDParamsName string, userIDParamsName string, operatorUserNamePramsName string, handler Handler) api.Handler {
  617. return func(c *api.Context) {
  618. httpRequest := builder.httpClient.NewRequest(http_client.WithNewRequestTimeout(time.Duration(builder.httpTimeoutSec) * time.Second))
  619. requestBuilder := newRequestBuilder(c, httpRequest)
  620. switch httpMethod {
  621. case http.MethodPost, http.MethodPut:
  622. err := AddJsonBodyTenantIDAndUserInfo(requestBuilder, tenantIDParamsName, userIDParamsName, operatorUserNamePramsName)
  623. if err != nil {
  624. panic(err)
  625. }
  626. case http.MethodDelete, http.MethodGet:
  627. err := AddQueryParamsTenantIDAndUserInfo(requestBuilder, tenantIDParamsName, userIDParamsName, operatorUserNamePramsName)
  628. if err != nil {
  629. panic(err)
  630. }
  631. default:
  632. panic("不支持的http方法s")
  633. }
  634. handler(requestBuilder)
  635. }
  636. }