Dynamic Namespace

This commit is contained in:
Ben Toogood
2020-04-09 11:03:33 +01:00
parent 27eb7db1c2
commit 4ff959ef50
5 changed files with 31 additions and 9 deletions

View File

@@ -33,7 +33,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
parts := strings.Split(req.URL.Path[1:], "/")
if len(parts) == 1 {
return &resolver.Endpoint{
Name: addNamespace(r.opts.Namespace, parts...),
Name: r.withNamespace(req, parts...),
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@@ -43,7 +43,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
// /v1/foo
if re.MatchString(parts[0]) {
return &resolver.Endpoint{
Name: addNamespace(r.opts.Namespace, parts[0:2]...),
Name: r.withNamespace(req, parts[0:2]...),
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@@ -51,7 +51,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
}
return &resolver.Endpoint{
Name: addNamespace(r.opts.Namespace, parts[0]),
Name: r.withNamespace(req, parts[0]),
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@@ -62,9 +62,11 @@ func (r *Resolver) String() string {
return "path"
}
func addNamespace(ns string, parts ...string) string {
func (r *Resolver) withNamespace(req *http.Request, parts ...string) string {
ns := r.opts.Namespace(req)
if len(ns) == 0 {
return strings.Join(parts, ".")
}
return strings.Join(append([]string{ns}, parts...), ".")
}