-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (70 loc) · 1.94 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
package main
import (
"flag"
"os"
"syscall"
_config "github.com/immobiliare/peephole/config"
_kiosk "github.com/immobiliare/peephole/kiosk"
_mold "github.com/immobiliare/peephole/mold"
_spy "github.com/immobiliare/peephole/spy"
_util "github.com/immobiliare/peephole/util"
"github.com/sirupsen/logrus"
)
var (
argConfig string
config *_config.Wrapper
mold *_mold.Mold
spy *_spy.Spy
kiosk *_kiosk.Kiosk
)
func init() {
_util.TrapSignal(os.Interrupt, func() { exit() })
flag.StringVar(&argConfig, "c", "/etc/peephole", "Configuration file path")
flag.Parse()
}
func main() {
var err error
logrus.WithField("path", argConfig).Infoln("Reading configuration file")
if config, err = _config.Parse(argConfig); err != nil {
logrus.WithError(err).Fatalln("Unable to get config")
}
if config.Debug && !_util.Debugging() {
os.Setenv(_util.DebugKey, "1")
if err = syscall.Exec(os.Args[0], os.Args, os.Environ()); err != nil {
logrus.WithError(err).Fatalln("Unable to exec proc in debug mode")
}
exit()
}
logrus.Infoln("Initializing mold")
if mold, err = _mold.Init(config.Mold); err != nil {
logrus.WithError(err).Fatalln("Unable to get config")
}
logrus.Infoln("Initializing spy")
if spy, err = _spy.Init(mold, config.Spy); err != nil {
logrus.WithError(err).Errorln("Unable to get config")
exit()
}
logrus.WithField("endpoints", len(spy.Endpoints())).Infoln("Starting spying")
go func() {
if err := spy.Watch(); err != nil {
logrus.WithError(err).Errorln("Unable to spy")
exit()
}
}()
logrus.WithField("bind", config.Kiosk.Bind).Infoln("Starting kiosk server")
kiosk = _kiosk.Init(mold, spy.EventChan, config.Kiosk)
if err := kiosk.Serve(); err != nil {
logrus.WithError(err).Errorln("Unable to serve connections")
exit()
}
}
func exit() {
if mold == nil {
return
}
logrus.Println("Closing DB")
if err := mold.Close(); err != nil {
logrus.WithError(err).Println("Unable to close db")
}
os.Exit(0)
}