Explorar el Código

添加直通便捷包

yjp hace 1 año
padre
commit
0c4866a3d6

+ 0 - 114
convenient/gateway/body_mapping.go

@@ -1,114 +0,0 @@
-package gateway
-
-import (
-	"git.sxidc.com/go-framework/baize/framework/binding"
-	"git.sxidc.com/go-framework/baize/framework/binding/request"
-	"git.sxidc.com/go-framework/baize/framework/binding/response"
-	"git.sxidc.com/go-framework/baize/framework/core/api"
-	"git.sxidc.com/go-framework/baize/framework/core/domain"
-	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
-	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
-	"git.sxidc.com/go-tools/utils/http_client"
-	"git.sxidc.com/service-supports/fserr"
-	"io"
-	"net/http"
-	"strings"
-	"time"
-)
-
-func (gw *Gateway) BodyMapping(binder *binding.Binder, bodyMapping *BodyMapping, middlewares ...api.Handler) {
-	bodyMapping.bind(binder, gw.httpClient, gw.options.timeoutSec, middlewares...)
-}
-
-type BodyMapping struct {
-	HttpMethod   string
-	RelativePath string
-	ToUrl        string
-
-	BodyMappingFunc func(body []byte) ([]byte, error)
-}
-
-func (bodyMapping *BodyMapping) bind(binder *binding.Binder, httpClient *http_client.Client, timeoutSec int64, middlewares ...api.Handler) {
-	binding.Bind[any](binder, &binding.BindItem[any]{
-		Method: bodyMapping.HttpMethod,
-		SimpleBindItem: &binding.SimpleBindItem[any]{
-			Path:         bodyMapping.RelativePath,
-			ResponseFunc: response.NoResponse,
-			ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
-				body, err := io.ReadAll(c.Request.Body)
-				if err != nil {
-					logger.GetInstance().Error(err)
-					c.AbortWithStatus(http.StatusBadGateway)
-					return nil, err
-				}
-
-				defer func() {
-					err := c.Request.Body.Close()
-					if err != nil {
-						logger.GetInstance().Error(err)
-						c.AbortWithStatus(http.StatusBadGateway)
-					}
-				}()
-
-				if bodyMapping.BodyMappingFunc != nil {
-					newBody, err := bodyMapping.BodyMappingFunc(body)
-					if err != nil {
-						logger.GetInstance().Error(err)
-						c.AbortWithStatus(http.StatusBadGateway)
-						return nil, err
-					}
-
-					body = newBody
-				}
-
-				headers := make(map[string]string)
-				for key, values := range c.Request.Header {
-					headers[key] = strings.Join(values, ",")
-				}
-
-				var httpResponse *http_client.Response
-				httpRequest := httpClient.
-					NewRequest(http_client.WithNewRequestTimeout(time.Duration(timeoutSec) * time.Second))
-
-				switch bodyMapping.HttpMethod {
-				case http.MethodPost:
-					resp, err := httpRequest.Post(bodyMapping.ToUrl, body, http_client.WithRequestHeaders(headers))
-					if err != nil {
-						logger.GetInstance().Error(err)
-						c.AbortWithStatus(http.StatusBadGateway)
-						return nil, err
-					}
-
-					httpResponse = resp
-				case http.MethodPut:
-					resp, err := httpRequest.Put(bodyMapping.ToUrl, body, http_client.WithRequestHeaders(headers))
-					if err != nil {
-						logger.GetInstance().Error(err)
-						c.AbortWithStatus(http.StatusBadGateway)
-						return nil, err
-					}
-
-					httpResponse = resp
-				default:
-					err := fserr.New("不支持Body传递参数")
-					logger.GetInstance().Error(err)
-					c.AbortWithStatus(http.StatusBadGateway)
-					return nil, err
-				}
-
-				c.Status(http.StatusOK)
-
-				_, err = c.Writer.Write(httpResponse.Body())
-				if err != nil {
-					logger.GetInstance().Error(err)
-					c.AbortWithStatus(http.StatusBadGateway)
-					return nil, err
-				}
-
-				c.Writer.Flush()
-
-				return nil, nil
-			},
-		},
-	}, middlewares...)
-}

+ 0 - 49
convenient/gateway/gateway.go

