This repository was archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_handler.go
More file actions
71 lines (57 loc) · 1.29 KB
/
get_handler.go
File metadata and controls
71 lines (57 loc) · 1.29 KB
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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"strings"
)
type GetHandler struct {
Store ArchiveStore
}
func (h *GetHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch {
case strings.HasSuffix(req.URL.Path, ".tar.gz"):
h.tarGz(w, req)
case strings.HasSuffix(req.URL.Path, ".json"):
h.json(w, req)
default:
http.NotFound(w, req)
}
}
func (h *GetHandler) tarGz(w http.ResponseWriter, req *http.Request) {
parts := strings.Split(req.URL.Path, ".tar.gz")
revs, err := h.Store.Revs(parts[0], 1)
if err != nil {
log.Println(err)
http.Error(w, "500", http.StatusInternalServerError)
return
}
if len(revs) == 0 {
http.NotFound(w, req)
return
}
rc, err := h.Store.Get(parts[0], revs[0].Rev)
if err != nil {
http.NotFound(w, req)
return
}
defer rc.Close()
io.Copy(w, rc)
}
func (h *GetHandler) json(w http.ResponseWriter, req *http.Request) {
parts := strings.Split(req.URL.Path, ".json")
revs, err := h.Store.Revs(parts[0], 0)
if err != nil {
log.Println(err)
http.Error(w, "500", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
if err = enc.Encode(revs); err != nil {
log.Println(err)
http.Error(w, "500", http.StatusInternalServerError)
return
}
}