Browse Source

完成网关一对一

yjp 1 year ago
parent
commit
0142318fed
1 changed files with 203 additions and 0 deletions
  1. 203 0
      convenient/gwtools/one2one.go

+ 203 - 0
convenient/gwtools/one2one.go

@@ -0,0 +1,203 @@
+package gwtools
+
+import (
+	"git.sxidc.com/go-framework/baize/framework/gateway"
+	"git.sxidc.com/go-tools/utils/template"
+	"github.com/iancoleman/strcase"
+	"net/http"
+)
+
+func One2One(builder *gateway.Builder, params *One2OneParams, opts ...Option) {
+	options := new(Options)
+
+	for _, opt := range opts {
+		opt(options)
+	}
+
+	params.options = options
+
+	params.one2one(builder)
+}
+
+type One2OneParams struct {
+	// 除去后缀的服务URL,如http://localhost:8080/example/api/v1
+	ServiceVersionedUrl string
+
+	// 左领域名称
+	LeftDomainCamelName string
+
+	// 右领域名称
+	RightDomainCamelName string
+
+	// 可选配置项,通过WithXXX配置
+	options *Options
+}
+
+func (params *One2OneParams) one2one(builder *gateway.Builder) {
+	options := params.options
+
+	leftDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.LeftDomainCamelName))
+	rightDomainPath := "/" + strcase.ToLowerCamel(template.Id(params.RightDomainCamelName))
+
+	if !options.disableLeft {
+		if !options.disableLeftUpdate {
+			// 左到右更新
+			builder.
+				Url(http.MethodPost, leftDomainPath+rightDomainPath+"/update").
+				Post(&gateway.PostRequest{
+					Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/update",
+				}, nil).
+				Build()
+		}
+
+		if !options.disableLeftQuery {
+			// 左到右查询
+			builder.
+				Url(http.MethodGet, leftDomainPath+rightDomainPath+"/query").
+				Get(&gateway.GetRequest{
+					Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/query",
+				}, nil).
+				Build()
+		}
+
+		if !options.disableLeftWithRightQuery {
+			// 左到右查询,携带右方信息
+			builder.
+				Url(http.MethodGet, leftDomainPath+rightDomainPath+"/queryWith").
+				Get(&gateway.GetRequest{
+					Url: params.ServiceVersionedUrl + leftDomainPath + rightDomainPath + "/queryWith",
+					QueryParams: AddQueryParamsTenantIDAndUserID("tenantId", "",
+						params.options.leftWithRightQueryGetTenantIDFunc, nil),
+				}, nil).
+				Build()
+		}
+	}
+
+	if !options.disableRight {
+		if !options.disableRightUpdate {
+			// 右到左更新
+			builder.
+				Url(http.MethodPost, rightDomainPath+leftDomainPath+"/update").
+				Post(&gateway.PostRequest{
+					Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/update",
+				}, nil).
+				Build()
+		}
+
+		if !options.disableRightQuery {
+			// 右到左查询
+			builder.
+				Url(http.MethodGet, rightDomainPath+leftDomainPath+"/query").
+				Get(&gateway.GetRequest{
+					Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/query",
+				}, nil).
+				Build()
+		}
+
+		if !options.disableRightWithLeftQuery {
+			// 右到左查询,携带左方信息
+			builder.
+				Url(http.MethodGet, rightDomainPath+leftDomainPath+"/queryWith").
+				Get(&gateway.GetRequest{
+					Url: params.ServiceVersionedUrl + rightDomainPath + leftDomainPath + "/queryWith",
+					QueryParams: AddQueryParamsTenantIDAndUserID("tenantId", "",
+						params.options.rightWithLeftQueryGetTenantIDFunc, nil),
+				}, nil).
+				Build()
+		}
+	}
+}
+
+type Option func(options *Options)
+
+type Options struct {
+	// 关闭左侧到右侧关联
+	disableLeft bool
+
+	// 关闭右侧到左侧关联
+	disableRight bool
+
+	// 关闭左侧更新
+	disableLeftUpdate bool
+
+	// 关闭左侧查询
+	disableLeftQuery bool
+
+	// 关闭右侧更新
+	disableRightUpdate bool
+
+	// 关闭右侧查询
+	disableRightQuery bool
+
+	// 关闭左实体带右实体信息查询
+	disableLeftWithRightQuery bool
+
+	// 关闭右实体带左实体信息查询
+	disableRightWithLeftQuery bool
+
+	// 获取租户ID的接口
+	leftWithRightQueryGetTenantIDFunc GetTenantIDFunc
+
+	// 获取租户ID的接口
+	rightWithLeftQueryGetTenantIDFunc GetTenantIDFunc
+}
+
+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
+	}
+}
+
+func WithLeftWithRightQueryGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) Option {
+	return func(options *Options) {
+		options.leftWithRightQueryGetTenantIDFunc = getTenantIDFunc
+	}
+}
+
+func WithRightWithLeftQueryGetTenantIDFunc(getTenantIDFunc GetTenantIDFunc) Option {
+	return func(options *Options) {
+		options.rightWithLeftQueryGetTenantIDFunc = getTenantIDFunc
+	}
+}