-
Notifications
You must be signed in to change notification settings - Fork 0
/
maestro.go
192 lines (156 loc) · 3.85 KB
/
maestro.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package maestro
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
type (
Client struct {
baseUrl string
authToken string
id string
client *http.Client
}
ClientConfig struct {
Timeout time.Duration
Transport *http.Transport
}
Registrant struct {
Id string `json:"id"`
Address string `json:"address"`
}
Route string
)
const (
authHeader = "X-Registry-Token"
apiPrefix = "/api/v1"
register Route = "/register"
deregister Route = "/deregister"
ping Route = "/ping"
query Route = "/query?id=%s"
)
func New(baseUrl, token, id string, config *ClientConfig) *Client {
httpClient := &http.Client{Timeout: 3 * time.Second}
c := Client{baseUrl: strings.TrimSuffix(baseUrl, "/"), authToken: token, id: id}
if config != nil {
// set HTTP client timeout
httpClient.Timeout = config.Timeout
// set custom transport
if config.Transport != nil {
httpClient.Transport = config.Transport
}
}
c.client = httpClient
return &c
}
// Register registered the service combined with the given public address
// with the registry
func (c *Client) Register(address string) error {
reg := Registrant{
Id: c.id,
Address: address,
}
regJson, err := json.MarshalIndent(reg, "", " ")
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, c.getUrl(register), bytes.NewBuffer(regJson))
if err != nil {
return err
}
c.addAuthHeader(req)
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("received non-success status code (%d)", resp.StatusCode)
}
return nil
}
// Deregister removes the service from the registry
func (c *Client) Deregister() error {
reg := Registrant{
Id: c.id,
Address: "",
}
regJson, err := json.MarshalIndent(reg, "", " ")
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodDelete, c.getUrl(deregister), bytes.NewBuffer(regJson))
if err != nil {
return err
}
c.addAuthHeader(req)
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("received non-success status code (%d)", resp.StatusCode)
}
return nil
}
// StartPing pings the Maestro instance at the supplied interval. Should be started
// as a goroutine. Can be stopped via the context's Cancel function.
func (c *Client) StartPing(ctx context.Context, interval time.Duration) {
t := time.NewTicker(interval)
for {
select {
case <-ctx.Done():
return
case <-t.C:
if err := c.ping(); err != nil {
fmt.Println("ping error: " + err.Error())
}
default:
}
}
}
func (c *Client) ping() error {
req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s?id=%s", c.getUrl(ping), c.id), nil)
if err != nil {
return err
}
c.addAuthHeader(req)
resp, err := c.client.Do(req)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return fmt.Errorf("received non-success status code (%d)", resp.StatusCode)
}
return nil
}
// Query queries Maestro for the info on the specified ID
func (c *Client) Query(id string) (Registrant, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(c.getUrl(query), id), nil)
if err != nil {
return Registrant{}, fmt.Errorf("could not create request: %s", err.Error())
}
c.addAuthHeader(req)
resp, err := c.client.Do(req)
if err != nil {
return Registrant{}, fmt.Errorf("could not execute query request: %s", err.Error())
}
defer resp.Body.Close()
var entry Registrant
err = json.NewDecoder(resp.Body).Decode(&entry)
if err != nil {
return Registrant{}, fmt.Errorf("could not decode JSON: %s", err.Error())
}
return entry, nil
}
func (c *Client) addAuthHeader(r *http.Request) {
r.Header.Add(authHeader, c.authToken)
}
func (c *Client) getUrl(r Route) string {
return c.baseUrl + apiPrefix + string(r)
}