forked from couchbaselabs/cbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_api.go
316 lines (270 loc) · 6.67 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
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/couchbaselabs/cbfs/config"
"github.com/dustin/gomemcached"
"github.com/dustin/gomemcached/client"
)
func doGetConfig(w http.ResponseWriter, req *http.Request) {
err := updateConfig()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Error updating config: %v", err)
return
}
w.Header().Set("Content-Type", "application/json")
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 doList(w http.ResponseWriter, req *http.Request) {
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 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")
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 doGetMeta(w http.ResponseWriter, req *http.Request, path string) {
got := fileMeta{}
err := couchbase.Get(path, &got)
if err != nil {
log.Printf("Error getting file %#v: %v", path, err)
w.WriteHeader(404)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
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)
err := couchbase.Gets(path, &got, &casid)
if err != nil {
log.Printf("Error getting file %#v: %v", path, err)
w.WriteHeader(404)
w.Write([]byte(err.Error()))
return
}
r := json.RawMessage{}
err = json.NewDecoder(req.Body).Decode(&r)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
got.Userdata = &r
b := mustEncode(&got)
err = couchbase.Do(path, 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}
resp, err := mc.Send(req)
if err != nil {
return err
}
if resp.Status != gomemcached.SUCCESS {
return resp
}
return nil
})
if err == nil {
w.WriteHeader(201)
} else {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
}
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,
}
// 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()
}
}
w.Header().Set("Content-Type", "application/json")
w.Write(mustEncode(respob))
}
func doGetFramesData(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(mustEncode(getFramesInfos()))
}
func proxyViewRequest(w http.ResponseWriter, req *http.Request,
path string) {
node := couchbase.Nodes[rand.Intn(len(couchbase.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")
gz := gzip.NewWriter(w)
defer gz.Close()
output = gz
}
w.WriteHeader(res.StatusCode)
io.Copy(output, res.Body)
}
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
}
w.Header().Set("Content-Type", "application/json")
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)
}