one2one.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. LeftDomainCamelName string
  20. // 右领域名称
  21. RightDomainCamelName string
  22. // 可选配置项,通过WithXXX配置
  23. options *Options
  24. }
  25. func (simple *Simple) bind(builder *gateway.Builder) {
  26. options := simple.options
  27. leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName))
  28. rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName))
  29. if !options.disableLeft {
  30. if !options.disableLeftUpdate {
  31. // 左到右更新
  32. builder.
  33. PostRoute(leftDomainPath+rightDomainPath+"/update",
  34. func(requestBuilder *gateway.RequestBuilder) {
  35. requestBuilder.
  36. Post(&gateway.PostRequest{
  37. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
  38. }).
  39. Request()
  40. })
  41. }
  42. if !options.disableLeftQuery {
  43. // 左到右查询
  44. builder.
  45. GetRoute(leftDomainPath+rightDomainPath+"/query",
  46. func(requestBuilder *gateway.RequestBuilder) {
  47. requestBuilder.
  48. Get(&gateway.GetRequest{
  49. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
  50. }).
  51. Request()
  52. })
  53. }
  54. if !options.disableLeftWithRightQuery {
  55. // 左到右查询,携带右方信息
  56. builder.
  57. GetRouteWithTenantID(leftDomainPath+rightDomainPath+"/queryWith",
  58. func(requestBuilder *gateway.RequestBuilder) {
  59. requestBuilder.
  60. Get(&gateway.GetRequest{
  61. Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith",
  62. }).
  63. Request()
  64. })
  65. }
  66. }
  67. if !options.disableRight {
  68. if !options.disableRightUpdate {
  69. // 右到左更新
  70. builder.
  71. PostRoute(rightDomainPath+leftDomainPath+"/update",
  72. func(requestBuilder *gateway.RequestBuilder) {
  73. requestBuilder.
  74. Post(&gateway.PostRequest{
  75. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
  76. }).
  77. Request()
  78. })
  79. }
  80. if !options.disableRightQuery {
  81. // 右到左查询
  82. builder.
  83. GetRoute(rightDomainPath+leftDomainPath+"/query",
  84. func(requestBuilder *gateway.RequestBuilder) {
  85. requestBuilder.
  86. Get(&gateway.GetRequest{
  87. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
  88. }).
  89. Request()
  90. })
  91. }
  92. if !options.disableRightWithLeftQuery {
  93. // 右到左查询,携带左方信息
  94. builder.
  95. GetRouteWithTenantID(rightDomainPath+leftDomainPath+"/queryWith",
  96. func(requestBuilder *gateway.RequestBuilder) {
  97. requestBuilder.
  98. Get(&gateway.GetRequest{
  99. Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
  100. }).
  101. Request()
  102. })
  103. }
  104. }
  105. }
  106. type Option func(options *Options)
  107. type Options struct {
  108. // 关闭左侧到右侧关联
  109. disableLeft bool
  110. // 关闭右侧到左侧关联
  111. disableRight bool
  112. // 关闭左侧更新
  113. disableLeftUpdate bool
  114. // 关闭左侧查询
  115. disableLeftQuery bool
  116. // 关闭右侧更新
  117. disableRightUpdate bool
  118. // 关闭右侧查询
  119. disableRightQuery bool
  120. // 关闭左实体带右实体信息查询
  121. disableLeftWithRightQuery bool
  122. // 关闭右实体带左实体信息查询
  123. disableRightWithLeftQuery bool
  124. }
  125. func WithDisableLeft() Option {
  126. return func(options *Options) {
  127. options.disableLeft = true
  128. }
  129. }
  130. func WithDisableRight() Option {
  131. return func(options *Options) {
  132. options.disableRight = true
  133. }
  134. }
  135. func WithDisableLeftUpdate() Option {
  136. return func(options *Options) {
  137. options.disableLeftUpdate = true
  138. }
  139. }
  140. func WithDisableLeftQuery() Option {
  141. return func(options *Options) {
  142. options.disableLeftQuery = true
  143. }
  144. }
  145. func WithDisableRightUpdate() Option {
  146. return func(options *Options) {
  147. options.disableRightUpdate = true
  148. }
  149. }
  150. func WithDisableRightQuery() Option {
  151. return func(options *Options) {
  152. options.disableRightQuery = true
  153. }
  154. }
  155. func WithDisableLeftWithRightQuery() Option {
  156. return func(options *Options) {
  157. options.disableLeftWithRightQuery = true
  158. }
  159. }
  160. func WithDisableRightWithLeftQuery() Option {
  161. return func(options *Options) {
  162. options.disableRightWithLeftQuery = true
  163. }
  164. }