-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
296 lines (249 loc) · 7.33 KB
/
server.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
// Copyright (C) 2018. See AUTHORS.
package api
import (
"context"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"encoding/json"
"fmt"
"image/png"
"io"
"net/http"
"time"
"github.com/vivint/rothko/api/query"
"github.com/vivint/rothko/data"
"github.com/vivint/rothko/data/load"
"github.com/vivint/rothko/database"
"github.com/vivint/rothko/dist/tdigest"
"github.com/vivint/rothko/draw/colors"
"github.com/vivint/rothko/draw/graph"
"github.com/vivint/rothko/external"
"github.com/vivint/rothko/merge"
"github.com/zeebo/errs"
)
// Options for the server.
type Options struct {
// Origin is sent back in Access-Control-Allow-Origin. If not set, sends
// back '*'.
Origin string
// Username and Password control basic auth to the server. If unset, no
// basic auth will be required.
Username string
Password string
}
// Server is an http.Handler that can serve responses for a frontend.
type Server struct {
db database.DB
static http.Handler
opts Options
username_hash [sha256.Size]byte
password_hash [sha256.Size]byte
nonce string
}
// New returns a new Server.
func New(db database.DB, static http.Handler, opts Options) *Server {
nonce := make([]byte, 16)
rand.Read(nonce)
if opts.Origin == "" {
opts.Origin = "*"
}
return &Server{
db: db,
static: static,
opts: opts,
username_hash: sha256.Sum256([]byte(opts.Username)),
password_hash: sha256.Sum256([]byte(opts.Password)),
nonce: hex.EncodeToString(nonce),
}
}
// ServeHTTP implements the http.Handler interface for the server. It just
// looks at the method and last path component to route.
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
now := time.Now()
w.Header().Set("Access-Control-Allow-Origin", s.opts.Origin)
tracker := &respTracker{ResponseWriter: w, wrote: false, code: 0}
if err := s.serveHTTP(req.Context(), tracker, req); err != nil {
if !tracker.wrote {
w.WriteHeader(getStatusCode(err))
fmt.Fprintf(w, "%+v\n", err)
}
}
// squelch requests for the nonce
if req.URL.Path == "/api/nonce" {
return
}
external.Infow("http request",
"status", tracker.code,
"method", req.Method,
"duration", time.Since(now).Round(time.Millisecond),
"path", req.URL.Path,
)
}
// validAuth returns true if the auth provided is valid.
func (s *Server) validAuth(ctx context.Context,
username, password string) bool {
username_hash := sha256.Sum256([]byte(username))
username_good := subtle.ConstantTimeCompare(
username_hash[:], s.username_hash[:])
password_hash := sha256.Sum256([]byte(password))
password_good := subtle.ConstantTimeCompare(
password_hash[:], s.password_hash[:])
return username_good&password_good == 1
}
// serveHTTP is a little wrapper for making error handling easier.
func (s *Server) serveHTTP(ctx context.Context, w http.ResponseWriter,
req *http.Request) (err error) {
if s.opts.Username != "" {
username, password, ok := req.BasicAuth()
if !ok || !s.validAuth(ctx, username, password) {
w.Header().Set("WWW-Authenticate", `Basic realm="rothko"`)
return errUnauthorized.New("")
}
}
if req.Method != "GET" {
return errMethodNotAllowed.New("%s", req.Method)
}
switch req.URL.Path {
case "/api/render":
return s.serveRender(ctx, w, req)
case "/api/query":
return s.serveQuery(ctx, w, req)
case "/api/nonce":
return s.serveNonce(ctx, w, req)
default:
if s.static != nil {
s.static.ServeHTTP(w, req)
return nil
}
return errNotFound.New("path: %q", req.URL.Path)
}
}
// serveRender serves either a png of the graph, or a json encoded set of
// columns, and the earliest data so that the frontend can draw the graph.
func (s *Server) serveRender(ctx context.Context, w http.ResponseWriter,
req *http.Request) (err error) {
// get the render parameters
metric := req.FormValue("metric")
if metric == "" {
return errBadRequest.New("metric required")
}
width := getInt(req.FormValue("width"), 1000)
height := getInt(req.FormValue("height"), 350)
padding := getInt(req.FormValue("padding"), 0)
now := getInt64(req.FormValue("now"), time.Now().UnixNano())
dur := getDuration(req.FormValue("duration"), 24*time.Hour)
samples := getInt(req.FormValue("samples"), 30)
compression := getFloat64(req.FormValue("compression"), 5)
stop_before := now - dur.Nanoseconds()
// set up some state for the query
var measured graph.Measured
var earliest []byte
measure_opts := graph.MeasureOptions{
Now: now,
Duration: dur,
Width: width,
Height: height,
Padding: padding,
}
merger := merge.NewMerger(merge.MergerOptions{
Samples: samples,
Now: now,
Duration: dur,
Params: tdigest.Params{Compression: compression},
})
var ok bool
// run the query
err = s.db.Query(ctx, metric, now, nil,
func(ctx context.Context, start, end int64, buf []byte) (
bool, error) {
// get the record ready
var rec data.Record
if err := rec.Unmarshal(buf); err != nil {
return false, errs.Wrap(err)
}
// if we don't have an earliest yet, keep it around and set it up
if measure_opts.Earliest == nil {
dist, err := load.Load(ctx, rec)
if err != nil {
return false, errs.Wrap(err)
}
earliest = append(earliest[:0], buf...)
measure_opts.Earliest = dist
measured, ok = graph.Measure(ctx, measure_opts)
if !ok {
return false, nil
}
merger.SetWidth(measured.Width)
}
// push in the record
if err := merger.Push(ctx, rec); err != nil {
return false, errs.Wrap(err)
}
// keep going until we need to stop based on the duration
return end >= stop_before, nil
})
if err != nil {
return errs.Wrap(err)
}
if !ok {
return errs.New("too small")
}
// grab the columns
cols, err := merger.Finish(ctx)
if err != nil {
return errs.Wrap(err)
}
// if it's json, encode it out
if req.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
type D = map[string]interface{}
return errs.Wrap(json.NewEncoder(w).Encode(D{
"metric": metric,
"columns": cols,
"earliest": earliest,
"now": now,
"duration": dur.Nanoseconds(),
"width": width,
"height": height,
"padding": padding,
}))
}
// if we never got an earliest, we need to measure without it to get the
// axes ready.
if measure_opts.Earliest == nil {
measured, ok = graph.Measure(ctx, measure_opts)
if !ok {
return errs.New("too small")
}
}
// draw the graph
out := measured.Draw(ctx, graph.DrawOptions{
Canvas: nil,
Columns: cols,
Colors: colors.Viridis,
})
// encode it out as a png
w.Header().Set("Content-Type", "image/png")
return errs.Wrap(png.Encode(w, out.AsImage()))
}
// serveQuery returns a set of metrics that match the query as a json list.
func (s *Server) serveQuery(ctx context.Context, w http.ResponseWriter,
req *http.Request) (err error) {
// get the render parameters
_query := req.FormValue("query")
results := getInt(req.FormValue("results"), 10)
search := query.New(_query, results)
if err := s.db.Metrics(ctx, search.Add); err != nil {
return errs.Wrap(err)
}
w.Header().Set("Content-Type", "application/json")
return errs.Wrap(json.NewEncoder(w).Encode(search.Matched()))
}
// serveNonce returns a nonce associated to the server instance.
func (s *Server) serveNonce(ctx context.Context, w http.ResponseWriter,
req *http.Request) (err error) {
io.WriteString(w, s.nonce)
return nil
}