/
options.go
194 lines (157 loc) · 7.11 KB
/
options.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package service
import (
"flag"
"fmt"
"strings"
"time"
"github.com/mailgun/metrics"
log "github.com/sirupsen/logrus"
)
type Options struct {
ApiPort int
ApiInterface string
PidPath string
Port int
Interface string
CertPath string
Engine string
EtcdApiVersion int
EtcdNodes listOptions
EtcdKey string
EtcdCaFile string
EtcdCertFile string
EtcdKeyFile string
EtcdConsistency string
EtcdSyncIntervalSeconds int64
EtcdUsername string
EtcdPassword string
EtcdInsecureSkipVerify bool
EtcdEnableTLS bool
EtcdDebug bool
Log string
LogSeverity SeverityFlag
LogFormatter log.Formatter // if set, .Log will be ignored
ServerReadTimeout time.Duration
ServerWriteTimeout time.Duration
ServerMaxHeaderBytes int
EndpointDialTimeout time.Duration
EndpointReadTimeout time.Duration
SealKey string
StatsdAddr string
StatsdPrefix string
MetricsClient metrics.Client
DefaultListener bool
TrustForwardHeader bool
MemProfileRate int
EnableJaegerTracing bool
DebugJaegerTracing bool
Aliases mapOptions
}
type SeverityFlag struct {
S log.Level
}
func (s *SeverityFlag) Get() interface{} {
return &s.S
}
// Set is part of the flag.Value interface.
func (s *SeverityFlag) Set(value string) error {
sev, err := log.ParseLevel(strings.ToLower(value))
if err != nil {
return err
}
s.S = sev
return nil
}
func (s *SeverityFlag) String() string {
return s.S.String()
}
// Helper to parse options that can occur several times, e.g. cassandra nodes
type listOptions []string
func (o *listOptions) String() string {
return fmt.Sprint(*o)
}
func (o *listOptions) Set(value string) error {
parts := strings.Split(value, ",")
*o = append(*o, parts...)
return nil
}
// Helper to parse options that are a list of key=values (key1=value1,key2=value2)
type mapOptions map[string]string
func (o *mapOptions) String() string {
return fmt.Sprint(*o)
}
func (o *mapOptions) Set(value string) error {
*o = make(map[string]string)
parts := strings.Split(value, ",")
for _, kv := range parts {
kvParts := strings.Split(kv, "=")
if len(kvParts) != 2 {
fmt.Printf("Invalid key=value format '%s'\n", kv)
continue
}
(*o)[kvParts[0]] = kvParts[1]
}
return nil
}
func validateOptions(o Options) (Options, error) {
if o.EndpointDialTimeout+o.EndpointReadTimeout >= o.ServerWriteTimeout {
fmt.Printf("!!!!!! WARN: serverWriteTimout(%s) should be > endpointDialTimeout(%s) + endpointReadTimeout(%s)\n\n",
o.ServerWriteTimeout, o.EndpointDialTimeout, o.EndpointReadTimeout)
}
flag.Visit(func(f *flag.Flag) {
if f.Name == "readTimeout" {
fmt.Printf("!!!!!! WARN: Using deprecated readTimeout flag, use serverReadTimeout instead\n\n")
}
if f.Name == "writeTimeout" {
fmt.Printf("!!!!!! WARN: Using deprecated writeTimeout flag, use serverWriteTimeout instead\n\n")
}
})
return o, nil
}
func ParseCommandLine() (options Options, err error) {
flag.Var(&options.EtcdNodes, "etcd", "Etcd discovery service API endpoints")
flag.IntVar(&options.EtcdApiVersion, "etcdApiVer", 3, "Etcd Client API version (defaults to '3')")
flag.StringVar(&options.EtcdKey, "etcdKey", "vulcand", "Etcd key for storing configuration")
flag.StringVar(&options.EtcdCaFile, "etcdCaFile", "", "Path to CA file for etcd communication")
flag.StringVar(&options.EtcdCertFile, "etcdCertFile", "", "Path to cert file for etcd communication")
flag.StringVar(&options.EtcdKeyFile, "etcdKeyFile", "", "Path to key file for etcd communication")
flag.StringVar(&options.EtcdConsistency, "etcdConsistency", "STRONG", "Etcd consistency (STRONG or WEAK)")
flag.Int64Var(&options.EtcdSyncIntervalSeconds, "etcdSyncIntervalSeconds", 0, "Interval between updating etcd cluster information. Use 0 to disable any syncing (default behavior.)")
flag.StringVar(&options.EtcdUsername, "etcdUsername", "", "Username for etcd auth")
flag.StringVar(&options.EtcdPassword, "etcdPassword", "", "Password for etcd auth")
flag.BoolVar(&options.EtcdInsecureSkipVerify, "etcdInsecureSkipVerify", false, "Enable TLS for etcd and skip ca verification")
flag.BoolVar(&options.EtcdEnableTLS, "etcdEnableTLS", false, "Enable TLS for etcd")
flag.BoolVar(&options.EtcdDebug, "etcdDebug", false, "Output etcd debug info to stderr")
flag.StringVar(&options.PidPath, "pidPath", "", "Path to write PID file to")
flag.IntVar(&options.Port, "port", 8181, "Port to listen on")
flag.IntVar(&options.ApiPort, "apiPort", 8182, "Port to provide api on")
flag.StringVar(&options.Interface, "interface", "", "Interface to bind to")
flag.StringVar(&options.ApiInterface, "apiInterface", "", "Interface to for API to bind to")
flag.StringVar(&options.CertPath, "certPath", "", "KeyPair to use (enables TLS)")
flag.StringVar(&options.Engine, "engine", "etcd", "Engine to use. Options: etcd (default), memng")
flag.StringVar(&options.Log, "log", "console", "Logging to use (console, json, syslog or logstash)")
options.LogSeverity.S = log.WarnLevel
flag.Var(&options.LogSeverity, "logSeverity", "logs at or above this level to the logging output")
flag.IntVar(&options.ServerMaxHeaderBytes, "serverMaxHeaderBytes", 1<<20, "Maximum size of request headers")
flag.DurationVar(&options.ServerReadTimeout, "readTimeout", time.Duration(60)*time.Second, "HTTP server read timeout (deprecated)")
flag.DurationVar(&options.ServerReadTimeout, "serverReadTimeout", time.Duration(60)*time.Second, "HTTP server read timeout")
flag.DurationVar(&options.ServerWriteTimeout, "writeTimeout", time.Duration(60)*time.Second, "HTTP server write timeout (deprecated)")
flag.DurationVar(&options.ServerWriteTimeout, "serverWriteTimeout", time.Duration(60)*time.Second, "HTTP server write timeout")
flag.DurationVar(&options.EndpointDialTimeout, "endpointDialTimeout", time.Duration(5)*time.Second, "Endpoint dial timeout")
flag.DurationVar(&options.EndpointReadTimeout, "endpointReadTimeout", time.Duration(50)*time.Second, "Endpoint read timeout")
flag.StringVar(&options.SealKey, "sealKey", "", "Seal key used to store encrypted data in the backend")
flag.StringVar(&options.StatsdPrefix, "statsdPrefix", "", "Statsd prefix will be appended to the metrics emitted by this instance")
flag.StringVar(&options.StatsdAddr, "statsdAddr", "", "Statsd address in form of 'host:port'")
flag.BoolVar(&options.DefaultListener, "default-listener", true, "Enables the default listener on startup (Default value: true)")
flag.BoolVar(&options.TrustForwardHeader, "trustForwardHeader", false, "Whether X-Forwarded-XXX headers should be trusted")
flag.IntVar(&options.MemProfileRate, "memProfileRate", 0, "Heap profile rate in bytes (disabled if 0)")
flag.BoolVar(&options.EnableJaegerTracing, "enableJaegerTracing", false, "Enable open tracing support via jaeger")
flag.BoolVar(&options.DebugJaegerTracing, "debugJaegerTracing", false, "Trace every request and log the trace")
flag.Var(&options.Aliases, "aliases", "Comma separated list of key=values which modify frontend expressions")
flag.Parse()
options, err = validateOptions(options)
if err != nil {
return options, err
}
return options, nil
}