From f2728a7fee8f7df4e268236e92d9d6fdfe8fe625 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sat, 17 Oct 2020 15:00:40 +0100 Subject: [PATCH] remove vpath resolver --- api/handler/http/http_test.go | 29 +------------- api/resolver/vpath/vpath.go | 75 ----------------------------------- api/router/options.go | 4 +- 3 files changed, 4 insertions(+), 104 deletions(-) delete mode 100644 api/resolver/vpath/vpath.go diff --git a/api/handler/http/http_test.go b/api/handler/http/http_test.go index 5c5c842f..6f069904 100644 --- a/api/handler/http/http_test.go +++ b/api/handler/http/http_test.go @@ -8,7 +8,7 @@ import ( "github.com/micro/go-micro/v3/api/handler" "github.com/micro/go-micro/v3/api/resolver" - "github.com/micro/go-micro/v3/api/resolver/vpath" + rpath "github.com/micro/go-micro/v3/api/resolver/path" "github.com/micro/go-micro/v3/api/router" regRouter "github.com/micro/go-micro/v3/api/router/registry" "github.com/micro/go-micro/v3/registry" @@ -57,7 +57,7 @@ func testHttp(t *testing.T, path, service, ns string) { rt := regRouter.NewRouter( router.WithHandler("http"), router.WithRegistry(r), - router.WithResolver(vpath.NewResolver( + router.WithResolver(rpath.NewResolver( resolver.WithServicePrefix(ns), )), ) @@ -92,31 +92,6 @@ func TestHttpHandler(t *testing.T) { "go.micro.api.test", "go.micro.api", }, - { - "/v1/foo", - "go.micro.api.v1.foo", - "go.micro.api", - }, - { - "/v1/foo/bar", - "go.micro.api.v1.foo", - "go.micro.api", - }, - { - "/v2/baz", - "go.micro.api.v2.baz", - "go.micro.api", - }, - { - "/v2/baz/bar", - "go.micro.api.v2.baz", - "go.micro.api", - }, - { - "/v2/baz/bar", - "v2.baz", - "", - }, } for _, d := range testData { diff --git a/api/resolver/vpath/vpath.go b/api/resolver/vpath/vpath.go deleted file mode 100644 index d0b6496f..00000000 --- a/api/resolver/vpath/vpath.go +++ /dev/null @@ -1,75 +0,0 @@ -// Package vpath resolves using http path and recognised versioned urls -package vpath - -import ( - "errors" - "net/http" - "regexp" - "strings" - - "github.com/micro/go-micro/v3/api/resolver" -) - -func NewResolver(opts ...resolver.Option) resolver.Resolver { - return &Resolver{opts: resolver.NewOptions(opts...)} -} - -type Resolver struct { - opts resolver.Options -} - -var ( - re = regexp.MustCompile("^v[0-9]+$") -) - -func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) { - if req.URL.Path == "/" { - return nil, errors.New("unknown name") - } - - options := resolver.NewResolveOptions(opts...) - - parts := strings.Split(req.URL.Path[1:], "/") - if len(parts) == 1 { - return &resolver.Endpoint{ - Name: r.withPrefix(parts...), - Host: req.Host, - Method: req.Method, - Path: req.URL.Path, - Domain: options.Domain, - }, nil - } - - // /v1/foo - if re.MatchString(parts[0]) { - return &resolver.Endpoint{ - Name: r.withPrefix(parts[0:2]...), - Host: req.Host, - Method: req.Method, - Path: req.URL.Path, - Domain: options.Domain, - }, nil - } - - return &resolver.Endpoint{ - Name: r.withPrefix(parts[0]), - Host: req.Host, - Method: req.Method, - Path: req.URL.Path, - Domain: options.Domain, - }, nil -} - -func (r *Resolver) String() string { - return "path" -} - -// withPrefix transforms "foo" into "go.micro.api.foo" -func (r *Resolver) withPrefix(parts ...string) string { - p := r.opts.ServicePrefix - if len(p) > 0 { - parts = append([]string{p}, parts...) - } - - return strings.Join(parts, ".") -} diff --git a/api/router/options.go b/api/router/options.go index f5993419..d8955897 100644 --- a/api/router/options.go +++ b/api/router/options.go @@ -2,7 +2,7 @@ package router import ( "github.com/micro/go-micro/v3/api/resolver" - "github.com/micro/go-micro/v3/api/resolver/vpath" + "github.com/micro/go-micro/v3/api/resolver/path" "github.com/micro/go-micro/v3/registry" "github.com/micro/go-micro/v3/registry/mdns" ) @@ -26,7 +26,7 @@ func NewOptions(opts ...Option) Options { } if options.Resolver == nil { - options.Resolver = vpath.NewResolver( + options.Resolver = path.NewResolver( resolver.WithHandler(options.Handler), ) }