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
/
Copy pathrouter.go
93 lines (75 loc) · 1.72 KB
/
router.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
package main
import (
"errors"
"fmt"
"io"
"net/http"
"net/http/pprof"
"strconv"
"strings"
)
type Router struct {
Store ArchiveStore
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
fmt.Println(req.Method, req.URL.String())
handler := r.getHandler(req)
if handler != nil {
handler.ServeHTTP(w, req)
}
fmt.Println(req.Method, req.URL.String(), "DONE")
}
func (r *Router) getHandler(req *http.Request) http.Handler {
url := req.URL.String()
switch {
case strings.HasPrefix(url, "/debug/pprof/cmdline"):
return http.HandlerFunc(pprof.Cmdline)
case strings.HasPrefix(url, "/debug/pprof/profile"):
return http.HandlerFunc(pprof.Profile)
case strings.HasPrefix(url, "/debug/pprof/"):
return http.HandlerFunc(pprof.Index)
case strings.HasPrefix(url, "/debug/pprof/symbol"):
return http.HandlerFunc(pprof.Symbol)
}
switch req.Method {
case "GET":
return &GetHandler{Store: r.Store}
case "POST":
return &PostHandler{Store: r.Store}
case "PATCH":
return &PatchHandler{Store: r.Store}
case "DELETE":
return &DeleteHandler{Store: r.Store}
}
return nil
}
func firstPart(req *http.Request) (io.ReadCloser, error) {
mr, err := req.MultipartReader()
if err != nil {
return nil, err
}
for {
part, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
continue
}
return part, nil
}
return nil, nil
}
func getRevParams(req *http.Request) (int, int, error) {
fromRev, err := strconv.Atoi(req.URL.Query().Get("rev"))
if err != nil {
return 0, 0, err
}
toRev, err := strconv.Atoi(req.Header.Get("X-Rev"))
if err != nil {
return 0, 0, err
}
if fromRev >= toRev {
return fromRev, toRev, errors.New("rev not newer")
}
return fromRev, toRev, nil
}