-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.go
61 lines (52 loc) · 1.15 KB
/
internal.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
package servers
import (
"net/http"
"time"
"github.com/bweston92/healthz/healthz"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
var LogInternalRequest bool = false
type (
internalRouter struct {
hz *healthz.Healthz
metrics http.Handler
}
)
func (h *internalRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if LogInternalRequest {
logrus.
WithField("request_uri", r.RequestURI).
WithField("request_method", r.Method).
Info("internal endpoint hit")
}
switch r.URL.Path {
case "/healthz":
h.hz.ServeHTTP(w, r)
case "/metrics":
h.metrics.ServeHTTP(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}
}
func (s *Server) runInternalHTTP() <-chan error {
errC := make(chan error)
h := &http.Server{
Addr: s.addr,
Handler: &internalRouter{
hz: s.healthz,
metrics: promhttp.Handler(),
},
ReadTimeout: 30 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 300 * time.Second,
}
go func() {
err := h.ListenAndServe()
if err != nil {
errC <- err
}
}()
return (<-chan error)(errC)
}