micro/resolver/registry/registry.go

35 lines
817 B
Go
Raw Normal View History

// Package registry resolves names using the micro registry
2019-07-28 12:14:40 +01:00
package registry
import (
"context"
"github.com/unistack-org/micro/v3/registry"
"github.com/unistack-org/micro/v3/resolver"
2019-07-28 12:14:40 +01:00
)
// Resolver is a registry network resolver
2019-07-28 12:14:40 +01:00
type Resolver struct {
// Registry is the registry to use otherwise we use the defaul
Registry registry.Registry
}
// Resolve assumes ID is a domain name e.g micro.mu
2019-07-28 20:00:09 +01:00
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
services, err := r.Registry.GetService(context.TODO(), name)
2019-07-28 12:14:40 +01:00
if err != nil {
return nil, err
}
records := make([]*resolver.Record, 0, len(services))
2019-07-28 12:14:40 +01:00
for _, service := range services {
for _, node := range service.Nodes {
records = append(records, &resolver.Record{
Address: node.Address,
})
}
}
return records, nil
}