-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_api_server.go
198 lines (175 loc) · 6.1 KB
/
cmd_api_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
package main
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strings"
"sync"
"github.com/gorilla/mux"
"github.com/skx/sos/libconfig"
)
var OPTIONS apiServerCmd
// Start the upload/download servers running.
func apiServer(options apiServerCmd) {
// If we received blob-servers on the command-line use them too.
// NOTE: blob-servers added on the command-line are placed in the
// "default" group.
if options.blob != "" {
servers := strings.Split(options.blob, ",")
for _, entry := range servers {
libconfig.AddServer("default", entry)
}
} else {
libconfig.InitServers()
}
if options.dump {
fmt.Printf("\t% 10s - %s\n", "group", "server")
for _, entry := range libconfig.Servers() {
fmt.Printf("\t% 10s - %s\n", entry.Group, entry.Location)
}
return
}
OPTIONS = options
fmt.Printf("[Launching API-server]\n")
fmt.Printf("\nUpload service\nhttp://%s:%d/upload\n", options.host, options.uport)
fmt.Printf("\nDownload service\nhttp://%s:%d/fetch/:id\n", options.host, options.dport)
fmt.Printf("\nBlob-servers:\n")
fmt.Printf("\t% 10s - %s\n", "group", "server")
for _, entry := range libconfig.Servers() {
fmt.Printf("\t% 10s - %s\n", entry.Group, entry.Location)
}
fmt.Printf("\n")
upRouter := mux.NewRouter()
upRouter.HandleFunc("/upload", APIUploadHandler).Methods("POST")
upRouter.PathPrefix("/").HandlerFunc(APIMissingHandler)
downRouter := mux.NewRouter()
downRouter.HandleFunc("/fetch/{id}", APIDownloadHandler).Methods("GET")
downRouter.HandleFunc("/fetch/{id}", APIDownloadHandler).Methods("HEAD")
downRouter.PathPrefix("/").HandlerFunc(APIMissingHandler)
// The following code is a hack to allow us to run two distinct HTTP-servers on different ports.
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", options.host, options.uport), upRouter))
wg.Done()
}()
wg.Add(1)
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", options.host, options.dport), downRouter))
wg.Done()
}()
wg.Wait()
}
// This is a helper for allowing us to consume a HTTP-body more than once.
type myReader struct {
*bytes.Buffer
}
// So that it implements the io.ReadCloser interface
func (m myReader) Close() error { return nil }
// APIUploadHandler handles uploads to the API server.
//
// This should attempt to upload against the blob-servers and return when that is complete. If
// there is a failure then it should repeat the process until all known servers are exhausted.
//
// The retry logic is described in the file `SCALING.md` in the repository, but in brief there are
// two cases:
// * All the servers are in the group `default`.
// * There are N defined groups.
//
// Both cases are handled by the call to OrderedServers() which returns the known blob-servers in a
// suitable order to minimize lookups. See `SCALING.md` for more details.
func APIUploadHandler(res http.ResponseWriter, req *http.Request) {
buf, _ := ioutil.ReadAll(req.Body)
// Create a copy of the buffer, so that we can consume it initially to hash the data.
rdr1 := myReader{bytes.NewBuffer(buf)}
hasher := sha1.New()
b, _ := ioutil.ReadAll(rdr1)
hasher.Write([]byte(b))
hash := hasher.Sum(nil)
// We try each blob-server in turn, and if/when we receive a successful result we'll return it to
// the caller.
for _, s := range libconfig.OrderedServers() {
rdr2 := myReader{bytes.NewBuffer(buf)}
req.Body = rdr2
// This is where we'll POST to.
url := fmt.Sprintf("%s%s%x", s.Location, "/blob/", hash)
child, _ := http.NewRequest("POST", url, req.Body)
client := &http.Client{}
r, err := client.Do(child)
if err != nil {
continue
}
response, _ := ioutil.ReadAll(r.Body)
if response != nil {
fmt.Fprintf(res, string(response))
return
}
}
res.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(res, "{\"error\":\"upload failed\"}")
return
}
// APIDownloadHandler handles downloads from the API server.
//
// This should attempt to download against the blob-servers and return when that is complete. If
// there is a failure then it should repeat the process until all known servers are exhausted..
//
// The retry logic is described in the file `SCALING.md` in the repository, but in brief there are
// two cases:
// * All the servers are in the group `default`.
// * There are N defined groups.
//
// Both cases are handled by the call to OrderedServers() which returns the known blob-servers in a
// suitable order to minimize lookups. See `SCALING.md` for more details.
func APIDownloadHandler(res http.ResponseWriter, req *http.Request) {
// The ID of the file we're to retrieve.
vars := mux.Vars(req)
id := vars["id"]
// Strip any extension which might be present on the ID.
extension := filepath.Ext(id)
id = id[0 : len(id)-len(extension)]
for _, s := range libconfig.OrderedServers() {
if OPTIONS.verbose {
fmt.Printf("Attempting retrieval from %s%s%s\n", s.Location, "/blob/", id)
}
response, err := http.Get(fmt.Sprintf("%s%s%s", s.Location, "/blob/", id))
if err != nil || response.StatusCode != 200 {
if err != nil && OPTIONS.verbose {
fmt.Printf("\tError fetching: %s\n", err.Error())
} else {
// If there was no error then the HTTP-connection to the back-end succeeded, but that didn't
// return a 200 OK. This might happen if a file was uploaded to only one host, but we've hit
// another.
if OPTIONS.verbose {
fmt.Printf("\tStatus Code : %d\n", response.StatusCode)
}
}
} else {
body, _ := ioutil.ReadAll(response.Body)
if body != nil {
if OPTIONS.verbose {
fmt.Printf("\tFound, read %d bytes\n", len(body))
}
if req.Method == "HEAD" {
res.Header().Set("Connection", "close")
res.WriteHeader(http.StatusOK)
return
}
io.Copy(res, bytes.NewReader(body))
return
}
}
}
res.Header().Set("Connection", "close")
res.WriteHeader(http.StatusNotFound)
}
// APIMissingHandler is a fall-back handler for all requests which are neither upload nor download.
func APIMissingHandler(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusNotFound)
fmt.Fprintf(res, "Invalid method or location.")
}