-
Notifications
You must be signed in to change notification settings - Fork 0
/
commander.go
70 lines (59 loc) · 1.45 KB
/
commander.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
package cmd
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/rs/zerolog/log"
"github.com/spaghettifunk/norman/internal/commander"
"github.com/spaghettifunk/norman/pkg/logger"
"github.com/spf13/cobra"
)
// commanderCmd represents the commander command
var commanderCmd = &cobra.Command{
Use: "commander",
Short: "",
Long: ``,
Run: commanderRun,
}
func commanderRun(cmd *cobra.Command, args []string) {
if normanCfg == nil {
panic(fmt.Errorf("configuration has not loaded correctly"))
}
// initialize global logging
logger.InitLogger("commander", *normanCfg)
// initialize service
c, err := commander.New(*normanCfg)
if err != nil {
panic(err.Error())
}
// signal channel to capture system calls
done := make(chan bool, 1)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
// start shutdown goroutine
go func() {
// capture sigterm and other system call here
<-sigCh
// shutdown http server
if err := c.ShutdownServer(); err != nil {
log.Fatal().Err(err)
return
}
log.Info().Msg("Commander Server is down. Bye Bye!")
close(done)
}()
// start http server goroutine
go func() {
addr := fmt.Sprintf("%s:%d", normanCfg.Commander.Address, normanCfg.Commander.Port)
if err := c.StartServer(addr); err != nil {
log.Fatal().Err(err).Msg(err.Error())
close(done)
}
}()
// time to say goodbye
<-done
}
func init() {
rootCmd.AddCommand(commanderCmd)
}