-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
306 lines (281 loc) · 7.33 KB
/
client.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Package statushub is a client for the StatusHub API.
//
// StatusHub is a service for consolidating log messages.
// Essentially, you can have all of your long-running
// scripts write to a single StatusHub host so that you
// can easily check on their outputs.
package statushub
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"github.com/gorilla/websocket"
"github.com/unixpickle/essentials"
)
// A LogRecord is one logged message.
type LogRecord struct {
Service string `json:"serviceName"`
Message string `json:"message"`
Time int64 `json:"time"`
ID int `json:"id"`
}
// A MediaRecord is a piece of media stored on the server.
type MediaRecord struct {
Folder string `json:"folder"`
Filename string `json:"filename"`
Mime string `json:"mime"`
Time int64 `json:"time"`
ID int `json:"id"`
}
// A Client interfaces with a StatusHub back-end.
type Client struct {
c *http.Client
rootURL url.URL
}
// NewClient creates a new, unauthenticated client.
//
// The rootURL specifies the base URL of the StatusHub
// server.
// For example, it might be "http://localhost:8080".
func NewClient(rootURL string) (*Client, error) {
j, err := cookiejar.New(nil)
if err != nil {
return nil, essentials.AddCtx("create cookie jar", err)
}
u, err := url.Parse(rootURL)
if err != nil {
return nil, essentials.AddCtx("bad root URL", err)
}
return &Client{
c: &http.Client{
Jar: j,
},
rootURL: *u,
}, nil
}
// Login attempts to authenticate with the server.
func (c *Client) Login(password string) error {
u := c.rootURL
u.Path = "/login"
query := bytes.NewReader([]byte("password=" + url.QueryEscape(password)))
res, err := c.c.Post(u.String(), "application/x-www-form-urlencoded", query)
if res != nil {
res.Body.Close()
}
if err != nil {
return err
}
if res.Request.URL.Path == u.Path {
return errors.New("login failed")
}
return nil
}
// Add adds a log record and returns its ID.
func (c *Client) Add(service, message string) (int, error) {
msg := map[string]string{
"service": service,
"message": message,
}
var resID int
err := c.apiCall("add", msg, &resID)
if err != nil {
err = essentials.AddCtx("add log record", err)
}
return resID, err
}
// AddBatch adds a batch of log records and returns their
// IDs.
func (c *Client) AddBatch(service string, messages []string) ([]int, error) {
msg := map[string]interface{}{
"service": service,
"messages": messages,
}
var resIDs []int
err := c.apiCall("addBatch", msg, &resIDs)
if err != nil {
err = essentials.AddCtx("add log records", err)
}
return resIDs, err
}
// AddMedia adds a media record and returns its ID.
func (c *Client) AddMedia(folder, filename, mime string, data []byte, replace bool) (int, error) {
msg := map[string]interface{}{
"folder": folder,
"filename": filename,
"mime": mime,
"data": data,
"replace": replace,
}
var resID int
err := c.apiCall("addMedia", msg, &resID)
if err != nil {
err = essentials.AddCtx("add media record", err)
}
return resID, err
}
// Overview returns the most recent log message from every
// service.
func (c *Client) Overview() ([]LogRecord, error) {
msg := map[string]string{}
var reply []LogRecord
if err := c.apiCall("overview", msg, &reply); err != nil {
return nil, essentials.AddCtx("fetch overview", err)
}
return reply, nil
}
// ServiceLog returns the log records for a service,
// sorted by most to least recent.
// It returns with an error if the service does not exist.
func (c *Client) ServiceLog(service string) ([]LogRecord, error) {
msg := map[string]string{"service": service}
var reply []LogRecord
if err := c.apiCall("serviceLog", msg, &reply); err != nil {
return nil, essentials.AddCtx("fetch service log", err)
}
return reply, nil
}
// Delete deletes the log for a service.
func (c *Client) Delete(service string) error {
msg := map[string]string{"service": service}
var result bool
err := c.apiCall("delete", msg, &result)
return essentials.AddCtx("delete service log", err)
}
// FullStream creates a channel of live log messages.
// The cancel chan can be closed to tell the stream to
// terminate.
// The returned channels will be closed on error or after
// a graceful shutdown.
func (c *Client) FullStream(cancel <-chan struct{}) (<-chan LogRecord, <-chan error) {
return c.streamCall(cancel, "/api/fullStream", "")
}
// ServiceStream is like FullStream, but it limits
// messages to a specific service.
func (c *Client) ServiceStream(service string, cancel <-chan struct{}) (<-chan LogRecord,
<-chan error) {
escaped := url.QueryEscape(service)
return c.streamCall(cancel, "/api/serviceStream", "service="+escaped)
}
func (c *Client) apiCall(name string, msg, reply interface{}) error {
u := c.rootURL
u.Path = "/api/" + name
query, err := json.Marshal(msg)
if err != nil {
return err
}
res, err := c.c.Post(u.String(), "application/json", bytes.NewReader(query))
if res != nil {
defer res.Body.Close()
}
if err != nil {
return err
}
contents, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
var respObj struct {
Data interface{} `json:"data"`
Error string `json:"error"`
}
respObj.Data = reply
if err := json.Unmarshal(contents, &respObj); err != nil {
return errors.New(err.Error() + ": " + string(contents))
}
if respObj.Error != "" {
return errors.New("remote error: " + respObj.Error)
}
if reply != nil {
dataJSON, _ := json.Marshal(respObj.Data)
if err := json.Unmarshal(dataJSON, reply); err != nil {
return essentials.AddCtx("unmarshal data", err)
}
}
return nil
}
func (c *Client) streamCall(done <-chan struct{}, path, query string) (<-chan LogRecord,
<-chan error) {
resChan := make(chan LogRecord, 1)
errChan := make(chan error, 1)
go func() {
defer close(resChan)
defer close(errChan)
u := c.websocketURL()
u.Path = path
u.RawQuery = query
conn, err := net.Dial("tcp", u.Host)
if err != nil {
errChan <- essentials.AddCtx("stream log", err)
return
}
// Create dummy request for the AddCookie magic.
req, err := http.NewRequest("GET", c.rootURL.String(), nil)
if err != nil {
errChan <- essentials.AddCtx("stream log", err)
return
}
for _, c := range c.c.Jar.Cookies(&c.rootURL) {
req.AddCookie(c)
}
req.Header.Set("Host", hostname(u.Host))
cli, _, err := websocket.NewClient(conn, u, req.Header, 100, 100)
if err != nil {
errChan <- essentials.AddCtx("stream log", err)
return
}
cleanupChan := make(chan struct{})
defer close(cleanupChan)
go func() {
select {
case <-done:
case <-cleanupChan:
}
cli.Close()
}()
for {
var msg LogRecord
err := cli.ReadJSON(&msg)
select {
case <-done:
return
default:
}
if err != nil {
errChan <- essentials.AddCtx("stream log", err)
return
}
select {
case resChan <- msg:
case <-done:
return
}
}
}()
return resChan, errChan
}
func (c *Client) websocketURL() *url.URL {
u := c.rootURL
if m, _ := regexp.MatchString(":[0-9]*$", u.Host); !m {
if u.Scheme == "http" {
u.Host += ":80"
} else if u.Scheme == "https" {
u.Host += ":443"
}
}
if u.Scheme == "http" {
u.Scheme = "ws"
} else if u.Scheme == "https" {
u.Scheme = "wss"
}
return &u
}
func hostname(h string) string {
expr := regexp.MustCompile(":[0-9]*$")
return expr.ReplaceAllString(h, "")
}