-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
100 lines (93 loc) · 1.79 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
98
99
100
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"syscall"
"github.com/lonord/router-service/app"
"github.com/lonord/router-service/base"
)
func main() {
err := run()
if err != nil {
log.Fatalln(err)
}
}
func run() error {
configFilePath := flag.String("c", "/etc/router-service/config.yml", "config file path")
flag.Parse()
// check
err := CheckDepedence()
if err != nil {
return err
}
// config
cfg, err := ba.ReadConfig(readConfigFile(*configFilePath))
if err != nil {
return err
}
// service ctx
ctx := app.NewMainContext(cfg)
// start
startService(ctx)
// listen signal
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan)
for {
select {
case sig := <-signalChan:
switch sig {
case syscall.SIGHUP:
fallthrough
case syscall.SIGINT:
fallthrough
case syscall.SIGQUIT:
fallthrough
case syscall.SIGTERM:
log.Println("got signal ", sig.String())
stopService(ctx)
return nil
}
}
}
}
func startService(ctx *app.MainContext) {
err := ctx.SubProcess.Dnsmasq.Start()
if err != nil {
log.Println(err)
}
err = ctx.SubProcess.Bridge.SetupBridge()
if err != nil {
log.Println(err)
}
err = ctx.SubProcess.Forward.SetupForward()
if err != nil {
log.Println(err)
}
ctx.WebService.Start()
log.Println("service started")
}
func stopService(ctx *app.MainContext) {
log.Println("service is going to shutdown")
ctx.WebService.Stop()
err := ctx.SubProcess.Forward.ClearForward()
if err != nil {
log.Println(err)
}
err = ctx.SubProcess.Bridge.ClearBridge()
if err != nil {
log.Println(err)
}
err = ctx.SubProcess.Dnsmasq.Stop()
if err != nil {
log.Println(err)
}
log.Println("bye")
}
func readConfigFile(p string) ba.ConfigReaderFn {
return func() ([]byte, error) {
return ioutil.ReadFile(p)
}
}