-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (63 loc) · 2.11 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 (
"time"
"github.com/Festivals-App/festivals-database/server"
"github.com/Festivals-App/festivals-database/server/config"
servertools "github.com/Festivals-App/festivals-server-tools"
"github.com/rs/zerolog/log"
)
func main() {
servertools.InitializeGlobalLogger("/var/log/festivals-database-node/info.log", true)
log.Info().Msg("Server startup.")
conf := config.DefaultConfig()
log.Info().Msg("Server configuration was initialized.")
server := server.NewServer(conf)
go server.Run(conf)
log.Info().Msg("Server did start.")
go sendNodeHeartbeat(conf)
go sendDatabaseHeartbeat(conf)
log.Info().Msg("Heartbeat routines where started.")
// wait forever
// https://stackoverflow.com/questions/36419054/go-projects-main-goroutine-sleep-forever
select {}
}
func sendNodeHeartbeat(conf *config.Config) {
heartbeatClient, err := servertools.HeartbeatClient(conf.TLSCert, conf.TLSKey)
if err != nil {
log.Fatal().Err(err).Str("type", "server").Msg("Failed to create heartbeat client")
}
beat := &servertools.Heartbeat{
Service: "festivals-database-node",
Host: "https://" + conf.ServiceBindHost,
Port: conf.ServicePort,
Available: true,
}
t := time.NewTicker(time.Duration(conf.Interval) * time.Second)
defer t.Stop()
for range t.C {
err = servertools.SendHeartbeat(heartbeatClient, conf.LoversEar, conf.ServiceKey, beat)
if err != nil {
log.Error().Err(err).Msg("Failed to send heartbeat")
}
}
}
func sendDatabaseHeartbeat(conf *config.Config) {
heartbeatClient, err := servertools.HeartbeatClient(conf.TLSCert, conf.TLSKey)
if err != nil {
log.Fatal().Err(err).Str("type", "server").Msg("Failed to create heartbeat client")
}
beat := &servertools.Heartbeat{
Service: "festivals-database",
Host: "mysql://" + conf.DatabaseBindAddress,
Port: conf.DatabasePort,
Available: true,
}
t := time.NewTicker(time.Duration(conf.Interval) * time.Second)
defer t.Stop()
for range t.C {
err = servertools.SendHeartbeat(heartbeatClient, conf.LoversEar, conf.ServiceKey, beat)
if err != nil {
log.Error().Err(err).Msg("Failed to send heartbeat")
}
}
}