-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
48 lines (39 loc) · 1.07 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
)
func main() {
http.Handle("/upload-chunk", handleUploadChunk())
http.Handle("/completed-chunks", handleCompletedChunk())
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleUploadChunk() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := ProcessChunk(r); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("chunk processed"))
})
}
func handleCompletedChunk() http.Handler {
type request struct {
UploadID string `json:"uploadId"`
Filename string `json:"filename"`
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var payload request
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// validate payload
if err := CompleteChunk(payload.UploadID, payload.Filename); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("file processed"))
})
}