-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.go
331 lines (310 loc) · 7.62 KB
/
views.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
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
"text/template"
)
// read/write file requests that shall only touch
// the current node. No cluster interaction.
func localHandler(w http.ResponseWriter, r *http.Request, s *site) {
secret := r.Header.Get("X-Cask-Cluster-Secret")
if !s.Cluster.CheckSecret(secret) {
log.Println("unauthorized local file request")
http.Error(w, "sorry, need the secret knock", 403)
return
}
parts := strings.Split(r.URL.String(), "/")
if len(parts) == 3 {
if r.Method == "POST" {
handleLocalPost(w, r, s)
return
}
fmt.Fprintf(w, "show form/handle post\n")
return
}
if len(parts) == 4 {
key := parts[2]
log.Printf("%s /local/%s/\n", r.Method, parts[2])
k, err := keyFromString(key)
if err != nil {
http.Error(w, "invalid key\n", 400)
return
}
if inm := r.Header.Get("If-None-Match"); inm != "" {
if inm == "\""+key+"\"" {
w.WriteHeader(http.StatusNotModified)
return
}
}
if !s.Backend.Exists(*k) {
http.Error(w, "not found\n", 404)
return
}
if r.Method == "HEAD" {
w.WriteHeader(200)
return
}
if r.Method == "GET" {
data, err := s.Backend.Read(*k)
if err != nil {
log.Println(err)
http.Error(w, "error reading file", 500)
return
}
w.Header().Set("Content-Type", "application/octet")
w.Header().Set("ETag", "\""+key+"\"")
w.Write(data)
// kick off a background goroutine to do read-repair
go func() {
s.VerifyKey(*k)
s.Rebalance(*k)
}()
}
}
}
func handleLocalPost(w http.ResponseWriter, r *http.Request, s *site) {
log.Println("write a file")
if !s.Node.Writeable {
http.Error(w, "this node is read-only", 503)
return
}
f, _, _ := r.FormFile("file")
defer f.Close()
h := sha1.New()
io.Copy(h, f)
key, err := keyFromString("sha1:" + fmt.Sprintf("%x", h.Sum(nil)))
if err != nil {
http.Error(w, "bad hash", 500)
return
}
if s.Backend.Exists(*key) {
log.Println("already exists, don't need to do anything")
fmt.Fprintf(w, key.String())
return
}
f.Seek(0, 0)
err = s.Backend.Write(*key, f)
if err != nil {
http.Error(w, "could not write file", 500)
return
}
fmt.Fprintf(w, key.String())
return
}
func serveDirect(w http.ResponseWriter, key key, s *site) bool {
if !s.Backend.Exists(key) {
return false
}
data, err := s.Backend.Read(key)
if err != nil {
log.Println(err)
return false
}
w.Header().Set("Content-Type", "application/octet")
w.Write(data)
log.Println("served direct")
// kick off a background goroutine to do read-repair
go func() {
s.VerifyKey(key)
s.Rebalance(key)
}()
return true
}
func fileHandler(w http.ResponseWriter, r *http.Request, s *site) {
parts := strings.Split(r.URL.String(), "/")
if len(parts) == 4 {
key := parts[2]
log.Printf("%s /file/%s/\n", r.Method, parts[2])
k, err := keyFromString(key)
if err != nil {
http.Error(w, "invalid key\n", 400)
return
}
if inm := r.Header.Get("If-None-Match"); inm != "" {
if inm == "\""+key+"\"" {
w.WriteHeader(http.StatusNotModified)
return
}
}
if serveDirect(w, *k, s) {
w.Header().Set("ETag", "\""+key+"\"")
return
}
data, err := s.Cluster.Retrieve(*k)
if err != nil {
http.Error(w, "not found", 404)
return
}
w.Header().Set("ETag", "\""+key+"\"")
w.Write(data)
} else {
http.Error(w, "bad request", 400)
}
}
func indexHandler(w http.ResponseWriter, r *http.Request, s *site) {
if r.Method == "GET" {
clusterInfoHandler(w, r, s)
return
}
if r.Method == "POST" {
postFileHandler(w, r, s)
return
}
http.Error(w, "method not supported", 405)
}
type clusterInfoPage struct {
Title string
Cluster *cluster
Myself *node
Neighbors []node
Site *site
FreeSpace uint64
}
func clusterInfoHandler(w http.ResponseWriter, r *http.Request, s *site) {
p := clusterInfoPage{
Title: "cluster status",
Cluster: s.Cluster,
Myself: s.Node,
Neighbors: s.Cluster.NeighborsInclusive(),
Site: s,
}
t, _ := template.New("cluster").Parse(clusterTemplate)
t.Execute(w, p)
}
var defaultReplication = 3
var minReplication = 1
type postResponse struct {
Key string `json:"key"`
Success bool `json:"success"`
}
func postFileHandler(w http.ResponseWriter, r *http.Request, s *site) {
log.Println("add a file")
f, _, _ := r.FormFile("file")
defer f.Close()
h := sha1.New()
io.Copy(h, f)
key, err := keyFromString("sha1:" + fmt.Sprintf("%x", h.Sum(nil)))
if err != nil {
log.Println(err)
http.Error(w, "bad hash", 500)
return
}
f.Seek(0, 0)
success := s.Cluster.AddFile(*key, f, defaultReplication, minReplication)
pr := postResponse{
Key: key.String(),
Success: success,
}
b, err := json.Marshal(pr)
if err != nil {
http.Error(w, "json error", 500)
return
}
w.Write(b)
}
func joinHandler(w http.ResponseWriter, r *http.Request, s *site) {
if r.Method == "POST" {
if r.FormValue("url") == "" {
fmt.Fprint(w, "no url specified")
return
}
u := r.FormValue("url")
secret := r.FormValue("secret")
if !s.Cluster.CheckSecret(secret) {
log.Println("got an unauthorized join attempt")
log.Println(secret)
http.Error(w, "need to know the secret knock", 403)
return
}
parts := strings.Split(u, ",")
_, err := mlist.Join(parts)
if err != nil {
fmt.Fprint(w, err)
return
}
fmt.Fprintf(w, "Added node")
} else {
// show form
w.Write([]byte(joinTemplate))
}
}
func configHandler(w http.ResponseWriter, r *http.Request, s *site) {
b, err := json.Marshal(s.Node)
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
// just ignore this crap
}
const clusterTemplate = `
<html>
<head>
<title>{{.Title}}</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<h1>Node: {{.Myself.UUID}}</h1>
<table class="table">
<tr><th>Backend</th><td>{{.Site.Backend}}</td></tr>
<tr><th>Free Space</th><td>{{.Site.Backend.FreeSpace}}</td></tr>
<tr><th>Base</th><td>{{.Myself.BaseURL}}</td></tr>
<tr><th>Writeable</th><td>{{.Myself.Writeable}}</td></tr>
<tr><th>Replication</th><td>{{.Site.Replication}}</td></tr>
<tr><th>Max Replication</th><td>{{.Site.MaxReplication}}</td></tr>
<tr><th>Active Anti-Entropy Interval</th><td>{{.Site.AAEInterval}} seconds</td></tr>
</table>
<h2>cluster status</h2>
<table class="table table-condensed table-striped">
<tr>
<th>UUID</th>
<th>Base</th>
<th>Writeable</th>
<th>Last Seen</th>
<th>Last Failed</th>
</tr>
{{range .Neighbors}}
{{if .LastSeen.IsZero}}
{{else}}
<tr {{if .Unhealthy}}class="danger"{{end}}>
<td>{{.UUID}}</td>
<td><a href="{{.BaseURL}}">{{.BaseURL}}</a></td>
<td>{{if .Writeable}}<span class="text-success">yes</span>{{else}}<span class="text-danger">read-only</span>{{end}}</td>
<td>{{.LastSeenFormatted}}</td>
<td>{{if .LastFailed.IsZero}}-{{else}}{{.LastFailedFormatted}}{{end}}</td>
</tr>
{{end}}
{{end}}
</table>
</body>
<ul class="nav nav-pills">
<li role="presentation"><a href="/join/">Add a node manually</a></li>
<li role="presentation"><a href="/config/">JSON config data</a></li>
</ul>
</div>
</html>
`
const joinTemplate = `
<html><head><title>Add Node</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<h1>Add Node</h1>
<form action="." method="post" class="form">
<input type="text" name="url" placeholder="Gossip Address" size="128" class="form-control"/><br />
<input type="text" name="secret" placeholder="cluster secret" class="form-control"/><br />
<input type="submit" value="add node" />
</form>
</div>
</body>
</html>
`