-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
115 lines (102 loc) · 2.71 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
package main
import (
"fmt"
"os"
"github.com/urfave/cli"
)
const (
portFlag = "port"
version = "0.0.1"
consulDatacenterFlag = "consul-datacenter"
consulHTTPAddrFlag = "consul-http-addr"
consulACLTokenFlag = "consul-acl-token"
consulSchemeFlag = "consul-scheme"
consulAllowStaleFlag = "consul-stale"
defaultPort = 8080
defaultConsulDatacenter = "dc1"
defaultConsulHTTPAddr = "localhost:8500"
defaultConsulScheme = "http"
)
func main() {
app := cli.NewApp()
configureCli(app)
app.Action = mainAction
app.Run(os.Args)
}
func mainAction(c *cli.Context) error {
conf, err := validateConfig(c)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
server, err := NewServer(conf)
if err != nil {
return cli.NewExitError(err, 1)
}
return server.Start()
}
func configureCli(app *cli.App) {
app.Name = "envoy-consul-sds"
app.Usage = "Envoy Consul Service Discovery Service"
app.Version = version
app.Flags = []cli.Flag{
&cli.IntFlag{
Name: fmt.Sprintf("%s, p", portFlag),
Value: defaultPort,
Usage: "The `port` to start the webserver on",
},
&cli.StringFlag{
Name: consulDatacenterFlag,
Value: defaultConsulDatacenter,
Usage: "The `datacenter` for consul",
},
&cli.StringFlag{
Name: consulHTTPAddrFlag,
Value: defaultConsulHTTPAddr,
Usage: "The `address` for consul http api",
},
&cli.StringFlag{
Name: consulACLTokenFlag,
Usage: "The acl token for consul",
},
&cli.StringFlag{
Name: consulSchemeFlag,
Value: defaultConsulScheme,
Usage: "The scheme for consul",
},
&cli.BoolFlag{
Name: consulAllowStaleFlag,
Usage: "Set stale parameter on consul service health queries",
},
}
cli.AppHelpTemplate = `{{.Name}} - {{.Usage}}
usage: {{.HelpName}} [options]
{{if .VisibleFlags}}
options:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}{{if .Version}}
version: {{.Version}}{{end}}
`
}
func validateConfig(c *cli.Context) (*ServerConfig, error) {
// set defaults for non-required flags if not specified
var port = c.Int(portFlag)
var consulDatacenter = c.String(consulDatacenterFlag)
var consulHTTPAddr = c.String(consulHTTPAddrFlag)
var consulACLToken = os.Getenv("CONSUL_HTTP_TOKEN")
var consulScheme = c.String(consulSchemeFlag)
var consulAllowStaleFlag = c.Bool(consulAllowStaleFlag)
if !c.IsSet(consulHTTPAddrFlag) {
consulHTTPAddr = os.Getenv("CONSUL_HTTP_ADDR")
}
if c.IsSet(consulACLTokenFlag) {
consulACLToken = c.String(consulACLTokenFlag)
}
return &ServerConfig{
port: port,
consulDatacenter: consulDatacenter,
consulHTTPAddr: consulHTTPAddr,
consulACLToken: consulACLToken,
consulScheme: consulScheme,
consulAllowStale: consulAllowStaleFlag,
}, nil
}