forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
426 lines (360 loc) · 12.7 KB
/
handler.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package influxdb
import (
"encoding/json"
"net/http"
"strings"
"github.com/bmizerany/pat"
"github.com/influxdb/influxdb/influxql"
)
// TODO: Standard response headers (see: HeaderHandler)
// TODO: Compression (see: CompressionHeaderHandler)
// TODO: Check HTTP response codes: 400, 401, 403, 409.
// Handler represents an HTTP handler for the InfluxDB server.
type Handler struct {
server *Server
mux *pat.PatternServeMux
// The InfluxDB verion returned by the HTTP response header.
Version string
}
// NewHandler returns a new instance of Handler.
func NewHandler(s *Server) *Handler {
h := &Handler{
server: s,
mux: pat.New(),
}
// Authentication route
h.mux.Get("/authenticate", http.HandlerFunc(h.serveAuthenticate))
// User routes.
h.mux.Get("/users", http.HandlerFunc(h.serveUsers))
h.mux.Post("/users", http.HandlerFunc(h.serveCreateUser))
h.mux.Put("/users/:user", http.HandlerFunc(h.serveUpdateUser))
h.mux.Del("/users/:user", http.HandlerFunc(h.serveDeleteUser))
// Database routes
h.mux.Get("/db", http.HandlerFunc(h.serveDatabases))
h.mux.Post("/db", http.HandlerFunc(h.serveCreateDatabase))
h.mux.Del("/db/:name", http.HandlerFunc(h.serveDeleteDatabase))
// Series routes.
h.mux.Get("/db/:db/series", http.HandlerFunc(h.serveQuery))
h.mux.Post("/db/:db/series", http.HandlerFunc(h.serveWriteSeries))
// Shard routes.
h.mux.Get("/db/:db/shards", http.HandlerFunc(h.serveShards))
h.mux.Del("/db/:db/shards/:id", http.HandlerFunc(h.serveDeleteShard))
// Retention policy routes.
h.mux.Get("/db/:db/retention_policies", http.HandlerFunc(h.serveRetentionPolicies))
h.mux.Post("/db/:db/retention_policies", http.HandlerFunc(h.serveCreateRetentionPolicy))
h.mux.Put("/db/:db/retention_policies/:name", http.HandlerFunc(h.serveUpdateRetentionPolicy))
h.mux.Del("/db/:db/retention_policies/:name", http.HandlerFunc(h.serveDeleteRetentionPolicy))
// Utilities
h.mux.Get("/ping", http.HandlerFunc(h.servePing))
// Cluster config endpoints
h.mux.Get("/cluster/servers", http.HandlerFunc(h.serveServers))
h.mux.Del("/cluster/servers/:id", http.HandlerFunc(h.serveDeleteServer))
return h
}
// ServeHTTP responds to HTTP request to the handler.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Max-Age", "2592000")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.Header().Add("X-Influxdb-Version", h.Version)
// If this is a CORS OPTIONS request then send back okie-dokie.
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
// Otherwise handle it via pat.
h.mux.ServeHTTP(w, r)
}
// serveQuery parses an incoming query and returns the results.
func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request) {
// TODO: Authentication.
// Parse query from query string.
urlQry := r.URL.Query()
_, err := influxql.NewParser(strings.NewReader(urlQry.Get("q"))).ParseQuery()
if err != nil {
h.error(w, "parse error: "+err.Error(), http.StatusBadRequest)
return
}
// Retrieve database from server.
/*
db := h.server.Database(urlQry.Get(":db"))
if db == nil {
h.error(w, ErrDatabaseNotFound.Error(), http.StatusNotFound)
return
}
*/
// Parse the time precision from the query params.
/*
precision, err := parseTimePrecision(urlQry.Get("time_precision"))
if err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
*/
// Execute query against the database.
/*
if err := db.ExecuteQuery(q); err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
*/
}
// serveWriteSeries receives incoming series data and writes it to the database.
func (h *Handler) serveWriteSeries(w http.ResponseWriter, r *http.Request) {
// TODO: Authentication.
/* TEMPORARILY REMOVED FOR PROTOBUFS.
// Retrieve database from server.
db := h.server.Database(r.URL.Query().Get(":db"))
if db == nil {
h.error(w, ErrDatabaseNotFound.Error(), http.StatusNotFound)
return
}
// Parse time precision from query parameters.
precision, err := parseTimePrecision(r.URL.Query().Get("time_precision"))
if err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Setup HTTP request reader. Wrap in a gzip reader if encoding set in header.
reader := r.Body
if r.Header.Get("Content-Encoding") == "gzip" {
if reader, err = gzip.NewReader(r.Body); err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
}
// Decode series from reader.
ss := []*serializedSeries{}
dec := json.NewDecoder(reader)
dec.UseNumber()
if err := dec.Decode(&ss); err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Convert the wire format to the internal representation of the time series.
series, err := serializedSeriesSlice(ss).series(precision)
if err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Write series data to the database.
// TODO: Allow multiple series written to DB at once.
for _, s := range series {
if err := db.WriteSeries(s); err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
}
*/
}
// serveDatabases returns a list of all databases on the server.
func (h *Handler) serveDatabases(w http.ResponseWriter, r *http.Request) {
// Retrieve databases from the server.
databases := h.server.Databases()
// JSON encode databases to the response.
w.Header().Add("content-type", "application/json")
_ = json.NewEncoder(w).Encode(databases)
}
// serveCreateDatabase creates a new database on the server.
func (h *Handler) serveCreateDatabase(w http.ResponseWriter, r *http.Request) {
var req struct {
Name string `json:"name"`
}
// Decode the request from the body.
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
} else if req.Name == "" {
h.error(w, ErrDatabaseNameRequired.Error(), http.StatusBadRequest)
return
}
// Create the database.
if err := h.server.CreateDatabase(req.Name); err == ErrDatabaseExists {
h.error(w, err.Error(), http.StatusConflict)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
// serveDeleteDatabase deletes an existing database on the server.
func (h *Handler) serveDeleteDatabase(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
if err := h.server.DeleteDatabase(name); err == ErrDatabaseNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// serveAuthenticate authenticates a user.
func (h *Handler) serveAuthenticate(w http.ResponseWriter, r *http.Request) {}
// serveUsers returns data about a single user.
func (h *Handler) serveUsers(w http.ResponseWriter, r *http.Request) {
// Generate a list of objects for encoding to the API.
a := make([]*userJSON, 0)
for _, u := range h.server.Users() {
a = append(a, &userJSON{
Name: u.Name,
Admin: u.Admin,
})
}
w.Header().Add("content-type", "application/json")
_ = json.NewEncoder(w).Encode(a)
}
type userJSON struct {
Name string `json:"name"`
Password string `json:"password,omitempty"`
Admin bool `json:"admin,omitempty"`
}
// serveCreateUser creates a new user.
func (h *Handler) serveCreateUser(w http.ResponseWriter, r *http.Request) {
// Read in user from request body.
var u userJSON
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Create the user.
if err := h.server.CreateUser(u.Name, u.Password, u.Admin); err == ErrUserExists {
h.error(w, err.Error(), http.StatusConflict)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// serveUpdateUser updates an existing user.
func (h *Handler) serveUpdateUser(w http.ResponseWriter, r *http.Request) {
// Read in user from request body.
var u userJSON
if err := json.NewDecoder(r.Body).Decode(&u); err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Create the user.
if err := h.server.UpdateUser(r.URL.Query().Get(":user"), u.Password); err == ErrUserNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// serveDeleteUser removes an existing user.
func (h *Handler) serveDeleteUser(w http.ResponseWriter, r *http.Request) {
// Delete the user.
if err := h.server.DeleteUser(r.URL.Query().Get(":user")); err == ErrUserNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// servePing returns a simple response to let the client know the server is running.
func (h *Handler) servePing(w http.ResponseWriter, r *http.Request) {}
// serveShards returns a list of shards.
func (h *Handler) serveShards(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
// Retrieves shards for the database.
shards, err := h.server.Shards(q.Get(":db"))
if err == ErrDatabaseNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
// Write data to the response.
w.Header().Add("content-type", "application/json")
_ = json.NewEncoder(w).Encode(shards)
}
// serveDeleteShard removes an existing shard.
func (h *Handler) serveDeleteShard(w http.ResponseWriter, r *http.Request) {}
// serveRetentionPolicies returns a list of retention policys.
func (h *Handler) serveRetentionPolicies(w http.ResponseWriter, r *http.Request) {
// Retrieve policies by database.
policies, err := h.server.RetentionPolicies(r.URL.Query().Get(":db"))
if err == ErrDatabaseNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
// Write data to response body.
w.Header().Add("content-type", "application/json")
_ = json.NewEncoder(w).Encode(policies)
}
// serveCreateRetentionPolicy creates a new retention policy.
func (h *Handler) serveCreateRetentionPolicy(w http.ResponseWriter, r *http.Request) {
// Decode the policy from the body.
var policy RetentionPolicy
if err := json.NewDecoder(r.Body).Decode(&policy); err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Create the retention policy.
if err := h.server.CreateRetentionPolicy(r.URL.Query().Get(":db"), &policy); err == ErrDatabaseNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err == ErrRetentionPolicyExists {
h.error(w, err.Error(), http.StatusConflict)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
// serveUpdateRetentionPolicy updates an existing retention policy.
func (h *Handler) serveUpdateRetentionPolicy(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
db, name := q.Get(":db"), q.Get(":name")
// Decode the new policy values from the body.
var policy RetentionPolicy
if err := json.NewDecoder(r.Body).Decode(&policy); err != nil {
h.error(w, err.Error(), http.StatusBadRequest)
return
}
// Update the retention policy.
if err := h.server.UpdateRetentionPolicy(db, name, &policy); err == ErrDatabaseNotFound || err == ErrRetentionPolicyNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// serveDeleteRetentionPolicy removes an existing retention policy.
func (h *Handler) serveDeleteRetentionPolicy(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
db, name := q.Get(":db"), q.Get(":name")
// Delete the retention policy.
if err := h.server.DeleteRetentionPolicy(db, name); err == ErrDatabaseNotFound || err == ErrRetentionPolicyNotFound {
h.error(w, err.Error(), http.StatusNotFound)
return
} else if err != nil {
h.error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// serveServers returns a list of servers in the cluster.
func (h *Handler) serveServers(w http.ResponseWriter, r *http.Request) {}
// serveDeleteServer removes a server from the cluster.
func (h *Handler) serveDeleteServer(w http.ResponseWriter, r *http.Request) {}
// error returns an error to the client in a standard format.
func (h *Handler) error(w http.ResponseWriter, error string, code int) {
// TODO: Return error as JSON.
http.Error(w, error, code)
}