api/resolver: add resolve options (#1756)

* api/resolver: Resolve options

* router/registry: fix init bug

* router/registry: fix wildcard query bug

* web: fix registation domain bug

* registry/etcd: pass domain in service metadata

* api/resolver/subdomain: expose domain func

* Update api/resolver/subdomain/subdomain.go

Co-authored-by: Dominic Wong <domwongemail@googlemail.com>

Co-authored-by: Dominic Wong <domwongemail@googlemail.com>
This commit is contained in:
ben-toogood
2020-06-29 16:37:45 +01:00
committed by GitHub
parent 132c1e35fe
commit df3e5364ca
11 changed files with 71 additions and 36 deletions

View File

@@ -22,21 +22,15 @@ type Resolver struct {
resolver.Resolver
}
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
// resolve the endpoint using the provided resolver
endpoint, err := r.Resolver.Resolve(req)
if err != nil {
return nil, err
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
if dom := r.Domain(req); len(dom) > 0 {
opts = append(opts, resolver.Domain(dom))
}
// override the domain
endpoint.Domain = r.resolveDomain(req)
// return the result
return endpoint, nil
return r.Resolver.Resolve(req, opts...)
}
func (r *Resolver) resolveDomain(req *http.Request) string {
func (r *Resolver) Domain(req *http.Request) string {
// determine the host, e.g. foobar.m3o.app
host := req.URL.Hostname()
if len(host) == 0 {
@@ -49,24 +43,24 @@ func (r *Resolver) resolveDomain(req *http.Request) string {
// check for an ip address
if net.ParseIP(host) != nil {
return r.opts.Domain
return ""
}
// check for dev enviroment
if host == "localhost" || host == "127.0.0.1" {
return r.opts.Domain
return ""
}
// extract the top level domain plus one (e.g. 'myapp.com')
domain, err := publicsuffix.EffectiveTLDPlusOne(host)
if err != nil {
logger.Debugf("Unable to extract domain from %v", host)
return r.opts.Domain
return ""
}
// there was no subdomain
if host == domain {
return r.opts.Domain
return ""
}
// remove the domain from the host, leaving the subdomain, e.g. "staging.foo.myapp.com" => "staging.foo"