-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
102 lines (87 loc) · 2.22 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
101
102
package main
import (
"encoding/json"
"flag"
"github.com/crabkun/crab/config"
log "github.com/sirupsen/logrus"
"io/ioutil"
"strings"
)
const VERSION = "beta2"
var (
ConfigFile string
)
func init() {
flag.StringVar(&ConfigFile, "config", "config.json", "config file")
}
func main() {
flag.Parse()
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: false,
FullTimestamp: true,
})
var err error
log.Infoln("Crab", VERSION)
l := log.WithFields(log.Fields{
"file": ConfigFile,
})
cfgBuf, err := ioutil.ReadFile(ConfigFile)
if err != nil {
l.WithError(err).Fatalln("load configure file failed")
}
var baseCfg *config.BaseConfig
err = json.Unmarshal(cfgBuf, &baseCfg)
if err != nil {
l.WithError(err).Fatalln("unmarshal configure file failed")
}
err = baseCfg.Validate()
if err != nil {
l.WithError(err).Fatalln("validate configure file failed")
}
logLvl, err := log.ParseLevel(baseCfg.LogLevel)
if err != nil {
l.WithError(err).WithFields(log.Fields{
"log_level": baseCfg.LogLevel,
}).Fatalln("unsupported log level")
}
log.SetLevel(logLvl)
switch strings.ToLower(baseCfg.Mode) {
case "client":
var clientConfig *config.ClientConfig
err = json.Unmarshal(cfgBuf, &clientConfig)
if err != nil {
l.WithError(err).Fatalln("unmarshal client configure file failed")
}
err = clientConfig.Validate()
if err != nil {
l.WithError(err).Fatalln("validate client configure file failed")
}
ClientMain(clientConfig)
case "server":
var serverConfig *config.ServerConfig
err = json.Unmarshal(cfgBuf, &serverConfig)
if err != nil {
l.WithError(err).Fatalln("unmarshal server configure file failed")
}
err = serverConfig.Validate()
if err != nil {
l.WithError(err).Fatalln("validate server configure file failed")
}
ServerMain(serverConfig)
case "master":
var masterConfig *config.MasterConfig
err = json.Unmarshal(cfgBuf, &masterConfig)
if err != nil {
l.WithError(err).Fatalln("unmarshal master configure file failed")
}
err = masterConfig.Validate()
if err != nil {
l.WithError(err).Fatalln("validate master configure file failed")
}
MasterMain(masterConfig)
default:
l.WithFields(log.Fields{
"mode": baseCfg.Mode,
}).Fatalln("unsupported mode")
}
}