forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
352 lines (314 loc) · 8.88 KB
/
env.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package environment
import (
"fmt"
"os"
"strconv"
)
const (
// Localhost default localhost
Localhost = "127.0.0.1"
// CassandraSeeds env
CassandraSeeds = "CASSANDRA_SEEDS"
// CassandraPort env
CassandraPort = "CASSANDRA_DB_PORT"
// CassandraDefaultPort Cassandra default port
CassandraDefaultPort = "9042"
// CassandraUsername env
CassandraUsername = "CASSANDRA_DB_USERNAME"
// CassandraDefaultUsername Cassandra default username
CassandraDefaultUsername = ""
// CassandraPassword env
CassandraPassword = "CASSANDRA_DB_PASSWORD"
// CassandraDefaultPassword Cassandra default password
CassandraDefaultPassword = ""
// CassandraAllowedAuthenticators env
CassandraAllowedAuthenticators = "CASSANDRA_DB_ALLOWED_AUTHENTICATORS"
// CassandraProtoVersion env
CassandraProtoVersion = "CASSANDRA_PROTO_VERSION"
// CassandraDefaultProtoVersion Cassandra default protocol version
CassandraDefaultProtoVersion = "4"
// CassandraDefaultProtoVersionInteger Cassandra default protocol version int version
CassandraDefaultProtoVersionInteger = 4
// MySQLSeeds env
MySQLSeeds = "MYSQL_SEEDS"
// MySQLPort env
MySQLPort = "MYSQL_PORT"
// MySQLDefaultPort is MySQL default port
MySQLDefaultPort = "3306"
// MySQLUser env
MySQLUser = "MYSQL_USER"
// MySQLDefaultUser is default user
MySQLDefaultUser = "root"
// MySQLPassword env
MySQLPassword = "MYSQL_PASSWORD"
// MySQLDefaultPassword is default password
MySQLDefaultPassword = "cadence"
// MongoSeeds env
MongoSeeds = "MONGO_SEEDS"
// MongoPort env
MongoPort = "MONGO_PORT"
// MongoDefaultPort is Mongo default port
MongoDefaultPort = "27017"
// KafkaSeeds env
KafkaSeeds = "KAFKA_SEEDS"
// KafkaPort env
KafkaPort = "KAFKA_PORT"
// KafkaDefaultPort Kafka default port
KafkaDefaultPort = "9092"
// ESSeeds env
ESSeeds = "ES_SEEDS"
// ESPort env
ESPort = "ES_PORT"
// ESDefaultPort ES default port
ESDefaultPort = "9200"
// ESVersion is the ElasticSearch version
ESVersion = "ES_VERSION"
// ESDefaultVersion is the default version
ESDefaultVersion = "v6"
// PostgresSeeds env
PostgresSeeds = "POSTGRES_SEEDS"
// PostgresPort env
PostgresPort = "POSTGRES_PORT"
// PostgresDefaultPort Postgres default port
PostgresDefaultPort = "5432"
// CLITransportProtocol env
CLITransportProtocol = "CADENCE_CLI_TRANSPORT_PROTOCOL"
// DefaultCLITransportProtocol CLI default channel
DefaultCLITransportProtocol = "tchannel"
)
// SetupEnv setup the necessary env
func SetupEnv() {
if os.Getenv(CassandraSeeds) == "" {
err := os.Setenv(CassandraSeeds, Localhost)
if err != nil {
panic(fmt.Sprintf("error setting env %v", CassandraSeeds))
}
}
if os.Getenv(CassandraPort) == "" {
err := os.Setenv(CassandraPort, CassandraDefaultPort)
if err != nil {
panic(fmt.Sprintf("error setting env %v", CassandraPort))
}
}
if os.Getenv(CassandraProtoVersion) == "" {
err := os.Setenv(CassandraProtoVersion, CassandraDefaultProtoVersion)
if err != nil {
panic(fmt.Sprintf("error setting env %v", CassandraProtoVersion))
}
}
if os.Getenv(MySQLSeeds) == "" {
err := os.Setenv(MySQLSeeds, Localhost)
if err != nil {
panic(fmt.Sprintf("error setting env %v", MySQLSeeds))
}
}
if os.Getenv(MySQLPort) == "" {
err := os.Setenv(MySQLPort, MySQLDefaultPort)
if err != nil {
panic(fmt.Sprintf("error setting env %v", MySQLPort))
}
}
if os.Getenv(PostgresSeeds) == "" {
err := os.Setenv(PostgresSeeds, Localhost)
if err != nil {
panic(fmt.Sprintf("error setting env %v", PostgresSeeds))
}
}
if os.Getenv(PostgresPort) == "" {
err := os.Setenv(PostgresPort, PostgresDefaultPort)
if err != nil {
panic(fmt.Sprintf("error setting env %v", PostgresPort))
}
}
if os.Getenv(KafkaSeeds) == "" {
err := os.Setenv(KafkaSeeds, Localhost)
if err != nil {
panic(fmt.Sprintf("error setting env %v", KafkaSeeds))
}
}
if os.Getenv(KafkaPort) == "" {
err := os.Setenv(KafkaPort, KafkaDefaultPort)
if err != nil {
panic(fmt.Sprintf("error setting env %v", KafkaPort))
}
}
if os.Getenv(ESSeeds) == "" {
err := os.Setenv(ESSeeds, Localhost)
if err != nil {
panic(fmt.Sprintf("error setting env %v", ESSeeds))
}
}
if os.Getenv(ESPort) == "" {
err := os.Setenv(ESPort, ESDefaultPort)
if err != nil {
panic(fmt.Sprintf("error setting env %v", ESPort))
}
}
if os.Getenv(CLITransportProtocol) == "" {
err := os.Setenv(CLITransportProtocol, DefaultCLITransportProtocol)
if err != nil {
panic(fmt.Sprintf("error setting env %v", CLITransportProtocol))
}
}
}
// GetCassandraAddress return the cassandra address
func GetCassandraAddress() string {
addr := os.Getenv(CassandraSeeds)
if addr == "" {
addr = Localhost
}
return addr
}
// GetCassandraPort return the cassandra port
func GetCassandraPort() int {
port := os.Getenv(CassandraPort)
if port == "" {
port = CassandraDefaultPort
}
p, err := strconv.Atoi(port)
if err != nil {
panic(fmt.Sprintf("error getting env %v", CassandraPort))
}
return p
}
// GetCassandraUsername return the cassandra username
func GetCassandraUsername() string {
user := os.Getenv(CassandraUsername)
if user == "" {
user = CassandraDefaultUsername
}
return user
}
// GetCassandraPassword return the cassandra password
func GetCassandraPassword() string {
pass := os.Getenv(CassandraPassword)
if pass == "" {
pass = CassandraDefaultPassword
}
return pass
}
// GetCassandraAllowedAuthenticators return the cassandra allowed authenticators
func GetCassandraAllowedAuthenticators() []string {
var authenticators []string
configuredAuthenticators := os.Getenv(CassandraAllowedAuthenticators)
if configuredAuthenticators == "" {
return authenticators
}
authenticators = append(authenticators, configuredAuthenticators)
return authenticators
}
// GetCassandraProtoVersion return the cassandra protocol version
func GetCassandraProtoVersion() int {
protoVersion := os.Getenv(CassandraProtoVersion)
if protoVersion == "" {
protoVersion = CassandraDefaultProtoVersion
}
p, err := strconv.Atoi(protoVersion)
if err != nil {
panic(fmt.Sprintf("error getting env %v", CassandraProtoVersion))
}
return p
}
// GetMySQLAddress return the MySQL address
func GetMySQLAddress() string {
addr := os.Getenv(MySQLSeeds)
if addr == "" {
addr = Localhost
}
return addr
}
// GetMySQLPort return the MySQL port
func GetMySQLPort() int {
port := os.Getenv(MySQLPort)
if port == "" {
port = MySQLDefaultPort
}
p, err := strconv.Atoi(port)
if err != nil {
panic(fmt.Sprintf("error getting env %v", MySQLPort))
}
return p
}
// GetMySQLUser return the MySQL user
func GetMySQLUser() string {
user := os.Getenv(MySQLUser)
if user == "" {
user = MySQLDefaultUser
}
return user
}
// GetMySQLPassword return the MySQL password
func GetMySQLPassword() string {
pw := os.Getenv(MySQLPassword)
if pw == "" {
pw = MySQLDefaultPassword
}
return pw
}
// GetPostgresAddress return the Postgres address
func GetPostgresAddress() string {
addr := os.Getenv(PostgresSeeds)
if addr == "" {
addr = Localhost
}
return addr
}
// GetPostgresPort return the Postgres port
func GetPostgresPort() int {
port := os.Getenv(PostgresPort)
if port == "" {
port = PostgresDefaultPort
}
p, err := strconv.Atoi(port)
if err != nil {
panic(fmt.Sprintf("error getting env %v", PostgresPort))
}
return p
}
// GetESVersion return the ElasticSearch version
func GetESVersion() string {
version := os.Getenv(ESVersion)
if version == "" {
version = ESDefaultVersion
}
return version
}
// GetMongoAddress return the MySQL address
func GetMongoAddress() string {
addr := os.Getenv(MongoSeeds)
if addr == "" {
addr = Localhost
}
return addr
}
// GetMongoPort return the MySQL port
func GetMongoPort() int {
port := os.Getenv(MongoPort)
if port == "" {
port = MongoDefaultPort
}
p, err := strconv.Atoi(port)
if err != nil {
panic(fmt.Sprintf("error getting env %v", MongoPort))
}
return p
}