-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
173 lines (155 loc) · 3.9 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"gopcep/controller"
"gopcep/grpcapi"
"gopcep/pcep"
"gopcep/restapi"
"os"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
bolt "go.etcd.io/bbolt"
)
type logCfg struct {
FullTimestamp bool
DisableColors bool
TimestampFormat string
TextFormat bool
LogLevel uint32
}
type cfg struct {
grpcapi grpcapi.Config
restapi restapi.Config
pcep pcep.Cfg
logCfg logCfg
bgpls controller.BGPGlobalCfg
}
func appCfg(cfgPath string) *cfg {
viper.SetConfigName("gopcep")
viper.AddConfigPath(cfgPath)
viper.SetConfigType("toml")
err := viper.ReadInConfig()
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "config",
"event": "cfg read error",
"cfg_path": cfgPath,
}).Fatal(err)
}
return &cfg{
pcep: pcep.Cfg{
ListenAddr: viper.GetString("pcep.listen_addr"),
ListenPort: viper.GetString("pcep.listen_port"),
Keepalive: uint8(viper.GetUint32("pcep.keepalive")),
},
grpcapi: grpcapi.Config{
ListenAddr: viper.GetString("grpcapi.listen_addr"),
ListenPort: viper.GetString("grpcapi.listen_port"),
Tokens: viper.GetStringSlice("grpcapi.tokens"),
},
bgpls: controller.BGPGlobalCfg{
AS: uint32(viper.GetUint32("bgpls.as")),
RouterId: viper.GetString("bgpls.router_id"),
},
restapi: restapi.Config{
Address: viper.GetString("restapi.listen_addr"),
Port: viper.GetString("restapi.listen_port"),
CertFile: viper.GetString("restapi.cert_file"),
KeyFile: viper.GetString("restapi.key_file"),
User: viper.GetString("restapi.user"),
Pass: viper.GetString("restapi.pass"),
Debug: viper.GetBool("restapi.debug"),
},
logCfg: logCfg{
LogLevel: viper.GetUint32("log.level"),
TimestampFormat: viper.GetString("log.time_format"),
TextFormat: viper.GetBool("log.text_format"),
FullTimestamp: viper.GetBool("log.full_timestamp"),
DisableColors: viper.GetBool("log.disable_colors"),
},
}
}
func configureLogging(cfg logCfg) {
logrus.SetLevel(logrus.Level(cfg.LogLevel))
if cfg.TextFormat {
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: cfg.DisableColors,
FullTimestamp: cfg.FullTimestamp,
TimestampFormat: cfg.TimestampFormat,
})
return
}
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: cfg.TimestampFormat,
})
}
func startController(c *cli.Context) error {
cfg := appCfg(c.String("config"))
configureLogging(cfg.logCfg)
logrus.WithFields(logrus.Fields{
"topic": "config",
"event": "red config",
"config": cfg,
}).Info("running with config")
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "db",
"event": "failed to open db",
}).Fatal(err)
}
defer func() {
err = db.Close()
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "db",
"event": "failed to close db",
}).Error(err)
}
}()
controller := controller.Start(db, &cfg.bgpls)
err = grpcapi.Start(&cfg.grpcapi, controller)
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "grpcapi",
"event": "start error",
}).Fatal(err)
}
err = restapi.Start(&cfg.restapi, controller)
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "restapi",
"event": "start error",
}).Fatal(err)
}
err = pcep.ListenForNewSession(controller, &cfg.pcep)
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "pcep",
"event": "ListenForNewSession error",
}).Fatal(err)
}
return nil
}
func main() {
app := &cli.App{
Name: "GoPCEP",
Usage: "Segment Routing Traffic Engineering Controller written in Go",
Version: "0.1",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config, c",
Value: ".",
Usage: "config path",
},
},
Action: startController,
}
err := app.Run(os.Args)
if err != nil {
logrus.WithFields(logrus.Fields{
"topic": "run",
"event": "failed to start",
}).Fatal(err)
}
}