-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
76 lines (64 loc) · 1.66 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
69
70
71
72
73
74
75
76
package main
import (
"cmp"
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/adamdecaf/deadcheck/internal/api"
"github.com/adamdecaf/deadcheck/internal/check"
"github.com/adamdecaf/deadcheck/internal/config"
"github.com/moov-io/base/log"
)
var (
flagConfig = flag.String("config", "", "Filepath to configuration file")
flagHttpAddr = flag.String("http.addr", ":8080", "HTTP listen address")
flagVersion = flag.Bool("version", false, "Print the version of deadcheck")
)
func main() {
flag.Parse()
if *flagVersion {
fmt.Printf("deadcheck %s", Version) //nolint:forbidigo
return
}
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
logger := log.NewDefaultLogger().With(log.Fields{
"app": log.String("deadcheck"),
"version": log.String(Version),
})
conf, err := config.Load(*flagConfig)
if err != nil {
logger.Error().LogErrorf("reading %s failed: %v", *flagConfig, err)
os.Exit(1)
}
conf.Server.BindAddress = cmp.Or(conf.Server.BindAddress, *flagHttpAddr)
instances, err := check.Setup(ctx, logger, conf)
if err != nil {
logger.Error().LogErrorf("setting up checks failed: %w", err)
os.Exit(1)
}
server, err := api.Server(logger, conf.Server, instances)
if err != nil {
logger.Error().LogErrorf("running HTTP server failed: %w", err)
os.Exit(1)
}
defer func() {
if server != nil {
server.Shutdown(ctx)
}
}()
// Listen for shutdown
errs := make(chan error)
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("signal: %v", <-c)
}()
err = <-errs
if err != nil {
logger.Warn().Logf("shutting down: %v", err)
}
}