remove vpath resolver
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/micro/go-micro/v3/api/handler"
|
"github.com/micro/go-micro/v3/api/handler"
|
||||||
"github.com/micro/go-micro/v3/api/resolver"
|
"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"
|
"github.com/micro/go-micro/v3/api/router"
|
||||||
regRouter "github.com/micro/go-micro/v3/api/router/registry"
|
regRouter "github.com/micro/go-micro/v3/api/router/registry"
|
||||||
"github.com/micro/go-micro/v3/registry"
|
"github.com/micro/go-micro/v3/registry"
|
||||||
@@ -57,7 +57,7 @@ func testHttp(t *testing.T, path, service, ns string) {
|
|||||||
rt := regRouter.NewRouter(
|
rt := regRouter.NewRouter(
|
||||||
router.WithHandler("http"),
|
router.WithHandler("http"),
|
||||||
router.WithRegistry(r),
|
router.WithRegistry(r),
|
||||||
router.WithResolver(vpath.NewResolver(
|
router.WithResolver(rpath.NewResolver(
|
||||||
resolver.WithServicePrefix(ns),
|
resolver.WithServicePrefix(ns),
|
||||||
)),
|
)),
|
||||||
)
|
)
|
||||||
@@ -92,31 +92,6 @@ func TestHttpHandler(t *testing.T) {
|
|||||||
"go.micro.api.test",
|
"go.micro.api.test",
|
||||||
"go.micro.api",
|
"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 {
|
for _, d := range testData {
|
||||||
|
@@ -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, ".")
|
|
||||||
}
|
|
@@ -2,7 +2,7 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/micro/go-micro/v3/api/resolver"
|
"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"
|
||||||
"github.com/micro/go-micro/v3/registry/mdns"
|
"github.com/micro/go-micro/v3/registry/mdns"
|
||||||
)
|
)
|
||||||
@@ -26,7 +26,7 @@ func NewOptions(opts ...Option) Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if options.Resolver == nil {
|
if options.Resolver == nil {
|
||||||
options.Resolver = vpath.NewResolver(
|
options.Resolver = path.NewResolver(
|
||||||
resolver.WithHandler(options.Handler),
|
resolver.WithHandler(options.Handler),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user