-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
78 lines (67 loc) · 1.97 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package service
import (
"strconv"
"strings"
)
type Service struct {
Name string `json:"name,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Endpoints []string `json:"endpoints,omitempty"`
Nodes []*Node `json:"nodes,omitempty"`
}
type (
Nodes []*Node
Node struct {
Id string `json:"id,omitempty"`
Version string `json:"version,omitempty"`
Address string `json:"address,omitempty"`
Port int `json:"port,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
)
func (t Node) GetPort() int {
if t.Port != 0 {
return t.Port
}
adders := strings.Split(t.Address, ":")
if len(adders) > 1 {
port, _ := strconv.Atoi(adders[len(adders)-1])
return port
}
return 0
}
type Endpoint struct {
Name string `json:"name,omitempty"`
Request *Value `json:"request,omitempty"`
Response *Value `json:"response,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
type Value struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Values []*Value `json:"values,omitempty"`
}
func addPort(addr string) int {
adders := strings.Split(addr, ":")
if len(adders) > 1 {
port, _ := strconv.Atoi(adders[len(adders)-1])
return port
}
return 0
}
// Instance is an instance of a service in a discovery system.
type Instance struct {
// ID is the unique instance ID as registered.
ID string `json:"id"`
// Name is the service name as registered.
Name string `json:"name"`
// Version is the version of the compiled.
Version string `json:"version"`
// Metadata is the kv pair metadata associated with the service instance.
Metadata map[string]string `json:"metadata"`
// Endpoints are endpoint addresses of the service instance.
// schema:
// http://127.0.0.1:8000?isSecure=false
// grpc://127.0.0.1:9000?isSecure=false
Endpoints []string `json:"endpoints"`
}