one2one.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package one2one
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/gateway"
  4. "git.sxidc.com/go-tools/utils/template"
  5. "github.com/iancoleman/strcase"
  6. )
  7. func BindSimple(builder *gateway.Builder, params *Simple, opts ...Option) {
  8. options := new(Options)
  9. for _, opt := range opts {
  10. opt(options)
  11. }
  12. params.options = options
  13. params.bind(builder)
  14. }
  15. type Simple struct {
  16. // 除去后缀的服务URL,如http://localhost:8080/example/api/v1
  17. ServiceVersionedUrl string
  18. // 服务缩写
  19. ServiceShortName string
  20. // 左领域名称
  21. LeftDomainCamelName string
  22. // 右领域名称
  23. RightDomainCamelName string
  24. // 可选配置项,通过WithXXX配置
  25. options *Options
  26. }
  27. func (simple *Simple) bind(builder *gateway.Builder) {
  28. options := simple.options
  29. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
  30. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
  31. if !options.disableLeft {
  32. if !options.disableLeftUpdate {
  33. // 左到右更新
  34. builder.
  35. PostRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/update"+options.leftUpdateUrlPattern,
  36. func(requestBuilder *gateway.RequestBuilder) {
  37. requestBuilder.
  38. Post(&gateway.PostRequest{
  39. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  40. }).
  41. Request()
  42. })
  43. }
  44. if !options.disableLeftQuery {
  45. // 左到右查询
  46. builder.
  47. GetRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/query"+options.leftQueryUrlPattern,
  48. func(requestBuilder *gateway.RequestBuilder) {
  49. requestBuilder.
  50. Get(&gateway.GetRequest{
  51. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  52. }).
  53. Request()
  54. })
  55. }
  56. if !options.disableLeftWithRightQuery {
  57. // 左到右查询,携带右方信息
  58. builder.
  59. GetRouteWithTenantID("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/queryWith"+options.leftWithRightQueryUrlPattern,
  60. func(requestBuilder *gateway.RequestBuilder) {
  61. requestBuilder.
  62. Get(&gateway.GetRequest{
  63. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith",
  64. }).
  65. Request()
  66. })
  67. }
  68. }
  69. if !options.disableRight {
  70. if !options.disableRightUpdate {
  71. // 右到左更新
  72. builder.
  73. PostRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/update"+options.rightUpdateUrlPattern,
  74. func(requestBuilder *gateway.RequestBuilder) {
  75. requestBuilder.
  76. Post(&gateway.PostRequest{
  77. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  78. }).
  79. Request()
  80. })
  81. }
  82. if !options.disableRightQuery {
  83. // 右到左查询
  84. builder.
  85. GetRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/query"+options.rightQueryUrlPattern,
  86. func(requestBuilder *gateway.RequestBuilder) {
  87. requestBuilder.
  88. Get(&gateway.GetRequest{
  89. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  90. }).
  91. Request()
  92. })
  93. }
  94. if !options.disableRightWithLeftQuery {
  95. // 右到左查询,携带左方信息
  96. builder.
  97. GetRouteWithTenantID("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/queryWith"+options.rightWithLeftQueryUrlPattern,
  98. func(requestBuilder *gateway.RequestBuilder) {
  99. requestBuilder.
  100. Get(&gateway.GetRequest{
  101. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  102. }).
  103. Request()
  104. })
  105. }
  106. }
  107. }
  108. type Option func(options *Options)
  109. type Options struct {
  110. // 关闭左侧到右侧关联
  111. disableLeft bool
  112. // 关闭右侧到左侧关联
  113. disableRight bool
  114. // 关闭左侧更新
  115. disableLeftUpdate bool
  116. // 关闭左侧查询
  117. disableLeftQuery bool
  118. // 关闭右侧更新
  119. disableRightUpdate bool
  120. // 关闭右侧查询
  121. disableRightQuery bool
  122. // 关闭左实体带右实体信息查询
  123. disableLeftWithRightQuery bool
  124. // 关闭右实体带左实体信息查询
  125. disableRightWithLeftQuery bool
  126. // url模式
  127. leftUpdateUrlPattern string
  128. leftQueryUrlPattern string
  129. rightUpdateUrlPattern string
  130. rightQueryUrlPattern string
  131. leftWithRightQueryUrlPattern string
  132. rightWithLeftQueryUrlPattern string
  133. }
  134. func WithDisableLeft() Option {
  135. return func(options *Options) {
  136. options.disableLeft = true
  137. }
  138. }
  139. func WithDisableRight() Option {
  140. return func(options *Options) {
  141. options.disableRight = true
  142. }
  143. }
  144. func WithDisableLeftUpdate() Option {
  145. return func(options *Options) {
  146. options.disableLeftUpdate = true
  147. }
  148. }
  149. func WithDisableLeftQuery() Option {
  150. return func(options *Options) {
  151. options.disableLeftQuery = true
  152. }
  153. }
  154. func WithDisableRightUpdate() Option {
  155. return func(options *Options) {
  156. options.disableRightUpdate = true
  157. }
  158. }
  159. func WithDisableRightQuery() Option {
  160. return func(options *Options) {
  161. options.disableRightQuery = true
  162. }
  163. }
  164. func WithDisableLeftWithRightQuery() Option {
  165. return func(options *Options) {
  166. options.disableLeftWithRightQuery = true
  167. }
  168. }
  169. func WithDisableRightWithLeftQuery() Option {
  170. return func(options *Options) {
  171. options.disableRightWithLeftQuery = true
  172. }
  173. }
  174. func WithLeftUpdateUrlPattern(urlPattern string) Option {
  175. return func(options *Options) {
  176. options.leftUpdateUrlPattern = urlPattern
  177. }
  178. }
  179. func WithLeftQueryUrlPattern(urlPattern string) Option {
  180. return func(options *Options) {
  181. options.leftQueryUrlPattern = urlPattern
  182. }
  183. }
  184. func WithRightUpdateUrlPattern(urlPattern string) Option {
  185. return func(options *Options) {
  186. options.rightUpdateUrlPattern = urlPattern
  187. }
  188. }
  189. func WithRightQueryUrlPattern(urlPattern string) Option {
  190. return func(options *Options) {
  191. options.rightQueryUrlPattern = urlPattern
  192. }
  193. }
  194. func WithLeftWithRightQueryUrlPattern(urlPattern string) Option {
  195. return func(options *Options) {
  196. options.leftWithRightQueryUrlPattern = urlPattern
  197. }
  198. }
  199. func WithRightWithLeftQueryUrlPattern(urlPattern string) Option {
  200. return func(options *Options) {
  201. options.rightWithLeftQueryUrlPattern = urlPattern
  202. }
  203. }