-
Notifications
You must be signed in to change notification settings - Fork 6
/
gothic.go
59 lines (54 loc) · 1.13 KB
/
gothic.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
package gothic
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/jrapoport/gothic/config"
"github.com/jrapoport/gothic/core"
"github.com/jrapoport/gothic/hosts"
"github.com/jrapoport/gothic/utils"
)
// Main is the application main
func Main(c *config.Config) error {
c.Log().Debug(utils.PrettyString(c))
signalsToCatch := []os.Signal{
os.Interrupt,
os.Kill,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGABRT,
syscall.SIGTERM,
syscall.SIGQUIT,
}
name := fmt.Sprintf("%s (%s)", c.Name, utils.ExecutableName())
stopCh := make(chan os.Signal, 1)
signal.Notify(stopCh, signalsToCatch...)
a, err := core.NewAPI(c)
if err != nil {
return err
}
defer func() {
if err = a.Shutdown(); err != nil {
c.Log().Error(err)
return
}
c.Log().Infof("%s shut down", name)
}()
c.Log().Infof("starting %s...", name)
err = hosts.Start(a, c)
if err != nil {
return err
}
defer func() {
if err = hosts.Shutdown(); err != nil {
c.Log().Error(err)
return
}
c.Log().Infof("%s shut down", name)
}()
c.Log().Infof("%s %s started", name, c.Version())
<-stopCh
c.Log().Infof("%s shutting down", name)
return nil
}