-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.go
95 lines (89 loc) · 2.67 KB
/
routes.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
package channelz
import (
"io"
"net/http"
"path"
"strconv"
"github.com/go-chi/chi/v5"
log "google.golang.org/grpc/grpclog"
)
type channelzHandler interface {
WriteTopChannelsPage(io.Writer)
WriteChannelsPage(io.Writer, int64)
WriteChannelPage(io.Writer, int64)
WriteSubchannelPage(io.Writer, int64)
WriteServerPage(io.Writer, int64)
WriteSocketPage(io.Writer, int64)
}
var pathPrefix string
func createRouter(prefix string, handler channelzHandler) *chi.Mux {
pathPrefix = prefix
router := chi.NewRouter()
router.Route(prefix, func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
handler.WriteTopChannelsPage(w)
})
r.Get("/channel/{channel}", func(w http.ResponseWriter, r *http.Request) {
channelStr := chi.URLParam(r, "channel")
channel, err := strconv.ParseInt(channelStr, 10, 0)
if err != nil {
log.Errorf("channelz: Unable to parse int for channel ID. %s", channelStr)
return
}
handler.WriteChannelPage(w, channel)
})
r.Get("/channels", func(w http.ResponseWriter, r *http.Request) {
startStr := r.URL.Query().Get("start")
start, err := strconv.ParseInt(startStr, 10, 0)
if err != nil {
log.Errorf("channelz: Unable to parse int for start channel ID. %s", startStr)
return
}
handler.WriteChannelsPage(w, start)
})
r.Get("/subchannel/{channel}", func(w http.ResponseWriter, r *http.Request) {
channelStr := chi.URLParam(r, "channel")
channel, err := strconv.ParseInt(channelStr, 10, 0)
if err != nil {
log.Errorf("channelz: Unable to parse int for sub-channel ID. %s", channelStr)
return
}
handler.WriteSubchannelPage(w, channel)
})
r.Get("/server/{server}", func(w http.ResponseWriter, r *http.Request) {
serverStr := chi.URLParam(r, "server")
server, err := strconv.ParseInt(serverStr, 10, 0)
if err != nil {
log.Errorf("channelz: Unable to parse int for server ID. %s", serverStr)
return
}
handler.WriteServerPage(w, server)
})
r.Get("/socket/{socket}", func(w http.ResponseWriter, r *http.Request) {
socketStr := chi.URLParam(r, "socket")
socket, err := strconv.ParseInt(socketStr, 10, 0)
if err != nil {
log.Errorf("channelz: Unable to parse int for socket ID. %s", socketStr)
return
}
handler.WriteSocketPage(w, socket)
})
})
return router
}
func createHyperlink(parts ...interface{}) string {
asStrings := []string{"/" + pathPrefix}
for _, p := range parts {
switch t := p.(type) {
case string:
asStrings = append(asStrings, t)
case int:
s := strconv.Itoa(t)
asStrings = append(asStrings, s)
case int64:
s := strconv.FormatInt(t, 10)
asStrings = append(asStrings, s)
}
}
return path.Join(asStrings...)
}