-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
68 lines (53 loc) · 1.43 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
func main() {
reg := prometheus.NewRegistry()
reg.MustRegister(newDockerCollector())
router := http.NewServeMux()
router.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{
Registry: reg,
}))
serverPort := 8080
if strPort, isSet := os.LookupEnv("DEX_PORT"); isSet {
if intPort, err := strconv.Atoi(strPort); err == nil {
serverPort = intPort
}
}
server := &http.Server{
Addr: fmt.Sprintf(":%v", serverPort),
Handler: router,
ReadTimeout: 5 * time.Second,
WriteTimeout: 120 * time.Second,
IdleTimeout: 15 * time.Second,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
log.Info("Server is shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
}()
log.Info("Server is ready to handle requests at :", serverPort)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %d: %v\n", serverPort, err)
}
<-done
log.Info("Server stopped")
}