-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
95 lines (84 loc) · 1.82 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"github.com/briandowns/sky-island/config"
"github.com/briandowns/sky-island/handlers"
"github.com/briandowns/sky-island/jail"
"github.com/briandowns/sky-island/log"
"github.com/briandowns/sky-island/utils"
"github.com/codegangsta/negroni"
"github.com/thoas/stats"
"gopkg.in/alexcesaro/statsd.v2"
)
var (
configFlag string
initFlag bool
)
var signalsChan = make(chan os.Signal, 1)
func main() {
signal.Notify(signalsChan, os.Interrupt)
go func() {
for range signalsChan {
signalsChan = nil
os.Exit(1)
}
}()
if os.Getgid() != 0 {
fmt.Println("must be run with super user permissions")
os.Exit(1)
}
flag.StringVar(&configFlag, "c", "", "sky-island configuration file")
flag.BoolVar(&initFlag, "i", false, "initialize system")
flag.Parse()
if configFlag == "" {
flag.Usage()
os.Exit(1)
}
conf, err := config.Load(configFlag)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
logger, err := log.Logger(conf, "sky-island")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
statsd.Address(conf.Jails.MonitoringAddr)
metrics, err := statsd.New()
if err != nil {
logger.Log("error", err.Error())
}
defer metrics.Close()
if initFlag {
jsvc := jail.NewJailService(conf, logger, metrics, utils.Wrap{})
if err := jsvc.InitializeSystem(); err != nil {
logger.Log("error", err.Error())
os.Exit(0)
}
os.Exit(0)
}
logger.Log("msg", "starting API...")
params := handlers.Params{
Logger: logger,
Conf: conf,
StatsMW: stats.New(),
Metrics: metrics,
}
router, err := handlers.AddHandlers(¶ms)
if err != nil {
logger.Log("error", err.Error())
os.Exit(0)
}
n := negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
)
n.Use(params.StatsMW)
n.UseHandler(router)
n.Run(":" + strconv.Itoa(conf.HTTPPort))
}