package one2one import ( "git.sxidc.com/go-framework/baize/framework/gateway" "git.sxidc.com/go-tools/utils/template" "github.com/iancoleman/strcase" ) func BindSimple(builder *gateway.Builder, params *Simple, opts ...Option) { options := new(Options) for _, opt := range opts { opt(options) } params.options = options params.bind(builder) } type Simple struct { // 除去后缀的服务URL,如http://localhost:8080/example/api/v1 ServiceVersionedUrl string // 服务缩写 ServiceShortName string // 左领域名称 LeftDomainCamelName string // 右领域名称 RightDomainCamelName string // 可选配置项,通过WithXXX配置 options *Options } func (simple *Simple) bind(builder *gateway.Builder) { options := simple.options leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.LeftDomainCamelName)) rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(simple.RightDomainCamelName)) if !options.disableLeft { if !options.disableLeftUpdate { // 左到右更新 builder. PostRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/update", func(requestBuilder *gateway.RequestBuilder) { requestBuilder. Post(&gateway.PostRequest{ Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update", }). Request() }) } if !options.disableLeftQuery { // 左到右查询 builder. GetRoute("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/query", func(requestBuilder *gateway.RequestBuilder) { requestBuilder. Get(&gateway.GetRequest{ Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query", }). Request() }) } if !options.disableLeftWithRightQuery { // 左到右查询,携带右方信息 builder. GetRouteWithTenantID("/"+simple.ServiceShortName+leftDomainPath+rightDomainPath+"/queryWith", func(requestBuilder *gateway.RequestBuilder) { requestBuilder. Get(&gateway.GetRequest{ Url: simple.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith", }). Request() }) } } if !options.disableRight { if !options.disableRightUpdate { // 右到左更新 builder. PostRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/update", func(requestBuilder *gateway.RequestBuilder) { requestBuilder. Post(&gateway.PostRequest{ Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update", }). Request() }) } if !options.disableRightQuery { // 右到左查询 builder. GetRoute("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/query", func(requestBuilder *gateway.RequestBuilder) { requestBuilder. Get(&gateway.GetRequest{ Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query", }). Request() }) } if !options.disableRightWithLeftQuery { // 右到左查询,携带左方信息 builder. GetRouteWithTenantID("/"+simple.ServiceShortName+rightDomainPath+leftDomainPath+"/queryWith", func(requestBuilder *gateway.RequestBuilder) { requestBuilder. Get(&gateway.GetRequest{ Url: simple.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith", }). Request() }) } } } type Option func(options *Options) type Options struct { // 关闭左侧到右侧关联 disableLeft bool // 关闭右侧到左侧关联 disableRight bool // 关闭左侧更新 disableLeftUpdate bool // 关闭左侧查询 disableLeftQuery bool // 关闭右侧更新 disableRightUpdate bool // 关闭右侧查询 disableRightQuery bool // 关闭左实体带右实体信息查询 disableLeftWithRightQuery bool // 关闭右实体带左实体信息查询 disableRightWithLeftQuery bool } func WithDisableLeft() Option { return func(options *Options) { options.disableLeft = true } } func WithDisableRight() Option { return func(options *Options) { options.disableRight = true } } func WithDisableLeftUpdate() Option { return func(options *Options) { options.disableLeftUpdate = true } } func WithDisableLeftQuery() Option { return func(options *Options) { options.disableLeftQuery = true } } func WithDisableRightUpdate() Option { return func(options *Options) { options.disableRightUpdate = true } } func WithDisableRightQuery() Option { return func(options *Options) { options.disableRightQuery = true } } func WithDisableLeftWithRightQuery() Option { return func(options *Options) { options.disableLeftWithRightQuery = true } } func WithDisableRightWithLeftQuery() Option { return func(options *Options) { options.disableRightWithLeftQuery = true } }