From fdd4ce3c195def2bd13712106cea3c0bd928d035 Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Mon, 2 Sep 2024 18:27:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9B=B4=E6=96=B0=E9=9B=86?= =?UTF-8?q?=E7=BE=A4=E6=97=B6=E9=85=8D=E7=BD=AE=E4=B8=8D=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gateway/apinto/entity/router.go | 31 +++++++++++++++++++------------ gateway/resource.go | 12 ++++++++---- module/publish/iml.go | 18 ++++++++++++++---- module/subscribe/iml.go | 2 +- module/subscribe/subscribe.go | 3 +++ 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/gateway/apinto/entity/router.go b/gateway/apinto/entity/router.go index 5b82feb9..ce711e3f 100644 --- a/gateway/apinto/entity/router.go +++ b/gateway/apinto/entity/router.go @@ -4,9 +4,9 @@ import ( "fmt" "net/textproto" "strings" - + "github.com/APIParkLab/APIPark/common" - + "github.com/APIParkLab/APIPark/common/enum" "github.com/APIParkLab/APIPark/gateway" ) @@ -26,6 +26,7 @@ type Router struct { Retry int `json:"retry"` TimeOut int `json:"time_out"` Labels map[string]string `json:"labels"` + Disable bool `json:"disable"` } type Rule struct { @@ -79,7 +80,7 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) //若请求路径包含restful参数 if common.IsRestfulPath(r.Path) { rewritePlugin.PathType = "regex" //正则替换 - + //如果转发路径包含restful参数 if common.IsRestfulPath(r.ProxyPath) { r.ProxyPath = formatProxyPath(r.Path, r.ProxyPath) @@ -100,7 +101,7 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) }, } } - + rules := make([]*Rule, 0, len(r.Rules)) for _, m := range r.Rules { rule := &Rule{ @@ -108,11 +109,11 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) Name: m.Key, Value: "", } - + if m.Position == enum.MatchPositionHeader { rule.Name = textproto.CanonicalMIMEHeaderKey(rule.Name) } - + switch m.MatchType { case enum.MatchTypeEqual: rule.Value = m.Pattern @@ -137,7 +138,7 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) case enum.MatchTypeAny: rule.Value = "*" } - + rules = append(rules, rule) } plugin := map[string]*Plugin{ @@ -160,7 +161,11 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) if r.Labels != nil { labels = r.Labels } - + protocols := []string{"http", "https"} + if len(r.Protocols) > 0 { + protocols = r.Protocols + } + return &Router{ BasicInfo: &BasicInfo{ ID: fmt.Sprintf("%s@router", r.ID), @@ -170,8 +175,9 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) Version: version, Matches: matches, }, - Host: hosts, - Method: r.Method, + Host: hosts, + Method: r.Methods, + Location: r.Path, Rules: rules, Service: r.Service, @@ -179,7 +185,8 @@ func ToRouter(r *gateway.ApiRelease, version string, matches map[string]string) Retry: r.Retry, TimeOut: r.Timeout, Labels: labels, - Protocols: []string{"http", "https"}, + Protocols: protocols, + Disable: r.Disable, } } @@ -195,7 +202,7 @@ func formatProxyPath(requestPath, proxyPath string) string { i += 1 } } - + for param, order := range restfulSet { newProxyPath = strings.ReplaceAll(newProxyPath, param, order) } diff --git a/gateway/resource.go b/gateway/resource.go index ddafc3ac..22abc93d 100644 --- a/gateway/resource.go +++ b/gateway/resource.go @@ -34,9 +34,11 @@ type ProjectRelease struct { type ApiRelease struct { *BasicItem - Path string - Method []string - Host []string + Path string + Methods []string + Host []string + Protocols []string + Plugins map[string]*Plugin Service string Rules []*MatchRule @@ -45,7 +47,9 @@ type ApiRelease struct { ProxyHeaders []*ProxyHeader Retry int Timeout int - Labels map[string]string + + Labels map[string]string + Disable bool } type ProxyHeader struct { diff --git a/module/publish/iml.go b/module/publish/iml.go index 0b050e59..12997bab 100644 --- a/module/publish/iml.go +++ b/module/publish/iml.go @@ -71,7 +71,15 @@ func (m *imlPublishModule) initGateway(ctx context.Context, partitionId string, if err != nil { return err } + apiIds := utils.SliceToSlice(releaseInfo.Apis, func(api *gateway.ApiRelease) string { + return api.ID + }) + clientDriver.Service().Online(ctx, &gateway.ServiceRelease{ + ID: releaseInfo.Id, + Apis: apiIds, + }) } + return nil } @@ -127,9 +135,11 @@ func (m *imlPublishModule) getProjectRelease(ctx context.Context, projectID stri Description: a.Description, Version: version, }, - Path: a.Path, - Method: a.Methods, - Service: a.Service, + Path: a.Path, + Methods: a.Methods, + Service: a.Service, + Protocols: a.Protocols, + Disable: a.Disable, } proxy, ok := proxyCommitMap[a.UUID] if ok { @@ -220,7 +230,7 @@ func (m *imlPublishModule) getReleaseInfo(ctx context.Context, projectID, releas Version: version, }, Path: a.Path, - Method: a.Methods, + Methods: a.Methods, Service: a.Service, } proxy, ok := proxyCommitMap[a.UUID] diff --git a/module/subscribe/iml.go b/module/subscribe/iml.go index 8d267f9a..0a77a2cf 100644 --- a/module/subscribe/iml.go +++ b/module/subscribe/iml.go @@ -77,7 +77,7 @@ func (i *imlSubscribeModule) getSubscribers(ctx context.Context, serviceIds []st }), nil } -func (i *imlSubscribeModule) initGateway(ctx context.Context, clientDriver gateway.IClientDriver) error { +func (i *imlSubscribeModule) initGateway(ctx context.Context, clusterId string, clientDriver gateway.IClientDriver) error { projects, err := i.serviceService.List(ctx) if err != nil { diff --git a/module/subscribe/subscribe.go b/module/subscribe/subscribe.go index 75480f43..a9e93778 100644 --- a/module/subscribe/subscribe.go +++ b/module/subscribe/subscribe.go @@ -2,6 +2,7 @@ package subscribe import ( "context" + "github.com/APIParkLab/APIPark/gateway" "github.com/APIParkLab/APIPark/module/system" "reflect" @@ -54,6 +55,8 @@ type IExportSubscribeApprovalModule interface { func init() { subscribeModule := new(imlSubscribeModule) autowire.Auto[ISubscribeModule](func() reflect.Value { + + gateway.RegisterInitHandleFunc(subscribeModule.initGateway) return reflect.ValueOf(subscribeModule) })