/
http.go
62 lines (55 loc) · 1.5 KB
/
http.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
package monitoring
import (
"context"
"errors"
"net"
"net/http"
"time"
"github.com/O1MaGnUmO1/chainlink-common/pkg/utils"
)
// HTTPServer is the HTTP interface exposed by every monitoring.
// It's used to export metrics to prometheus, to query the node for configurations, etc.
type HTTPServer interface {
Handle(path string, handler http.Handler)
Run(ctx context.Context)
}
func NewHTTPServer(baseCtx context.Context, addr string, log Logger) HTTPServer {
mux := http.NewServeMux()
srv := &http.Server{
Addr: addr,
Handler: mux,
BaseContext: func(_ net.Listener) context.Context {
return baseCtx
},
ReadHeaderTimeout: 60 * time.Second,
}
return &httpServer{srv, mux, log, addr}
}
type httpServer struct {
server *http.Server
mux *http.ServeMux
log Logger
addr string
}
func (h *httpServer) Handle(path string, handler http.Handler) {
h.mux.Handle(path, handler)
}
// Run should be executed as a goroutine
func (h *httpServer) Run(ctx context.Context) {
var subs utils.Subprocesses
defer subs.Wait()
subs.Go(func() {
h.log.Debugw("starting HTTP server")
if err := h.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
h.log.Fatalw("failed to start HTTP server", "address", h.addr, "error", err)
} else {
h.log.Infow("HTTP server stopped")
}
})
subs.Go(func() {
<-ctx.Done()
if err := h.server.Shutdown(ctx); err != nil && !errors.Is(err, context.Canceled) {
h.log.Errorw("failed to shut HTTP server down", "error", err)
}
})
}