Skip to content

Commit

Permalink
api/resolver: add resolve options (micro#1756)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Co-authored-by: Dominic Wong <[email protected]>
  • Loading branch information
ben-toogood and domwong authored Jun 29, 2020
1 parent 132c1e3 commit df3e536
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 36 deletions.
7 changes: 5 additions & 2 deletions api/resolver/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ type Resolver struct {
opts resolver.Options
}

func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
// parse options
options := resolver.NewResolveOptions(opts...)

// /foo.Bar/Service
if req.URL.Path == "/" {
return nil, errors.New("unknown name")
Expand All @@ -28,7 +31,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
Domain: r.opts.Domain,
Domain: options.Domain,
}, nil
}

Expand Down
7 changes: 5 additions & 2 deletions api/resolver/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ type Resolver struct {
opts resolver.Options
}

func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
// parse options
options := resolver.NewResolveOptions(opts...)

return &resolver.Endpoint{
Name: req.Host,
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
Domain: r.opts.Domain,
Domain: options.Domain,
}, nil
}

Expand Down
32 changes: 24 additions & 8 deletions api/resolver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

type Options struct {
Handler string
Domain string
ServicePrefix string
}

Expand All @@ -19,13 +18,6 @@ func WithHandler(h string) Option {
}
}

// WithDomain sets the namespace option
func WithDomain(n string) Option {
return func(o *Options) {
o.Domain = n
}
}

// WithServicePrefix sets the ServicePrefix option
func WithServicePrefix(p string) Option {
return func(o *Options) {
Expand All @@ -39,6 +31,30 @@ func NewOptions(opts ...Option) Options {
for _, o := range opts {
o(&options)
}
return options
}

// ResolveOptions are used when resolving a request
type ResolveOptions struct {
Domain string
}

// ResolveOption sets an option
type ResolveOption func(*ResolveOptions)

// Domain sets the resolve Domain option
func Domain(n string) ResolveOption {
return func(o *ResolveOptions) {
o.Domain = n
}
}

// NewResolveOptions returns new initialised resolve options
func NewResolveOptions(opts ...ResolveOption) ResolveOptions {
var options ResolveOptions
for _, o := range opts {
o(&options)
}
if len(options.Domain) == 0 {
options.Domain = registry.DefaultDomain
}
Expand Down
7 changes: 5 additions & 2 deletions api/resolver/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ type Resolver struct {
opts resolver.Options
}

func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
func (r *Resolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
// parse options
options := resolver.NewResolveOptions(opts...)

if req.URL.Path == "/" {
return nil, resolver.ErrNotFound
}
Expand All @@ -24,7 +27,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
Domain: r.opts.Domain,
Domain: options.Domain,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion api/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (

// Resolver resolves requests to endpoints
type Resolver interface {
Resolve(r *http.Request) (*Endpoint, error)
Resolve(r *http.Request, opts ...ResolveOption) (*Endpoint, error)
String() string
}

Expand Down
24 changes: 9 additions & 15 deletions api/resolver/subdomain/subdomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions api/resolver/vpath/vpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ var (
re = regexp.MustCompile("^v[0-9]+$")
)

func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
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: r.opts.Domain,
Domain: options.Domain,
}, nil
}

Expand All @@ -45,7 +47,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
Domain: r.opts.Domain,
Domain: options.Domain,
}, nil
}

Expand All @@ -54,7 +56,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
Domain: r.opts.Domain,
Domain: options.Domain,
}, nil
}

Expand Down
7 changes: 7 additions & 0 deletions registry/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ func (e *etcdRegistry) registerNode(s *registry.Service, node *registry.Node, op
options.Domain = defaultDomain
}

// set the domain in metadata so it can be retrieved by wildcard queries
if s.Metadata == nil {
s.Metadata = map[string]string{"domain": options.Domain}
} else {
s.Metadata["domain"] = options.Domain
}

e.Lock()
// ensure the leases and registers are setup for this domain
if _, ok := e.leases[options.Domain]; !ok {
Expand Down
1 change: 1 addition & 0 deletions router/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ func (r *router) Close() error {

// remove event chan
r.eventChan = nil
r.running = false

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion router/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (t *table) List() ([]Route, error) {
func isMatch(route Route, address, gateway, network, router string, strategy Strategy) bool {
// matches the values provided
match := func(a, b string) bool {
if a == "*" || a == b {
if a == "*" || b == "*" || a == b {
return true
}
return false
Expand Down
8 changes: 7 additions & 1 deletion web/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,16 @@ func (s *service) register() error {

var regErr error

// register options
rOpts := []registry.RegisterOption{
registry.RegisterTTL(s.opts.RegisterTTL),
registry.RegisterDomain(s.opts.Service.Server().Options().Namespace),
}

// try three times if necessary
for i := 0; i < 3; i++ {
// attempt to register
if err := r.Register(s.srv, registry.RegisterTTL(s.opts.RegisterTTL)); err != nil {
if err := r.Register(s.srv, rOpts...); err != nil {
// set the error
regErr = err
// backoff then retry
Expand Down

0 comments on commit df3e536

Please sign in to comment.