@@ -1,49 +0,0 @@
-package gateway
-
-import "git.sxidc.com/go-tools/utils/http_client"
-
-type Gateway struct {
-	httpClient *http_client.Client
-
-	options *Options
-}
-
-func NewGateway(opts ...Option) *Gateway {
-	options := new(Options)
-
-	for _, opt := range opts {
-		opt(options)
-	}
-
-	if options.timeoutSec == 0 {
-		options.timeoutSec = 30
-	}
-
-	return &Gateway{
-		httpClient: http_client.New(),
-		options:    options,
-	}
-}
-
-func DestroyGateway(gateway *Gateway) {
-	if gateway == nil {
-		return
-	}
-
-	http_client.Destroy(gateway.httpClient)
-	gateway.httpClient = nil
-
-	gateway = nil
-}
-
-type Option func(options *Options)
-
-type Options struct {
-	timeoutSec int64
-}
-
-func WithTimeoutSec(timeoutSec int64) Option {
-	return func(options *Options) {
-		options.timeoutSec = timeoutSec
-	}
-}

+ 188 - 0
convenient/gwtools/pass_through.go

@@ -0,0 +1,188 @@
+package gwtools
+
+import (
+	"git.sxidc.com/go-framework/baize/framework/core/api"
+	"git.sxidc.com/go-framework/baize/framework/gateway"
+	"net/http"
+)
+
+func PostPassThrough(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodPost, middlewares...)
+}
+
+func DeletePassThrough(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodDelete, middlewares...)
+}
+
+func PutPassThrough(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodPut, middlewares...)
+}
+
+func GetPassThrough(builder *gateway.Builder, params *SimplePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodGet, middlewares...)
+}
+
+func PassThrough(builder *gateway.Builder, params *PassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, middlewares...)
+}
+
+func OnePostPassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodPost, middlewares...)
+}
+
+func OneDeletePassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodDelete, middlewares...)
+}
+
+func OnePutPassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodPut, middlewares...)
+}
+
+func OneGetPassThrough(builder *gateway.Builder, params *SimpleOnePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodGet, middlewares...)
+}
+
+func OnePassThrough(builder *gateway.Builder, params *OnePassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, middlewares...)
+}
+
+func PostCommonPassThrough(builder *gateway.Builder, params *SimpleCommonPassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodPost, middlewares...)
+}
+
+func DeleteCommonPassThrough(builder *gateway.Builder, params *SimpleCommonPassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodDelete, middlewares...)
+}
+
+func PutCommonPassThrough(builder *gateway.Builder, params *SimpleCommonPassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodPut, middlewares...)
+}
+
+func GetCommonPassThrough(builder *gateway.Builder, params *SimpleCommonPassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, http.MethodGet, middlewares...)
+}
+
+func CommonPassThrough(builder *gateway.Builder, params *CommonPassThroughParams, middlewares ...api.Handler) {
+	params.passThrough(builder, middlewares...)
+}
+
+type PassThroughRequestItem struct {
+	Request  gateway.BuilderRequest
+	Callback gateway.RequestCallbackFunc
+}
+
+type SimplePassThroughParams struct {
+	RelativePath string
+	Request      gateway.BuilderRequest
+}
+
+func (params *SimplePassThroughParams) passThrough(builder *gateway.Builder, httpMethod string, middlewares ...api.Handler) {
+	passThroughParams := &PassThroughParams{
+		HttpMethod:              httpMethod,
+		SimplePassThroughParams: params,
+	}
+
+	passThroughParams.passThrough(builder, middlewares...)
+}
+
+type PassThroughParams struct {
+	HttpMethod string
+	*SimplePassThroughParams
+}
+
+func (params *PassThroughParams) passThrough(builder *gateway.Builder, middlewares ...api.Handler) {
+	copyBuilder := builder.Url(params.HttpMethod, params.RelativePath)
+
+	switch req := params.Request.(type) {
+	case *gateway.PostRequest:
+		copyBuilder = copyBuilder.Post(req, nil)
+	case *gateway.DeleteRequest:
+		copyBuilder = copyBuilder.Delete(req, nil)
+	case *gateway.PutRequest:
+		copyBuilder = copyBuilder.Put(req, nil)
+	case *gateway.GetRequest:
+		copyBuilder = copyBuilder.Get(req, nil)
+	default:
+		panic("不支持的请求类型")
+	}
+
+	copyBuilder.Build(middlewares...)
+}
+
+type SimpleOnePassThroughParams struct {
+	RelativePath string
+	RequestItem  *PassThroughRequestItem
+}
+
+func (params *SimpleOnePassThroughParams) passThrough(builder *gateway.Builder, httpMethod string, middlewares ...api.Handler) {
+	onePassThroughParams := &OnePassThroughParams{
+		HttpMethod:                 httpMethod,
+		SimpleOnePassThroughParams: params,
+	}
+
+	onePassThroughParams.passThrough(builder, middlewares...)
+}
+
+type OnePassThroughParams struct {
+	HttpMethod string
+	*SimpleOnePassThroughParams
+}
+
+func (params *OnePassThroughParams) passThrough(builder *gateway.Builder, middlewares ...api.Handler) {
+	copyBuilder := builder.Url(params.HttpMethod, params.RelativePath)
+
+	switch req := params.RequestItem.Request.(type) {
+	case *gateway.PostRequest:
+		copyBuilder = copyBuilder.Post(req, params.RequestItem.Callback)
+	case *gateway.DeleteRequest:
+		copyBuilder = copyBuilder.Delete(req, params.RequestItem.Callback)
+	case *gateway.PutRequest:
+		copyBuilder = copyBuilder.Put(req, params.RequestItem.Callback)
+	case *gateway.GetRequest:
+		copyBuilder = copyBuilder.Get(req, params.RequestItem.Callback)
+	default:
+		panic("不支持的请求类型")
+	}
+
+	copyBuilder.Build(middlewares...)
+}
+
+type SimpleCommonPassThroughParams struct {
+	RelativePath string
+	RequestItems []PassThroughRequestItem
+}
+
+func (params *SimpleCommonPassThroughParams) passThrough(builder *gateway.Builder, httpMethod string, middlewares ...api.Handler) {
+	passThroughParams := &CommonPassThroughParams{
+		HttpMethod:                    httpMethod,
+		SimpleCommonPassThroughParams: params,
+	}
+
+	passThroughParams.passThrough(builder, middlewares...)
+}
+
+type CommonPassThroughParams struct {
+	HttpMethod string
+	*SimpleCommonPassThroughParams
+}
+
+func (params *CommonPassThroughParams) passThrough(builder *gateway.Builder, middlewares ...api.Handler) {
+	copyBuilder := builder.Url(params.HttpMethod, params.RelativePath)
+
+	for _, requestItem := range params.RequestItems {
+		switch req := requestItem.Request.(type) {
+		case *gateway.PostRequest:
+			copyBuilder = copyBuilder.Post(req, requestItem.Callback)
+		case *gateway.DeleteRequest:
+			copyBuilder = copyBuilder.Delete(req, requestItem.Callback)
+		case *gateway.PutRequest:
+			copyBuilder = copyBuilder.Put(req, requestItem.Callback)
+		case *gateway.GetRequest:
+			copyBuilder = copyBuilder.Get(req, requestItem.Callback)
+		default:
+			panic("不支持的请求类型")
+		}
+	}
+
+	copyBuilder.Build(middlewares...)
+}

