-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
72 lines (62 loc) · 1.57 KB
/
server.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
package opendj_exporter
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var commit string
var tag string
func GetVersion() string {
return fmt.Sprintf("%s (%s)", tag, commit)
}
type Server struct {
server *http.Server
logger log.FieldLogger
}
func NewMetricsServer(bindAddr, metricsPath string) *Server {
mux := http.NewServeMux()
mux.Handle(metricsPath, promhttp.Handler())
mux.HandleFunc("/version", showVersion)
var err error
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, err = w.Write([]byte(`<html>
<head><title>OpenDJ Exporter</title></head>
<body>
<h1>OpenDJ Exporter</h1>
<p><a href="` + metricsPath + `">Metrics</a></p>
</body>
</html>`))
if err != nil {
log.Fatalf("Error sending response body: %s", err)
}
})
return &Server{
server: &http.Server{Addr: bindAddr, Handler: mux},
logger: log.WithField("component", "server"),
}
}
func showVersion(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintln(w, GetVersion())
}
func (s *Server) Start() error {
s.logger.WithField("addr", s.server.Addr).Info("starting http listener")
err := s.server.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
}
func (s *Server) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
s.server.Shutdown(ctx)
cancel()
}