one2one.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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",
  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",
  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",
  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",
  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",
  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",
  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. }
  127. func WithDisableLeft() Option {
  128. return func(options *Options) {
  129. options.disableLeft = true
  130. }
  131. }
  132. func WithDisableRight() Option {
  133. return func(options *Options) {
  134. options.disableRight = true
  135. }
  136. }
  137. func WithDisableLeftUpdate() Option {
  138. return func(options *Options) {
  139. options.disableLeftUpdate = true
  140. }
  141. }
  142. func WithDisableLeftQuery() Option {
  143. return func(options *Options) {
  144. options.disableLeftQuery = true
  145. }
  146. }
  147. func WithDisableRightUpdate() Option {
  148. return func(options *Options) {
  149. options.disableRightUpdate = true
  150. }
  151. }
  152. func WithDisableRightQuery() Option {
  153. return func(options *Options) {
  154. options.disableRightQuery = true
  155. }
  156. }
  157. func WithDisableLeftWithRightQuery() Option {
  158. return func(options *Options) {
  159. options.disableLeftWithRightQuery = true
  160. }
  161. }
  162. func WithDisableRightWithLeftQuery() Option {
  163. return func(options *Options) {
  164. options.disableRightWithLeftQuery = true
  165. }
  166. }