+ 6 - 5
framework/gateway/builder.go

@@ -19,11 +19,12 @@ type Builder struct {
 	params *builderParams
 }
 
-func newBuilder(router api.Router, httpClient *http_client.Client) *Builder {
+func newBuilder(router api.Router, httpClient *http_client.Client, httpTimeoutSec int64) *Builder {
 	return &Builder{
-		router:     router,
-		httpClient: httpClient,
-		params:     newBuilderParams(),
+		router:         router,
+		httpClient:     httpClient,
+		httpTimeoutSec: httpTimeoutSec,
+		params:         newBuilderParams(),
 	}
 }
 
@@ -117,7 +118,7 @@ func (builder *Builder) addRequest(item *builderRequestItem) *Builder {
 }
 
 func (builder *Builder) copy() *Builder {
-	copyBuilder := newBuilder(builder.router, builder.httpClient)
+	copyBuilder := newBuilder(builder.router, builder.httpClient, builder.httpTimeoutSec)
 	copyBuilder.params = builder.params.copy()
 	return copyBuilder
 }

+ 7 - 5
framework/gateway/builder_params.go

@@ -25,11 +25,13 @@ func newBuilderParams() *builderParams {
 		responseSuccessFunc: func(c *api.Context, historyRequests []BuilderRequest, customResultMap map[string]any) {
 			c.Status(http.StatusOK)
 
-			_, err := c.Writer.Write(historyRequests[len(historyRequests)-1].Response().Body())
-			if err != nil {
-				logger.GetInstance().Error(fserr.New(err.Error()))
-				c.AbortWithStatus(http.StatusInternalServerError)
-				return
+			if historyRequests != nil && len(historyRequests) != 0 {
+				_, err := c.Writer.Write(historyRequests[len(historyRequests)-1].Response().Body())
+				if err != nil {
+					logger.GetInstance().Error(fserr.New(err.Error()))
+					c.AbortWithStatus(http.StatusInternalServerError)
+					return
+				}
 			}
 
 			c.Writer.Flush()

+ 1 - 2
framework/gateway/gateway.go

@@ -40,8 +40,7 @@ func DestroyGateway(gw *Gateway) {
 }
 
 func (gw *Gateway) NewBuilder(routerType string, version string) *Builder {
-
-	return newBuilder(gw.api.ChooseRouter(routerType, version), gw.httpClient)
+	return newBuilder(gw.api.ChooseRouter(routerType, version), gw.httpClient, gw.options.httpClientTimeoutSec)
 }
 
 type Option func(options *Options)