pass_through.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  9. params.passThrough(builder, http.MethodPost, opts...)
  10. }
  11. func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  12. params.passThrough(builder, http.MethodDelete, opts...)
  13. }
  14. func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  15. params.passThrough(builder, http.MethodPut, opts...)
  16. }
  17. func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  18. params.passThrough(builder, http.MethodGet, opts...)
  19. }
  20. func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  21. params.passThrough(builder, http.MethodPost, append(opts,
  22. WithTenantIDParamsName("tenantId"),
  23. WithUserIDParamsName("userId"))...)
  24. }
  25. func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  26. params.passThrough(builder, http.MethodPost, append(opts,
  27. WithTenantIDParamsName("tenantId"),
  28. WithUserIDParamsName("createUserId"))...)
  29. }
  30. func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  31. params.passThrough(builder, http.MethodDelete, append(opts,
  32. WithTenantIDParamsName("tenantId"),
  33. WithUserIDParamsName("userId"))...)
  34. }
  35. func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  36. params.passThrough(builder, http.MethodDelete, append(opts,
  37. WithTenantIDParamsName("tenantId"),
  38. WithUserIDParamsName("deleteUserId"))...)
  39. }
  40. func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  41. params.passThrough(builder, http.MethodPut, append(opts,
  42. WithTenantIDParamsName("tenantId"),
  43. WithUserIDParamsName("userId"))...)
  44. }
  45. func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  46. params.passThrough(builder, http.MethodPut, append(opts,
  47. WithTenantIDParamsName("tenantId"),
  48. WithUserIDParamsName("updateUserId"))...)
  49. }
  50. func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  51. params.passThrough(builder, http.MethodGet, append(opts,
  52. WithTenantIDParamsName("tenantId"),
  53. WithUserIDParamsName("userId"))...)
  54. }
  55. func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  56. params.passThrough(builder, http.MethodGet, append(opts,
  57. WithTenantIDParamsName("tenantId"),
  58. WithUserIDParamsName("createUserId"))...)
  59. }
  60. func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  61. params.passThrough(builder, http.MethodGet, append(opts,
  62. WithTenantIDParamsName("tenantId"),
  63. WithUserIDParamsName("deleteUserId"))...)
  64. }
  65. func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  66. params.passThrough(builder, http.MethodGet, append(opts,
  67. WithTenantIDParamsName("tenantId"),
  68. WithUserIDParamsName("updateUserId"))...)
  69. }
  70. func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  71. params.passThrough(builder, http.MethodPost, append(opts,
  72. WithTenantIDParamsName("tenantId"))...)
  73. }
  74. func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  75. params.passThrough(builder, http.MethodDelete, append(opts,
  76. WithTenantIDParamsName("tenantId"))...)
  77. }
  78. func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  79. params.passThrough(builder, http.MethodPut, append(opts,
  80. WithTenantIDParamsName("tenantId"))...)
  81. }
  82. func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  83. params.passThrough(builder, http.MethodGet, append(opts,
  84. WithTenantIDParamsName("tenantId"))...)
  85. }
  86. func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  87. params.passThrough(builder, http.MethodPost, append(opts,
  88. WithUserIDParamsName("userId"))...)
  89. }
  90. func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  91. params.passThrough(builder, http.MethodPost, append(opts,
  92. WithUserIDParamsName("createUserId"))...)
  93. }
  94. func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  95. params.passThrough(builder, http.MethodDelete, append(opts,
  96. WithUserIDParamsName("userId"))...)
  97. }
  98. func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  99. params.passThrough(builder, http.MethodDelete, append(opts,
  100. WithUserIDParamsName("deleteUserId"))...)
  101. }
  102. func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  103. params.passThrough(builder, http.MethodPut, append(opts,
  104. WithUserIDParamsName("userId"))...)
  105. }
  106. func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  107. params.passThrough(builder, http.MethodPut, append(opts,
  108. WithUserIDParamsName("updateUserId"))...)
  109. }
  110. func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  111. params.passThrough(builder, http.MethodGet, append(opts,
  112. WithUserIDParamsName("userId"))...)
  113. }
  114. func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  115. params.passThrough(builder, http.MethodGet, append(opts,
  116. WithUserIDParamsName("createUserId"))...)
  117. }
  118. func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  119. params.passThrough(builder, http.MethodGet, append(opts,
  120. WithUserIDParamsName("deleteUserId"))...)
  121. }
  122. func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  123. params.passThrough(builder, http.MethodGet, append(opts,
  124. WithUserIDParamsName("updateUserId"))...)
  125. }
  126. type Simple struct {
  127. RelativePath string
  128. ServiceUrl string
  129. }
  130. func (params *Simple) passThrough(builder *gateway.Builder, httpMethod string, opts ...Option) {
  131. options := new(Options)
  132. for _, opt := range opts {
  133. opt(options)
  134. }
  135. builder.AddRoute(httpMethod, params.RelativePath,
  136. func(requestBuilder *gateway.RequestBuilder) {
  137. if options.responseSuccessCallback != nil {
  138. requestBuilder.ResponseSuccessCallback(options.responseSuccessCallback)
  139. }
  140. if options.responseErrorCallback != nil {
  141. requestBuilder.ResponseErrorCallback(options.responseErrorCallback)
  142. }
  143. if strutils.IsStringNotEmpty(options.tenantIDParamsName) ||
  144. strutils.IsStringNotEmpty(options.userIDParamsName) {
  145. if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
  146. err := gateway.AddJsonBodyTenantIDAndUserID(requestBuilder, options.tenantIDParamsName, options.userIDParamsName)
  147. if err != nil {
  148. requestBuilder.ResponseError(err)
  149. return
  150. }
  151. }
  152. if httpMethod == http.MethodDelete || httpMethod == http.MethodGet {
  153. err := gateway.AddQueryParamsTenantIDAndUserID(requestBuilder, options.tenantIDParamsName, options.userIDParamsName)
  154. if err != nil {
  155. requestBuilder.ResponseError(err)
  156. return
  157. }
  158. }
  159. }
  160. if options.beforeRequestCallback != nil {
  161. err := options.beforeRequestCallback(requestBuilder)
  162. if err != nil {
  163. requestBuilder.ResponseError(err)
  164. return
  165. }
  166. }
  167. requestOptions := make([]gateway.RequestOption, 0)
  168. if options.afterRequestCallback != nil {
  169. requestOptions = append(requestOptions, gateway.WithRequestResponseCallback(
  170. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  171. err := options.afterRequestCallback(requestBuilder)
  172. if err != nil {
  173. return err
  174. }
  175. return nil
  176. }))
  177. }
  178. switch httpMethod {
  179. case http.MethodPost:
  180. requestBuilder.Post(&gateway.PostRequest{
  181. Url: params.ServiceUrl,
  182. }, requestOptions...)
  183. case http.MethodDelete:
  184. requestBuilder.Delete(&gateway.DeleteRequest{
  185. Url: params.ServiceUrl,
  186. }, requestOptions...)
  187. case http.MethodPut:
  188. requestBuilder.Put(&gateway.PutRequest{
  189. Url: params.ServiceUrl,
  190. }, requestOptions...)
  191. case http.MethodGet:
  192. requestBuilder.Get(&gateway.GetRequest{
  193. Url: params.ServiceUrl,
  194. }, requestOptions...)
  195. default:
  196. panic("不支持的请求类型")
  197. }
  198. requestBuilder.Request()
  199. }, options.middlewares...)
  200. }
  201. type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
  202. type Option func(options *Options)
  203. type Options struct {
  204. // tenantIDParamsName 租户ID请求参数名称
  205. tenantIDParamsName string
  206. // userIDParamsName 用户ID请求参数名称
  207. userIDParamsName string
  208. // beforeRequestCallback 请求前回调
  209. beforeRequestCallback RequestBuilderCallback
  210. // afterRequestCallback 请求后回调
  211. afterRequestCallback RequestBuilderCallback
  212. // responseSuccessCallback 成功响应回调
  213. responseSuccessCallback gateway.ResponseSuccessCallback
  214. // responseErrorCallback 失败响应回调
  215. responseErrorCallback gateway.ResponseErrorCallback
  216. // 中间件
  217. middlewares []gateway.Handler
  218. }
  219. func WithTenantIDParamsName(tenantIDParamsName string) Option {
  220. return func(options *Options) {
  221. options.tenantIDParamsName = tenantIDParamsName
  222. }
  223. }
  224. func WithUserIDParamsName(userIDParamsName string) Option {
  225. return func(options *Options) {
  226. options.userIDParamsName = userIDParamsName
  227. }
  228. }
  229. func WithBeforeRequestCallback(callback RequestBuilderCallback) Option {
  230. return func(options *Options) {
  231. options.beforeRequestCallback = callback
  232. }
  233. }
  234. func WithAfterRequestCallback(callback RequestBuilderCallback) Option {
  235. return func(options *Options) {
  236. options.afterRequestCallback = callback
  237. }
  238. }
  239. func WithResponseSuccessCallback(callback gateway.ResponseSuccessCallback) Option {
  240. return func(options *Options) {
  241. options.responseSuccessCallback = callback
  242. }
  243. }
  244. func WithResponseErrorCallback(callback gateway.ResponseErrorCallback) Option {
  245. return func(options *Options) {
  246. options.responseErrorCallback = callback
  247. }
  248. }
  249. func WithMiddlewares(middlewares ...gateway.Handler) Option {
  250. return func(options *Options) {
  251. options.middlewares = middlewares
  252. }
  253. }