-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathhttp_api.go
434 lines (368 loc) · 9.15 KB
/
http_api.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
427
428
429
430
431
432
433
434
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/couchbase/gomemcached"
"github.com/couchbase/gomemcached/client"
"github.com/couchbaselabs/cbfs/config"
)
func doGetConfig(w http.ResponseWriter, req *http.Request) {
err := updateConfig()
if err != nil && !gomemcached.IsNotFound(err) {
log.Printf("Error updating config: %v", err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
e := json.NewEncoder(w)
err = e.Encode(&globalConfig)
if err != nil {
log.Printf("Error sending config: %v", err)
}
}
func putConfig(w http.ResponseWriter, req *http.Request) {
d := json.NewDecoder(req.Body)
conf := cbfsconfig.CBFSConfig{}
err := d.Decode(&conf)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error reading config: %v", err)
return
}
err = StoreConfig(conf)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error writing config: %v", err)
return
}
err = updateConfig()
if err != nil {
log.Printf("Error fetching newly stored config: %v", err)
}
w.WriteHeader(204)
}
func doBlobInfo(w http.ResponseWriter, req *http.Request) {
if err := req.ParseForm(); err != nil {
http.Error(w, err.Error(), 400)
return
}
blobs, err := getBlobs(req.Form["blob"])
if err != nil {
http.Error(w, err.Error(), 500)
return
}
res := map[string]interface{}{}
for k, v := range blobs {
res[k] = struct {
Nodes map[string]time.Time `json:"nodes"`
}{v.Nodes}
}
sendJson(w, req, res)
}
func doList(w http.ResponseWriter, req *http.Request) {
if canGzip(req) {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
w = &geezyWriter{w, gz}
}
w.WriteHeader(200)
explen := getHash().Size() * 2
filepath.Walk(*root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && !strings.HasPrefix(info.Name(), "tmp") &&
len(info.Name()) == explen {
_, e := w.Write([]byte(info.Name() + "\n"))
return e
}
return nil
})
}
func doListTaskInfo(w http.ResponseWriter, req *http.Request) {
res := struct {
Global map[string][]string `json:"global"`
Local map[string][]string `json:"local"`
}{make(map[string][]string), make(map[string][]string)}
for k, v := range globalPeriodicJobRecipes {
res.Global[k] = v.excl
}
for k, v := range localPeriodicJobRecipes {
res.Local[k] = v.excl
}
sendJson(w, req, res)
}
func doListTasks(w http.ResponseWriter, req *http.Request) {
tasks, err := listRunningTasks()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error listing tasks: %v", err)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
// Reformat for more APIish output.
output := map[string]map[string]TaskState{}
for _, tl := range tasks {
// Remove node prefix from local task names.
npre := tl.Node + "/"
for k, v := range tl.Tasks {
if strings.HasPrefix(k, npre) {
delete(tl.Tasks, k)
tl.Tasks[k[len(npre):]] = v
}
}
output[tl.Node] = tl.Tasks
}
e := json.NewEncoder(w)
err = e.Encode(output)
if err != nil {
log.Printf("Error encoding running tasks list: %v", err)
}
}
func doFileInfo(w http.ResponseWriter, req *http.Request, fn string) {
fm := fileMeta{}
err := couchbase.Get(shortName(fn), &fm)
switch {
case err == nil:
case gomemcached.IsNotFound(err):
http.Error(w, "not found", 404)
return
default:
http.Error(w, err.Error(), 500)
return
}
sendJson(w, req, map[string]interface{}{
"path": fn,
"meta": fm,
})
}
func doGetMeta(w http.ResponseWriter, req *http.Request, path string) {
got := fileMeta{}
err := couchbase.Get(shortName(path), &got)
if err != nil {
log.Printf("Error getting file %#v: %v", path, err)
http.Error(w, err.Error(), 404)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
if got.Userdata == nil {
w.Write([]byte("{}"))
} else {
w.Write(*got.Userdata)
}
}
func putMeta(w http.ResponseWriter, req *http.Request, path string) {
got := fileMeta{}
casid := uint64(0)
k := shortName(path)
err := couchbase.Gets(k, &got, &casid)
if err != nil {
log.Printf("Error getting file %#v: %v", path, err)
http.Error(w, err.Error(), 404)
return
}
r := json.RawMessage{}
err = json.NewDecoder(req.Body).Decode(&r)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
got.Userdata = &r
b := mustEncode(&got)
err = couchbase.Do(k, func(mc *memcached.Client, vb uint16) error {
req := &gomemcached.MCRequest{
Opcode: gomemcached.SET,
VBucket: vb,
Key: []byte(path),
Cas: casid,
Opaque: 0,
Extras: []byte{0, 0, 0, 0, 0, 0, 0, 0},
Body: b}
_, err := mc.Send(req)
return err
})
if err == nil {
w.WriteHeader(201)
} else {
http.Error(w, err.Error(), 500)
}
}
func doListNodes(w http.ResponseWriter, req *http.Request) {
nl, err := findAllNodes()
if err != nil {
log.Printf("Error executing nodes view: %v", err)
w.WriteHeader(500)
fmt.Fprintf(w, "Error generating node list: %v", err)
return
}
respob := map[string]map[string]interface{}{}
for _, node := range nl {
age := time.Since(node.Time)
respob[node.name] = map[string]interface{}{
"size": node.storageSize,
"addr": node.Address(),
"starttime": node.Started,
"hbtime": node.Time,
"hbage_ms": age.Nanoseconds() / 1e6,
"hbage_str": age.String(),
"used": node.Used,
"free": node.Free,
"addr_raw": node.Addr,
"bindaddr": node.BindAddr,
"framesbind": node.FrameBind,
"version": node.Version,
}
// Grandfathering these in.
if !node.Started.IsZero() {
uptime := time.Since(node.Started)
respob[node.name]["uptime_ms"] = uptime.Nanoseconds() / 1e6
respob[node.name]["uptime_str"] = uptime.String()
}
}
sendJson(w, req, respob)
}
func doGetFramesData(w http.ResponseWriter, req *http.Request) {
sendJson(w, req, getFramesInfos())
}
func proxyViewRequest(w http.ResponseWriter, req *http.Request,
path string) {
nodes := couchbase.Nodes()
node := nodes[rand.Intn(len(nodes))]
u, err := url.Parse(node.CouchAPIBase)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
u.Path = "/" + path
u.RawQuery = req.URL.RawQuery
client := &http.Client{
Transport: TimeoutTransport(*viewTimeout),
}
res, err := client.Get(u.String())
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
defer res.Body.Close()
for k, vs := range res.Header {
w.Header()[k] = vs
}
output := io.Writer(w)
if canGzip(req) {
w.Header().Set("Content-Encoding", "gzip")
w.Header().Del("Content-Length")
gz := gzip.NewWriter(w)
defer gz.Close()
output = gz
}
w.WriteHeader(res.StatusCode)
io.Copy(output, res.Body)
}
func proxyCRUDGet(w http.ResponseWriter, req *http.Request,
path string) {
val, err := couchbase.GetRaw(shortName(path))
if err != nil {
w.WriteHeader(404)
fmt.Fprintf(w, "Error getting value: %v", err)
return
}
w.WriteHeader(200)
w.Write(val)
}
func proxyCRUDPut(w http.ResponseWriter, req *http.Request,
path string) {
data, err := ioutil.ReadAll(req.Body)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error reading data: %v", err)
return
}
err = couchbase.SetRaw(shortName(path), 0, data)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error storing value: %v", err)
return
}
w.WriteHeader(204)
}
func proxyCRUDDelete(w http.ResponseWriter, req *http.Request,
path string) {
err := couchbase.Delete(shortName(path))
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error deleting value: %v", err)
return
}
w.WriteHeader(204)
}
func doListDocs(w http.ResponseWriter, req *http.Request,
path string) {
// trim off trailing slash early so we handle them consistently
if strings.HasSuffix(path, "/") {
path = path[0 : len(path)-1]
}
includeMeta := req.FormValue("includeMeta")
depthString := req.FormValue("depth")
depth := 1
if depthString != "" {
i, err := strconv.Atoi(depthString)
if err != nil {
w.WriteHeader(400)
fmt.Fprintf(w, "Error processing depth parameter: %v", err)
return
}
depth = i
}
fl, err := listFiles(path, includeMeta == "true", depth)
if err != nil {
log.Printf("Error executing file browse view: %v", err)
w.WriteHeader(500)
fmt.Fprintf(w, "Error generating file list: %v", err)
return
}
if len(fl.Dirs) == 0 && len(fl.Files) == 0 {
w.WriteHeader(404)
return
}
if canGzip(req) {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
w = &geezyWriter{w, gz}
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
e := json.NewEncoder(w)
err = e.Encode(fl)
if err != nil {
log.Printf("Error writing json stream: %v", err)
}
}
func doPing(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(204)
}
func doInduceTask(w http.ResponseWriter, req *http.Request, taskName string) {
err := induceTask(taskName)
switch err {
case noSuchTask:
http.Error(w, fmt.Sprintf("No such task: %q", taskName), 404)
case taskAlreadyQueued, nil:
w.WriteHeader(202)
default:
http.Error(w, err.Error(), 500)
}
}