-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrvlist.go
124 lines (111 loc) · 2.83 KB
/
srvlist.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package rproxy
import (
"bufio"
"errors"
"io"
"strings"
)
// DefaultProtocol will be used for Services for which no protocol has been specified
// (ServiceInfo.Protocol == "")
var DefaultProtocol = "http"
// ServiceList is map of services with
// key:name value:ServiceInfo
// name key is equal to ServiceInfo.Name
type ServiceList map[string]ServiceInfo
type ServiceInfo struct {
Name string
Addr string
Protocol string
}
// URI builds uri string for given service on it's protocol and address
// Example:
// s := ServiceInfo{Addr:"localhost:8080", Protocol:"http"}
// s.URI() == "http://localhost:8080"
func (si *ServiceInfo) URI() string {
if si.Protocol == "" {
si.Protocol = DefaultProtocol
}
return si.Protocol + "://" + si.Addr
}
// NewServiceList returns empty non-nil ServiceList map
func NewServiceList() ServiceList {
sl := make(ServiceList)
return sl
}
// NewServiceListFromReader creates new ServiceList and populates it with
// services data from reader
// Wrap over ServiceList.Parse(io.Reader)
// r format:
// [service_name1] [addr1]
// [service_name2] [addr2] ...
// Example:
// auth_service 192.168.1.2:80
// main_service 192.168.1.3:80
func NewServiceListFromReader(r io.Reader) (ServiceList, error) {
sl := make(ServiceList)
err := sl.Parse(r)
if err != nil {
return nil, err
}
return sl, nil
}
// Parse appends parsed services from r.
// r format (protocol is optional):
// [service_name1] [addr1]
// [service_name2] [addr2] [*protocol]...
//
// Example:
// auth_service 192.168.1.2:80
// main_service 192.168.1.3:80 http
func (sl ServiceList) Parse(r io.Reader) error {
scanner := bufio.NewScanner(r)
if sl == nil {
sl = make(map[string]ServiceInfo)
}
for scanner.Scan() {
rawInfo := strings.Split(
scanner.Text(),
" ",
)
var serviceInfo ServiceInfo
switch len(rawInfo) {
case 3:
serviceInfo.Name = rawInfo[0]
serviceInfo.Addr = rawInfo[1]
serviceInfo.Protocol = rawInfo[2]
case 2:
serviceInfo.Name = rawInfo[0]
serviceInfo.Addr = rawInfo[1]
serviceInfo.Protocol = DefaultProtocol
default:
return errors.New("parsing service list: ragged input. format: \"[service_name] [address] [*protocol]\", protocol is optional")
}
sl[serviceInfo.Name] = serviceInfo
}
if scanner.Err() != nil {
return scanner.Err()
}
return nil
}
// Add appends services to ServiceList
func (sl ServiceList) Add(services ...ServiceInfo) {
if sl == nil {
sl = make(map[string]ServiceInfo, len(services))
}
for _, one := range services {
sl[one.Name] = one
}
}
// Remove deletes services with given names from ServiceList
func (sl ServiceList) Remove(serviceNames ...string) {
for _, name := range serviceNames {
delete(sl, name)
}
}
// Get returns ServiceInfo for given name
func (sl ServiceList) Get(name string) ServiceInfo {
if sl == nil {
return ServiceInfo{}
}
return sl[name]
}