new registry util func
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
336868ed0d
commit
34d93306d6
@ -1,7 +1,11 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/unistack-org/micro/v3/registry"
|
||||
"github.com/unistack-org/micro/v3/server"
|
||||
"github.com/unistack-org/micro/v3/util/addr"
|
||||
)
|
||||
|
||||
func addNodes(old, neu []*registry.Node) []*registry.Node {
|
||||
@ -146,3 +150,42 @@ func Remove(old, del []*registry.Service) []*registry.Service {
|
||||
|
||||
return services
|
||||
}
|
||||
|
||||
func NewService(s server.Server) (*registry.Service, error) {
|
||||
opts := s.Options()
|
||||
|
||||
advt := opts.Address
|
||||
if len(opts.Advertise) > 0 {
|
||||
advt = opts.Advertise
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(advt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addr, err := addr.Extract(host)
|
||||
if err != nil {
|
||||
addr = host
|
||||
}
|
||||
|
||||
node := ®istry.Node{
|
||||
Id: opts.Name + "-" + opts.Id,
|
||||
Address: net.JoinHostPort(addr, port),
|
||||
Metadata: opts.Metadata,
|
||||
}
|
||||
|
||||
if node.Metadata == nil {
|
||||
node.Metadata = make(map[string]string, 3)
|
||||
}
|
||||
|
||||
node.Metadata["server"] = s.String()
|
||||
node.Metadata["broker"] = opts.Broker.String()
|
||||
node.Metadata["registry"] = opts.Registry.String()
|
||||
|
||||
return ®istry.Service{
|
||||
Name: opts.Name,
|
||||
Version: opts.Version,
|
||||
Nodes: []*registry.Node{node},
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user