-
Notifications
You must be signed in to change notification settings - Fork 107
/
main.go
180 lines (171 loc) · 4.78 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
174
175
176
177
178
179
180
//go:generate protoc -I=. -I=$GOPATH/src --gofast_out=. ./server/protocol/internal.proto
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/nats-io/nats.go"
"github.com/urfave/cli"
"github.com/liftbridge-io/liftbridge/server"
)
func main() {
app := cli.NewApp()
app.Name = "liftbridge"
app.Usage = "Lightweight, fault-tolerant message streams"
app.Version = server.Version
app.Flags = getFlags()
app.Action = start
if err := app.Run(os.Args); err != nil {
panic(err)
}
}
func start(c *cli.Context) error {
// Read config from file if present.
config, err := server.NewConfig(c.String("config"))
if err != nil {
return err
}
if err := overrideFromFlags(c, config); err != nil {
return err
}
server := server.New(config)
if err := server.Start(); err != nil {
return err
}
runtime.Goexit()
return nil
}
func overrideFromFlags(c *cli.Context, config *server.Config) error {
// Override with flags.
if c.IsSet("id") {
config.Clustering.ServerID = c.String("id")
}
if c.IsSet("namespace") {
config.Clustering.Namespace = c.String("namespace")
}
if c.IsSet("port") {
config.Port = c.Int("port")
}
if c.IsSet("level") {
level, err := server.GetLogLevel(c.String("level"))
if err != nil {
return err
}
config.LogLevel = level
}
if c.IsSet("raft-bootstrap-seed") {
config.Clustering.RaftBootstrapSeed = c.Bool("raft-bootstrap-seed")
}
if c.IsSet("raft-bootstrap-peers") {
config.Clustering.RaftBootstrapPeers = c.StringSlice("raft-bootstrap-peers")
}
if c.IsSet("data-dir") {
config.DataDir = c.String("data-dir")
}
if c.IsSet("tls-cert") {
config.TLSCert = c.String("tls-cert")
}
if c.IsSet("tls-key") {
config.TLSKey = c.String("tls-key")
}
if c.IsSet("nats-servers") {
natsServers, err := normalizeNatsServers(c.StringSlice("nats-servers"))
if err != nil {
return err
}
config.NATS.Servers = natsServers
}
if c.IsSet("embedded-nats") {
config.EmbeddedNATS = true
}
if c.IsSet("embedded-nats-config") {
config.EmbeddedNATS = true
config.EmbeddedNATSConfig = c.String("embedded-nats-config")
}
return nil
}
func getFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "load configuration from `FILE`",
},
cli.StringFlag{
Name: "server-id, id",
Usage: "ID of the server in the cluster if there is no stored ID (default: random ID)",
},
cli.StringFlag{
Name: "namespace, ns",
Usage: "cluster namespace",
Value: server.DefaultNamespace,
},
cli.StringSliceFlag{
Name: "nats-servers, n",
Usage: fmt.Sprintf("connect to NATS cluster at `ADDR[,ADDR]` (default: %q)", nats.DefaultURL),
// NOTE: cannot use Value here as urfave/cli has another bug
// where it does not replace this value with the specified values but appends them:-(
// Value: &cli.StringSlice{nats.DefaultURL},
},
cli.BoolFlag{
Name: "embedded-nats, e",
Usage: "run a NATS server embedded in this process",
},
cli.StringFlag{
Name: "embedded-nats-config, nc",
Usage: "load configuration for embedded NATS server from `FILE`",
},
cli.StringFlag{
Name: "data-dir, d",
Usage: "store data in `DIR` (default: \"/tmp/liftbridge/<namespace>\")",
},
cli.IntFlag{
Name: "port, p",
Usage: "port to bind to",
Value: server.DefaultPort,
},
cli.StringFlag{
Name: "tls-cert",
Usage: "server certificate file",
},
cli.StringFlag{
Name: "tls-key",
Usage: "private key for server certificate",
},
cli.StringFlag{
Name: "level, l",
Usage: "logging level [debug|info|warn|error]",
Value: "info",
},
cli.BoolFlag{
Name: "raft-bootstrap-seed",
Usage: "bootstrap the Raft cluster by electing self as leader if there is no existing state",
},
cli.StringSliceFlag{
Name: "raft-bootstrap-peers",
Usage: "bootstrap the Raft cluster with the provided list of peer IDs if there is no existing state",
},
}
}
func normalizeNatsServers(natsServers []string) ([]string, error) {
if natsServers != nil {
// urlfave.cli has issues with *Slice flags - it doesn't yet parse
// command-line entries the same way as env vars, see
// https://github.com/urfave/cli/pull/605
// It has been around since Mar 2017 so don't hold your breath for a fix!
// ... so we are manually splitting here for now.
// We also need to handle possible multiple --nats-servers on the cli as this is supported.
allNatsServers := make([]string, 0)
for _, natsServersString := range natsServers {
currNatsServers := strings.Split(natsServersString, ",")
for i := range currNatsServers {
if trimmedNatsServer := strings.TrimSpace(currNatsServers[i]); trimmedNatsServer != "" {
// TODO: validate the server URL and return error?
allNatsServers = append(allNatsServers, trimmedNatsServer)
}
}
}
return allNatsServers, nil
}
return nil, nil
}