diff --git a/api/resolver/subdomain/subdomain.go b/api/resolver/subdomain/subdomain.go deleted file mode 100644 index c2c381a0..00000000 --- a/api/resolver/subdomain/subdomain.go +++ /dev/null @@ -1,85 +0,0 @@ -// Package subdomain is a resolver which uses the subdomain to determine the domain to route to. It -// offloads the endpoint resolution to a child resolver which is provided in New. -package subdomain - -import ( - "net" - "net/http" - "strings" - - "github.com/micro/go-micro/v3/api/resolver" - "github.com/micro/go-micro/v3/logger" - "golang.org/x/net/publicsuffix" -) - -func NewResolver(parent resolver.Resolver, opts ...resolver.Option) resolver.Resolver { - options := resolver.NewOptions(opts...) - return &Resolver{options, parent} -} - -type Resolver struct { - opts resolver.Options - resolver.Resolver -} - -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)) - } - - return r.Resolver.Resolve(req, opts...) -} - -func (r *Resolver) Domain(req *http.Request) string { - // determine the host, e.g. foobar.m3o.app - host := req.URL.Hostname() - if len(host) == 0 { - if h, _, err := net.SplitHostPort(req.Host); err == nil { - host = h // host does contain a port - } else if strings.Contains(err.Error(), "missing port in address") { - host = req.Host // host does not contain a port - } - } - - // check for an ip address - if net.ParseIP(host) != nil { - return "" - } - - // check for dev enviroment - if host == "localhost" || host == "127.0.0.1" { - 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 "" - } - - // there was no subdomain - if host == domain { - return "" - } - - // remove the domain from the host, leaving the subdomain, e.g. "staging.foo.myapp.com" => "staging.foo" - subdomain := strings.TrimSuffix(host, "."+domain) - - // ignore the API subdomain - if subdomain == "api" { - return "" - } - - // return the reversed subdomain as the namespace, e.g. "staging.foo" => "foo-staging" - comps := strings.Split(subdomain, ".") - for i := len(comps)/2 - 1; i >= 0; i-- { - opp := len(comps) - 1 - i - comps[i], comps[opp] = comps[opp], comps[i] - } - return strings.Join(comps, "-") -} - -func (r *Resolver) String() string { - return "subdomain" -} diff --git a/api/resolver/subdomain/subdomain_test.go b/api/resolver/subdomain/subdomain_test.go deleted file mode 100644 index 2a33fa60..00000000 --- a/api/resolver/subdomain/subdomain_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package subdomain - -import ( - "net/http" - "net/url" - "testing" - - "github.com/micro/go-micro/v3/api/resolver/vpath" - - "github.com/stretchr/testify/assert" -) - -func TestResolve(t *testing.T) { - tt := []struct { - Name string - Host string - Result string - }{ - { - Name: "Top level domain", - Host: "micro.mu", - Result: "micro", - }, - { - Name: "Effective top level domain", - Host: "micro.com.au", - Result: "micro", - }, - { - Name: "Subdomain dev", - Host: "dev.micro.mu", - Result: "dev", - }, - { - Name: "Subdomain foo", - Host: "foo.micro.mu", - Result: "foo", - }, - { - Name: "Multi-level subdomain", - Host: "staging.myapp.m3o.app", - Result: "myapp-staging", - }, - { - Name: "Dev host", - Host: "127.0.0.1", - Result: "micro", - }, - { - Name: "Localhost", - Host: "localhost", - Result: "micro", - }, - { - Name: "IP host", - Host: "81.151.101.146", - Result: "micro", - }, - } - - for _, tc := range tt { - t.Run(tc.Name, func(t *testing.T) { - r := NewResolver(vpath.NewResolver()) - result, err := r.Resolve(&http.Request{URL: &url.URL{Host: tc.Host, Path: "foo/bar"}}) - assert.Nil(t, err, "Expecter err to be nil") - if result != nil { - assert.Equal(t, tc.Result, result.Domain, "Expected %v but got %v", tc.Result, result.Domain) - } - }) - } -}