/
main.go
97 lines (81 loc) · 2.15 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
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"github.com/AlbinoDrought/creamy-gateway-picker/remote"
"github.com/caarlos0/env"
"github.com/imroc/req"
)
var client remote.Client
var cfg config
func main() {
if err := env.Parse(&cfg); err != nil {
log.Fatalln("error parsing config", err)
}
if len(cfg.GatewayLabels) != len(cfg.GatewayNames) {
log.Println("gateway label and name mismatch, using names as labels")
cfg.GatewayLabels = cfg.GatewayNames
}
if len(cfg.GatewayStatusNames) != len(cfg.GatewayNames) {
log.Println("gateway status name and normal name mismatch, using names as status names")
cfg.GatewayStatusNames = cfg.GatewayNames
}
gateways := make([]gateway, len(cfg.GatewayNames))
for i, gatewayName := range cfg.GatewayNames {
gateways[i].Name = gatewayName
gateways[i].Label = cfg.GatewayLabels[i]
gateways[i].StatusName = cfg.GatewayStatusNames[i]
}
cfg.Gateways = gateways
if cfg.Debug {
req.SetFlags(req.LreqHead | req.LreqBody)
req.Debug = true
}
client = remote.NewSensemillaClient(cfg.RemoteHost, cfg.RemoteUsername, cfg.RemotePassword)
ctx, cancel := context.WithCancel(context.Background())
gracefulWaitGroup := sync.WaitGroup{}
gracefulShutdownComplete := make(chan bool, 1)
go func() {
log.Println("performing client self-check")
_, err := getGatewayStatus()
if err != nil {
log.Fatal("error during list gateways self-check", err)
}
log.Println("client self-check passed!")
}()
serverFinished := bootServer(ctx)
gracefulWaitGroup.Add(1)
go func() {
err := <-serverFinished
if err != nil {
log.Println("server exited with error", err)
}
gracefulWaitGroup.Done()
}()
go func() {
gracefulWaitGroup.Wait()
gracefulShutdownComplete <- true
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
firstInterrupt := true
for {
select {
case <-gracefulShutdownComplete:
log.Println("Graceful shutdown finished, bye!")
return
case <-c:
if firstInterrupt {
firstInterrupt = false
cancel()
log.Println("Interrupt received, initiated graceful shutdown")
} else {
log.Println("Performing unclean shutdown")
return
}
}
}
}