add resolver
This commit is contained in:
parent
9a56c4e0b2
commit
b89423bf37
30
network/resolver/dns/dns.go
Normal file
30
network/resolver/dns/dns.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Package dns resolves ids to dns srv records
|
||||||
|
package dns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/network/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Resolver struct{}
|
||||||
|
|
||||||
|
// Resolve assumes ID is a domain name e.g micro.mu
|
||||||
|
func (r *Resolver) Resolve(id string) ([]*resolver.Record, error) {
|
||||||
|
_, addrs, err := net.LookupSRV("network", "udp", id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var records []*resolver.Record
|
||||||
|
for _, addr := range addrs {
|
||||||
|
address := addr.Target
|
||||||
|
if addr.Port > 0 {
|
||||||
|
address = fmt.Sprintf("%s:%d", addr.Target, addr.Port)
|
||||||
|
}
|
||||||
|
records = append(records, &resolver.Record{
|
||||||
|
Address: address,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return records, nil
|
||||||
|
}
|
12
network/resolver/resolver.go
Normal file
12
network/resolver/resolver.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// Package resolver resolves network ids to addresses
|
||||||
|
package resolver
|
||||||
|
|
||||||
|
type Resolver interface {
|
||||||
|
// Resolve returns a list of addresses for an id
|
||||||
|
Resolve(id string) ([]*Record, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A resoved id
|
||||||
|
type Record struct {
|
||||||
|
Address string
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user