/
routes.go
100 lines (87 loc) · 2.75 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
96
97
98
99
100
package httpserver
import (
"net/http"
)
// DefaultRoute is the type for the default route names.
type DefaultRoute string
const (
// IndexRoute is the identifier to enable the index handler.
IndexRoute DefaultRoute = "index"
indexPath string = "/"
// IPRoute is the identifier to enable the ip handler.
IPRoute DefaultRoute = "ip"
ipHandlerPath string = "/ip"
// MetricsRoute is the identifier to enable the metrics handler.
MetricsRoute DefaultRoute = "metrics"
metricsHandlerPath string = "/metrics"
// PingRoute is the identifier to enable the ping handler.
PingRoute DefaultRoute = "ping"
pingHandlerPath string = "/ping"
// PprofRoute is the identifier to enable the pprof handler.
PprofRoute DefaultRoute = "pprof"
pprofHandlerPath string = "/pprof/*option"
// StatusRoute is the identifier to enable the status handler.
StatusRoute DefaultRoute = "status"
statusHandlerPath string = "/status"
)
func allDefaultRoutes() []DefaultRoute {
return []DefaultRoute{
IndexRoute,
IPRoute,
MetricsRoute,
PingRoute,
PprofRoute,
StatusRoute,
}
}
func newDefaultRoutes(cfg *config) []Route {
routes := make([]Route, 0, len(cfg.defaultEnabledRoutes)+1)
for _, id := range cfg.defaultEnabledRoutes {
_, disableLogger := cfg.disableDefaultRouteLogger[id]
switch id {
case IndexRoute:
// The index route needs to access all the routes bound to the handler.
case IPRoute:
routes = append(routes, Route{
Method: http.MethodGet,
Path: ipHandlerPath,
Handler: cfg.ipHandlerFunc,
DisableLogger: disableLogger,
Description: "Returns the public IP address of this service instance.",
})
case MetricsRoute:
routes = append(routes, Route{
Method: http.MethodGet,
Path: metricsHandlerPath,
Handler: cfg.metricsHandlerFunc,
DisableLogger: disableLogger,
Description: "Returns Prometheus metrics.",
})
case PingRoute:
routes = append(routes, Route{
Method: http.MethodGet,
Path: pingHandlerPath,
Handler: cfg.pingHandlerFunc,
DisableLogger: disableLogger,
Description: "Ping this service.",
})
case PprofRoute:
routes = append(routes, Route{
Method: http.MethodGet,
Path: pprofHandlerPath,
Handler: cfg.pprofHandlerFunc,
DisableLogger: disableLogger,
Description: "Returns pprof data for the selected profile.",
})
case StatusRoute:
routes = append(routes, Route{
Method: http.MethodGet,
Path: statusHandlerPath,
Handler: cfg.statusHandlerFunc,
DisableLogger: disableLogger,
Description: "Check this service health status.",
})
}
}
return routes
}