-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
106 lines (94 loc) · 2.33 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"embed"
"fmt"
"heartbeats/pkg/config"
"heartbeats/pkg/heartbeat"
"heartbeats/pkg/history"
"heartbeats/pkg/logger"
"heartbeats/pkg/notify"
"heartbeats/pkg/server"
"os"
"github.com/alecthomas/kingpin/v2"
)
const version = "0.6.13"
//go:embed web
var templates embed.FS
var (
configPath = kingpin.Flag("config", "Path to the configuration file").
Short('c').
Envar("HEARTBEATS_CONFIG").
Default("./deploy/config.yaml").
String()
listenAddress = kingpin.Flag("listen-address", "Address to listen on").
Short('l').
Envar("HEARTBEATS_LISTEN_ADDRESS").
Default("localhost:8080").
String()
siteRoot = kingpin.
Flag("site-root", "Site root for the heartbeat service").
Short('s').
Envar("HEARTBEATS_SITE_ROOT").
Default("http://<listenaddress>").
String()
maxSize = kingpin.Flag("max-size", "Maximum size of the cache").
Short('m').
Envar("HEARTBEATS_MAX_SIZE").
Default("1000").
Int()
reduce = kingpin.Flag("reduce", "Percentage to reduce when max size is exceeded").
Short('r').
Envar("HEARTBEATS_REDUCE").
Default("25").
Int()
verbose = kingpin.Flag("verbose", "Enable verbose logging").
Short('v').
Envar("HEARTBEATS_VERBOSE").
Bool()
)
// run initializes and runs the server with the provided context and verbosity settings.
func run(ctx context.Context, verbose bool) error {
kingpin.CommandLine.Name = "heartbeats"
kingpin.UsageTemplate(kingpin.CompactUsageTemplate)
kingpin.Parse()
log := logger.NewLogger(verbose)
heartbeatsStore := heartbeat.NewStore()
notificationStore := notify.NewStore()
historyStore := history.NewStore()
if err := config.Read(
*configPath,
history.Config{
MaxSize: *maxSize,
Reduce: *reduce,
},
heartbeatsStore,
notificationStore,
historyStore,
); err != nil {
return fmt.Errorf("error reading config file. %v", err)
}
if err := config.Validate(heartbeatsStore, notificationStore); err != nil {
return fmt.Errorf("Error validating config file. %v", err)
}
return server.Run(
ctx,
log,
version,
server.Config{
ListenAddress: *listenAddress,
SiteRoot: *siteRoot,
},
templates,
heartbeatsStore,
notificationStore,
historyStore,
)
}
func main() {
ctx := context.Background()
if err := run(ctx, *verbose); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}