pass_through.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. package pass_through
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/gateway"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "net/http"
  7. )
  8. // PostRoute POST直传API
  9. // 参数:
  10. // - builder: 该网关API构建器
  11. // - params: 网关直通参数
  12. // - opts: 网关直通选项
  13. // 返回值: 无
  14. func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  15. params.passThrough(builder, http.MethodPost, opts...)
  16. }
  17. // DeleteRoute DELETE直传API
  18. // 参数:
  19. // - builder: 该网关API构建器
  20. // - params: 网关直通参数
  21. // - opts: 网关直通选项
  22. // 返回值: 无
  23. func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  24. params.passThrough(builder, http.MethodDelete, opts...)
  25. }
  26. // PutRoute PUT直传API
  27. // 参数:
  28. // - builder: 该网关API构建器
  29. // - params: 网关直通参数
  30. // - opts: 网关直通选项
  31. // 返回值: 无
  32. func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  33. params.passThrough(builder, http.MethodPut, opts...)
  34. }
  35. // GetRoute GET直传API
  36. // 参数:
  37. // - builder: 该网关API构建器
  38. // - params: 网关直通参数
  39. // - opts: 网关直通选项
  40. // 返回值: 无
  41. func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  42. params.passThrough(builder, http.MethodGet, opts...)
  43. }
  44. // PostRouteWithTenantIDAndUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  45. // 参数:
  46. // - builder: 该网关API构建器
  47. // - params: 网关直通参数
  48. // - opts: 网关直通选项
  49. // 返回值: 无
  50. func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  51. params.passThrough(builder, http.MethodPost, append(opts,
  52. WithTenantIDParamsName("tenantId"),
  53. WithUserIDParamsName("userId"))...)
  54. }
  55. // PostRouteWithTenantIDAndCreateUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  56. // 参数:
  57. // - builder: 该网关API构建器
  58. // - params: 网关直通参数
  59. // - opts: 网关直通选项
  60. // 返回值: 无
  61. func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  62. params.passThrough(builder, http.MethodPost, append(opts,
  63. WithTenantIDParamsName("tenantId"),
  64. WithUserIDParamsName("createUserId"))...)
  65. }
  66. // DeleteRouteWithTenantIDAndUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  67. // 参数:
  68. // - builder: 该网关API构建器
  69. // - params: 网关直通参数
  70. // - opts: 网关直通选项
  71. // 返回值: 无
  72. func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  73. params.passThrough(builder, http.MethodDelete, append(opts,
  74. WithTenantIDParamsName("tenantId"),
  75. WithUserIDParamsName("userId"))...)
  76. }
  77. // DeleteRouteWithTenantIDAndDeleteUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  78. // 参数:
  79. // - builder: 该网关API构建器
  80. // - params: 网关直通参数
  81. // - opts: 网关直通选项
  82. // 返回值: 无
  83. func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  84. params.passThrough(builder, http.MethodDelete, append(opts,
  85. WithTenantIDParamsName("tenantId"),
  86. WithUserIDParamsName("deleteUserId"))...)
  87. }
  88. // PutRouteWithTenantIDAndUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  89. // 参数:
  90. // - builder: 该网关API构建器
  91. // - params: 网关直通参数
  92. // - opts: 网关直通选项
  93. // 返回值: 无
  94. func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  95. params.passThrough(builder, http.MethodPut, append(opts,
  96. WithTenantIDParamsName("tenantId"),
  97. WithUserIDParamsName("userId"))...)
  98. }
  99. // PutRouteWithTenantIDAndUpdateUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  100. // 参数:
  101. // - builder: 该网关API构建器
  102. // - params: 网关直通参数
  103. // - opts: 网关直通选项
  104. // 返回值: 无
  105. func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  106. params.passThrough(builder, http.MethodPut, append(opts,
  107. WithTenantIDParamsName("tenantId"),
  108. WithUserIDParamsName("updateUserId"))...)
  109. }
  110. // GetRouteWithTenantIDAndUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  111. // 参数:
  112. // - builder: 该网关API构建器
  113. // - params: 网关直通参数
  114. // - opts: 网关直通选项
  115. // 返回值: 无
  116. func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  117. params.passThrough(builder, http.MethodGet, append(opts,
  118. WithTenantIDParamsName("tenantId"),
  119. WithUserIDParamsName("userId"))...)
  120. }
  121. // GetRouteWithTenantIDAndCreateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  122. // 参数:
  123. // - builder: 该网关API构建器
  124. // - params: 网关直通参数
  125. // - opts: 网关直通选项
  126. // 返回值: 无
  127. func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  128. params.passThrough(builder, http.MethodGet, append(opts,
  129. WithTenantIDParamsName("tenantId"),
  130. WithUserIDParamsName("createUserId"))...)
  131. }
  132. // GetRouteWithTenantIDAndDeleteUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  133. // 参数:
  134. // - builder: 该网关API构建器
  135. // - params: 网关直通参数
  136. // - opts: 网关直通选项
  137. // 返回值: 无
  138. func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  139. params.passThrough(builder, http.MethodGet, append(opts,
  140. WithTenantIDParamsName("tenantId"),
  141. WithUserIDParamsName("deleteUserId"))...)
  142. }
  143. // GetRouteWithTenantIDAndUpdateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  144. // 参数:
  145. // - builder: 该网关API构建器
  146. // - params: 网关直通参数
  147. // - opts: 网关直通选项
  148. // 返回值: 无
  149. func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  150. params.passThrough(builder, http.MethodGet, append(opts,
  151. WithTenantIDParamsName("tenantId"),
  152. WithUserIDParamsName("updateUserId"))...)
  153. }
  154. // PostRouteWithTenantID POST直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  155. // 参数:
  156. // - builder: 该网关API构建器
  157. // - params: 网关直通参数
  158. // - opts: 网关直通选项
  159. // 返回值: 无
  160. func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  161. params.passThrough(builder, http.MethodPost, append(opts,
  162. WithTenantIDParamsName("tenantId"))...)
  163. }
  164. // DeleteRouteWithTenantID DELETE直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
  165. // 参数:
  166. // - builder: 该网关API构建器
  167. // - params: 网关直通参数
  168. // - opts: 网关直通选项
  169. // 返回值: 无
  170. func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  171. params.passThrough(builder, http.MethodDelete, append(opts,
  172. WithTenantIDParamsName("tenantId"))...)
  173. }
  174. // PutRouteWithTenantID PUT直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  175. // 参数:
  176. // - builder: 该网关API构建器
  177. // - params: 网关直通参数
  178. // - opts: 网关直通选项
  179. // 返回值: 无
  180. func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  181. params.passThrough(builder, http.MethodPut, append(opts,
  182. WithTenantIDParamsName("tenantId"))...)
  183. }
  184. // GetRouteWithTenantID GET直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
  185. // 参数:
  186. // - builder: 该网关API构建器
  187. // - params: 网关直通参数
  188. // - opts: 网关直通选项
  189. // 返回值: 无
  190. func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  191. params.passThrough(builder, http.MethodGet, append(opts,
  192. WithTenantIDParamsName("tenantId"))...)
  193. }
  194. // PostRouteWithUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId
  195. // 参数:
  196. // - builder: 该网关API构建器
  197. // - params: 网关直通参数
  198. // - opts: 网关直通选项
  199. // 返回值: 无
  200. func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  201. params.passThrough(builder, http.MethodPost, append(opts,
  202. WithUserIDParamsName("userId"))...)
  203. }
  204. // PostRouteWithCreateUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  205. // 参数:
  206. // - builder: 该网关API构建器
  207. // - params: 网关直通参数
  208. // - opts: 网关直通选项
  209. // 返回值: 无
  210. func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  211. params.passThrough(builder, http.MethodPost, append(opts,
  212. WithUserIDParamsName("createUserId"))...)
  213. }
  214. // DeleteRouteWithUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId
  215. // 参数:
  216. // - builder: 该网关API构建器
  217. // - params: 网关直通参数
  218. // - opts: 网关直通选项
  219. // 返回值: 无
  220. func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  221. params.passThrough(builder, http.MethodDelete, append(opts,
  222. WithUserIDParamsName("userId"))...)
  223. }
  224. // DeleteRouteWithDeleteUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  225. // 参数:
  226. // - builder: 该网关API构建器
  227. // - params: 网关直通参数
  228. // - opts: 网关直通选项
  229. // 返回值: 无
  230. func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  231. params.passThrough(builder, http.MethodDelete, append(opts,
  232. WithUserIDParamsName("deleteUserId"))...)
  233. }
  234. // PutRouteWithUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  235. // 参数:
  236. // - builder: 该网关API构建器
  237. // - params: 网关直通参数
  238. // - opts: 网关直通选项
  239. // 返回值: 无
  240. func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  241. params.passThrough(builder, http.MethodPut, append(opts,
  242. WithUserIDParamsName("userId"))...)
  243. }
  244. // PutRouteWithUpdateUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId
  245. // 参数:
  246. // - builder: 该网关API构建器
  247. // - params: 网关直通参数
  248. // - opts: 网关直通选项
  249. // 返回值: 无
  250. func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  251. params.passThrough(builder, http.MethodPut, append(opts,
  252. WithUserIDParamsName("updateUserId"))...)
  253. }
  254. // GetRouteWithUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为userId
  255. // 参数:
  256. // - builder: 该网关API构建器
  257. // - params: 网关直通参数
  258. // - opts: 网关直通选项
  259. // 返回值: 无
  260. func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  261. params.passThrough(builder, http.MethodGet, append(opts,
  262. WithUserIDParamsName("userId"))...)
  263. }
  264. // GetRouteWithCreateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为createUserId
  265. // 参数:
  266. // - builder: 该网关API构建器
  267. // - params: 网关直通参数
  268. // - opts: 网关直通选项
  269. // 返回值: 无
  270. func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  271. params.passThrough(builder, http.MethodGet, append(opts,
  272. WithUserIDParamsName("createUserId"))...)
  273. }
  274. // GetRouteWithDeleteUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  275. // 参数:
  276. // - builder: 该网关API构建器
  277. // - params: 网关直通参数
  278. // - opts: 网关直通选项
  279. // 返回值: 无
  280. func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  281. params.passThrough(builder, http.MethodGet, append(opts,
  282. WithUserIDParamsName("deleteUserId"))...)
  283. }
  284. // GetRouteWithUpdateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为updateUserId
  285. // 参数:
  286. // - builder: 该网关API构建器
  287. // - params: 网关直通参数
  288. // - opts: 网关直通选项
  289. // 返回值: 无
  290. func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  291. params.passThrough(builder, http.MethodGet, append(opts,
  292. WithUserIDParamsName("updateUserId"))...)
  293. }
  294. // Simple 参数
  295. type Simple struct {
  296. // RelativePath 网关开放API的RelativePath
  297. RelativePath string
  298. // 服务的URL
  299. ServiceUrl string
  300. }
  301. func (params *Simple) passThrough(builder *gateway.Builder, httpMethod string, opts ...Option) {
  302. options := new(Options)
  303. for _, opt := range opts {
  304. opt(options)
  305. }
  306. builder.AddRoute(httpMethod, params.RelativePath,
  307. func(requestBuilder *gateway.RequestBuilder) {
  308. if options.responseSuccessCallback != nil {
  309. requestBuilder.ResponseSuccessCallback(options.responseSuccessCallback)
  310. }
  311. if options.responseErrorCallback != nil {
  312. requestBuilder.ResponseErrorCallback(options.responseErrorCallback)
  313. }
  314. if strutils.IsStringNotEmpty(options.tenantIDParamsName) ||
  315. strutils.IsStringNotEmpty(options.userIDParamsName) {
  316. if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
  317. err := gateway.AddJsonBodyTenantIDAndUserID(requestBuilder, options.tenantIDParamsName, options.userIDParamsName)
  318. if err != nil {
  319. requestBuilder.ResponseError(err)
  320. return
  321. }
  322. }
  323. if httpMethod == http.MethodDelete || httpMethod == http.MethodGet {
  324. err := gateway.AddQueryParamsTenantIDAndUserID(requestBuilder, options.tenantIDParamsName, options.userIDParamsName)
  325. if err != nil {
  326. requestBuilder.ResponseError(err)
  327. return
  328. }
  329. }
  330. }
  331. if options.beforeRequestCallback != nil {
  332. err := options.beforeRequestCallback(requestBuilder)
  333. if err != nil {
  334. requestBuilder.ResponseError(err)
  335. return
  336. }
  337. }
  338. requestOptions := make([]gateway.RequestOption, 0)
  339. if options.afterRequestCallback != nil {
  340. requestOptions = append(requestOptions, gateway.WithRequestResponseCallback(
  341. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  342. err := options.afterRequestCallback(requestBuilder)
  343. if err != nil {
  344. return err
  345. }
  346. return nil
  347. }))
  348. }
  349. switch httpMethod {
  350. case http.MethodPost:
  351. requestBuilder.Post(&gateway.PostRequest{
  352. Url: params.ServiceUrl,
  353. }, requestOptions...)
  354. case http.MethodDelete:
  355. requestBuilder.Delete(&gateway.DeleteRequest{
  356. Url: params.ServiceUrl,
  357. }, requestOptions...)
  358. case http.MethodPut:
  359. requestBuilder.Put(&gateway.PutRequest{
  360. Url: params.ServiceUrl,
  361. }, requestOptions...)
  362. case http.MethodGet:
  363. requestBuilder.Get(&gateway.GetRequest{
  364. Url: params.ServiceUrl,
  365. }, requestOptions...)
  366. default:
  367. panic("不支持的请求类型")
  368. }
  369. requestBuilder.Request()
  370. }, options.middlewares...)
  371. }
  372. type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
  373. type Option func(options *Options)
  374. type Options struct {
  375. // tenantIDParamsName 租户ID请求参数名称
  376. tenantIDParamsName string
  377. // userIDParamsName 用户ID请求参数名称
  378. userIDParamsName string
  379. // beforeRequestCallback 请求前回调
  380. beforeRequestCallback RequestBuilderCallback
  381. // afterRequestCallback 请求后回调
  382. afterRequestCallback RequestBuilderCallback
  383. // responseSuccessCallback 成功响应回调
  384. responseSuccessCallback gateway.ResponseSuccessCallback
  385. // responseErrorCallback 失败响应回调
  386. responseErrorCallback gateway.ResponseErrorCallback
  387. // 中间件
  388. middlewares []gateway.Handler
  389. }
  390. // WithTenantIDParamsName 设置请求参数中的租户ID参数的名称
  391. func WithTenantIDParamsName(tenantIDParamsName string) Option {
  392. return func(options *Options) {
  393. options.tenantIDParamsName = tenantIDParamsName
  394. }
  395. }
  396. // WithUserIDParamsName 设置请求参数中的用户ID参数的名称
  397. func WithUserIDParamsName(userIDParamsName string) Option {
  398. return func(options *Options) {
  399. options.userIDParamsName = userIDParamsName
  400. }
  401. }
  402. // WithBeforeRequestCallback 设置请求前回调
  403. func WithBeforeRequestCallback(callback RequestBuilderCallback) Option {
  404. return func(options *Options) {
  405. options.beforeRequestCallback = callback
  406. }
  407. }
  408. // WithAfterRequestCallback 设置请求后回调
  409. func WithAfterRequestCallback(callback RequestBuilderCallback) Option {
  410. return func(options *Options) {
  411. options.afterRequestCallback = callback
  412. }
  413. }
  414. // WithResponseSuccessCallback 设置成功响应回调,默认回调会将服务响应作为网关API的响应返回
  415. func WithResponseSuccessCallback(callback gateway.ResponseSuccessCallback) Option {
  416. return func(options *Options) {
  417. options.responseSuccessCallback = callback
  418. }
  419. }
  420. // WithResponseErrorCallback 设置失败响应回调,默认回调会按照是否存在错误返回MsgResponse
  421. func WithResponseErrorCallback(callback gateway.ResponseErrorCallback) Option {
  422. return func(options *Options) {
  423. options.responseErrorCallback = callback
  424. }
  425. }
  426. // WithMiddlewares 设置中间件
  427. func WithMiddlewares(middlewares ...gateway.Handler) Option {
  428. return func(options *Options) {
  429. options.middlewares = middlewares
  430. }
  431. }