This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (64 loc) · 1.48 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
77
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/spf13/viper"
)
// cancelOnInterrupt calls f when os.Interrupt or SIGTERM is received
func cancelOnInterrupt(ctx context.Context, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(
c,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT,
)
go func() {
select {
case <-ctx.Done():
log.Printf("cancelOnInterrupt() <-ctx.Done(): ctx: %+v c: %+v", ctx, c)
case <-c:
log.Printf("cancelOnInterrupt() <-c: ctx: %+v c: %+v", ctx, c)
cancel()
}
}()
}
func readConfig() (*viper.Viper, error) {
var err error
v := viper.New()
v.AutomaticEnv()
return v, err
}
// run all application
func run() error {
viper, err := readConfig()
if err != nil {
return err
}
statsdConfig, err := NewStatsDConfig(viper)
if err != nil {
return err
}
statsd := NewStatsDServer(statsdConfig)
log.Printf("StatsDServer New(): Configuration set: %+v\n", statsd)
// make the context and control then
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
cancelOnInterrupt(ctx, cancelFunc)
log.Printf("StatsDServer Run(): Start the UDP server: %+v", statsd.Address)
if err := statsd.Run(ctx); err != nil && err != context.Canceled {
return fmt.Errorf("statsd.Run() error: %+v", err)
}
return nil
}
func main() {
log.Printf("Starting go-statsd-zabbix")
if err := run(); err != nil {
log.Fatalf("main() error run(): %+v\n", err)
}
}