From f34a4d29de62245ccdce794177daa5b9cc2f8c6e Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 8 Apr 2020 15:38:02 +0100 Subject: [PATCH] Strip namespace from registry router --- registry.go | 31 ++----------------------------- registry_test.go | 45 --------------------------------------------- 2 files changed, 2 insertions(+), 74 deletions(-) diff --git a/registry.go b/registry.go index 375aa7d..b3f0495 100644 --- a/registry.go +++ b/registry.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "regexp" - "strings" "sync" "time" @@ -29,28 +28,6 @@ type registryRouter struct { eps map[string]*api.Service } -func setNamespace(ns, name string) string { - ns = strings.TrimSpace(ns) - name = strings.TrimSpace(name) - - // no namespace - if len(ns) == 0 { - return name - } - - switch { - // has - suffix - case strings.HasSuffix(ns, "-"): - return strings.Replace(ns+name, ".", "-", -1) - // has . suffix - case strings.HasSuffix(ns, "."): - return ns + name - } - - // default join . - return strings.Join([]string{ns, name}, ".") -} - func (r *registryRouter) isClosed() bool { select { case <-r.exit: @@ -79,10 +56,6 @@ func (r *registryRouter) refresh() { // for each service, get service and store endpoints for _, s := range services { - // only get services for this namespace - if !strings.HasPrefix(s.Name, r.opts.Namespace) { - continue - } service, err := r.rc.GetService(s.Name) if err != nil { if logger.V(logger.ErrorLevel, logger.DefaultLogger) { @@ -105,7 +78,7 @@ func (r *registryRouter) refresh() { // process watch event func (r *registryRouter) process(res *registry.Result) { // skip these things - if res == nil || res.Service == nil || !strings.HasPrefix(res.Service.Name, r.opts.Namespace) { + if res == nil || res.Service == nil { return } @@ -350,7 +323,7 @@ func (r *registryRouter) Route(req *http.Request) (*api.Service, error) { } // service name - name := setNamespace(r.opts.Namespace, rp.Name) + name := rp.Name // get service services, err := r.rc.GetService(name) diff --git a/registry_test.go b/registry_test.go index 4564623..5824d23 100644 --- a/registry_test.go +++ b/registry_test.go @@ -9,51 +9,6 @@ import ( "github.com/micro/go-micro/v2/api" ) -func TestSetNamespace(t *testing.T) { - testCases := []struct { - namespace string - name string - expected string - }{ - // default dotted path - { - "go.micro.api", - "foo", - "go.micro.api.foo", - }, - // dotted end - { - "go.micro.api.", - "foo", - "go.micro.api.foo", - }, - // dashed end - { - "go-micro-api-", - "foo", - "go-micro-api-foo", - }, - // no namespace - { - "", - "foo", - "foo", - }, - { - "go-micro-api-", - "v2.foo", - "go-micro-api-v2-foo", - }, - } - - for _, test := range testCases { - name := setNamespace(test.namespace, test.name) - if name != test.expected { - t.Fatalf("expected name %s got %s", test.expected, name) - } - } -} - func TestRouter(t *testing.T) { r := newRouter()