2019-11-16 02:55:11 -08:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
"github.com/micro/go-micro/v3/registry"
|
2019-11-16 02:55:11 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func serviceToRecord(s *registry.Service, ttl time.Duration) *record {
|
2020-02-24 17:15:20 +03:00
|
|
|
metadata := make(map[string]string, len(s.Metadata))
|
2019-11-16 02:55:11 -08:00
|
|
|
for k, v := range s.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:15:20 +03:00
|
|
|
nodes := make(map[string]*node, len(s.Nodes))
|
2019-11-16 02:55:11 -08:00
|
|
|
for _, n := range s.Nodes {
|
|
|
|
nodes[n.Id] = &node{
|
|
|
|
Node: n,
|
|
|
|
TTL: ttl,
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints := make([]*registry.Endpoint, len(s.Endpoints))
|
|
|
|
for i, e := range s.Endpoints {
|
|
|
|
endpoints[i] = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return &record{
|
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: metadata,
|
|
|
|
Nodes: nodes,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 12:39:19 +01:00
|
|
|
func recordToService(r *record, domain string) *registry.Service {
|
2020-02-24 17:15:20 +03:00
|
|
|
metadata := make(map[string]string, len(r.Metadata))
|
2019-11-16 02:55:11 -08:00
|
|
|
for k, v := range r.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2020-06-18 12:39:19 +01:00
|
|
|
// set the domain in metadata so it can be determined when a wildcard query is performed
|
|
|
|
metadata["domain"] = domain
|
|
|
|
|
2019-11-16 02:55:11 -08:00
|
|
|
endpoints := make([]*registry.Endpoint, len(r.Endpoints))
|
|
|
|
for i, e := range r.Endpoints {
|
|
|
|
request := new(registry.Value)
|
2019-12-03 22:59:44 +03:00
|
|
|
if e.Request != nil {
|
|
|
|
*request = *e.Request
|
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
response := new(registry.Value)
|
2019-12-03 22:59:44 +03:00
|
|
|
if e.Response != nil {
|
|
|
|
*response = *e.Response
|
|
|
|
}
|
2019-11-16 02:55:11 -08:00
|
|
|
|
2020-02-24 17:15:20 +03:00
|
|
|
metadata := make(map[string]string, len(e.Metadata))
|
2019-11-16 02:55:11 -08:00
|
|
|
for k, v := range e.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints[i] = ®istry.Endpoint{
|
|
|
|
Name: e.Name,
|
|
|
|
Request: request,
|
|
|
|
Response: response,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := make([]*registry.Node, len(r.Nodes))
|
|
|
|
i := 0
|
|
|
|
for _, n := range r.Nodes {
|
2020-02-24 17:15:20 +03:00
|
|
|
metadata := make(map[string]string, len(n.Metadata))
|
2019-11-16 02:55:11 -08:00
|
|
|
for k, v := range n.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes[i] = ®istry.Node{
|
|
|
|
Id: n.Id,
|
|
|
|
Address: n.Address,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istry.Service{
|
|
|
|
Name: r.Name,
|
|
|
|
Version: r.Version,
|
|
|
|
Metadata: metadata,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
|
|
|
}
|