-
Notifications
You must be signed in to change notification settings - Fork 787
/
env.go
219 lines (194 loc) · 5.31 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
// 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"
// MySQLSeeds env
MySQLSeeds = "MYSQL_SEEDS"
// MySQLPort env
MySQLPort = "MYSQL_PORT"
// MySQLDefaultPort MySQL default port
MySQLDefaultPort = "3306"
// 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"
)
// 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(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))
}
}
}
// 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
}
// 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
}
// 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
}