forked from red-gold/telar-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
328 lines (278 loc) · 9.85 KB
/
start.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
package function
import (
"context"
"encoding/base64"
"fmt"
"log"
"os"
"strconv"
core "github.com/red-gold/telar-core"
"github.com/red-gold/telar-core/config"
coreSetting "github.com/red-gold/telar-core/config"
"github.com/red-gold/telar-core/data/mongodb"
coreUtils "github.com/red-gold/telar-core/utils"
)
const (
basePath = "/var/openfaas/secrets/"
mongoHostSecretKey = "mongo-host"
mongoDatabaseSecretKey = "mongo-database"
phoneAuthIDSecretKey = "phone-auth-id"
phoneAuthTokenSecretKey = "phone-auth-token"
privateKeySecretKey = "key"
publicKeySecretKey = "key.pub"
recaptchaSecretKey = "recaptcha-key"
refEmailPassSecretKey = "ref-email-pass"
payloadSecretKey = "payload-secret"
)
var secretKeys = []string{mongoHostSecretKey, mongoDatabaseSecretKey,
phoneAuthIDSecretKey, phoneAuthTokenSecretKey, privateKeySecretKey,
publicKeySecretKey, recaptchaSecretKey, refEmailPassSecretKey, payloadSecretKey}
// Initiailze micros configurations
func InitConfig() {
coreConfig := getAllConfiguration()
core.InitConfigFromData(*coreConfig)
}
// Start run startup operations
func Start(ctx context.Context) (interface{}, error) {
coreConfig := coreSetting.AppConfig
switch *coreConfig.DBType {
case config.DB_MONGO:
mongoClient, err := mongodb.NewMongoClient(ctx, *coreConfig.MongoDBHost, *coreConfig.Database)
if err != nil {
return nil, err
}
return mongoClient, nil
}
return nil, fmt.Errorf("Please set valid database type in confing file!")
}
// getAllConfigFromFile get all config from files
func getAllConfigFromFile() map[string][]byte {
filePaths := []string{}
for _, v := range secretKeys {
filePaths = append(filePaths, basePath+v)
}
return coreUtils.GetFilesContents(filePaths...)
}
// getAllConfiguration get all configuration
func getAllConfiguration() *config.Configuration {
var newCoreConfig config.Configuration
loadSecretMode, ok := os.LookupEnv("load_secret_mode")
if ok {
log.Printf("[INFO]: Load secret mode information loaded from env.")
if loadSecretMode == "env" {
loadSecretsFromEnv(&newCoreConfig)
}
} else {
log.Printf("[INFO]: No secret mode in env. Secrets are loading from file.")
loadSecretsFromFile(&newCoreConfig)
}
// Load from environment //
appName, ok := os.LookupEnv("app_name")
if ok {
newCoreConfig.AppName = &appName
log.Printf("[INFO]: App Name information loaded from env.")
}
queryPrettyURL, ok := os.LookupEnv("query_pretty_url")
if ok {
parsedQueryPrettyURL, errParseDebug := strconv.ParseBool(queryPrettyURL)
if errParseDebug != nil {
log.Printf("[ERROR]: Query Pretty URL information loading error: %s", errParseDebug.Error())
}
newCoreConfig.QueryPrettyURL = &parsedQueryPrettyURL
log.Printf("[INFO]: Query Pretty URL information loaded from env.")
}
debug, ok := os.LookupEnv("debug")
if ok {
parsedDebug, errParseDebug := strconv.ParseBool(debug)
if errParseDebug != nil {
log.Printf("[ERROR]: Debug information loading error: %s", errParseDebug.Error())
}
newCoreConfig.Debug = &parsedDebug
log.Printf("[INFO]: Debug information loaded from env.")
}
gateway, ok := os.LookupEnv("gateway")
if ok {
newCoreConfig.Gateway = &gateway
log.Printf("[INFO]: Gateway information loaded from env.")
}
internalGateway, ok := os.LookupEnv("internal_gateway")
if ok {
newCoreConfig.InternalGateway = &internalGateway
log.Printf("[INFO]: Internal gateway information loaded from env. | %s |", internalGateway)
}
webDomain, ok := os.LookupEnv("web_domain")
if ok {
newCoreConfig.WebDomain = &webDomain
log.Printf("[INFO]: Web domain information loaded from env.")
}
orgName, ok := os.LookupEnv("org_name")
if ok {
newCoreConfig.OrgName = &orgName
log.Printf("[INFO]: Organization Name information loaded from env.")
}
orgAvatar, ok := os.LookupEnv("org_avatar")
if ok {
newCoreConfig.OrgAvatar = &orgAvatar
log.Printf("[INFO]: Organization Avatar information loaded from env.")
}
server, ok := os.LookupEnv("server")
if ok {
newCoreConfig.Server = &server
log.Printf("[INFO]: Server information loaded from env.")
}
recaptchaSiteKey, ok := os.LookupEnv("recaptcha_site_key")
if ok {
newCoreConfig.RecaptchaSiteKey = &recaptchaSiteKey
log.Printf("[INFO]: Recaptcha site key information loaded from env.")
}
origin, ok := os.LookupEnv("origin")
if ok {
newCoreConfig.Origin = &origin
log.Printf("[INFO]: Origin information loaded from env.")
}
headerCookieName, ok := os.LookupEnv("header_cookie_name")
if ok {
newCoreConfig.HeaderCookieName = &headerCookieName
log.Printf("[INFO]: Header cookie name information loaded from env.")
}
payloadCookieName, ok := os.LookupEnv("payload_cookie_name")
if ok {
newCoreConfig.PayloadCookieName = &payloadCookieName
log.Printf("[INFO]: Payload cookie name information loaded from env.")
}
signatureCookieName, ok := os.LookupEnv("signature_cookie_name")
if ok {
newCoreConfig.SignatureCookieName = &signatureCookieName
log.Printf("[INFO]: Signature cookie name information loaded from env.")
}
baseRoute, ok := os.LookupEnv("base_route_domain")
if ok {
newCoreConfig.BaseRoute = &baseRoute
log.Printf("[INFO]: Base route information loaded from env.")
}
smtpEmail, ok := os.LookupEnv("smtp_email")
if ok {
newCoreConfig.SmtpEmail = &smtpEmail
log.Printf("[INFO]: SMTP Email information loaded from env.")
}
refEmail, ok := os.LookupEnv("ref_email")
if ok {
newCoreConfig.RefEmail = &refEmail
log.Printf("[INFO]: Reference Email information loaded from env.")
}
phoneSourceNumebr, ok := os.LookupEnv("phone_source_number")
if ok {
newCoreConfig.PhoneSourceNumber = &phoneSourceNumebr
log.Printf("[INFO]: Phone Source Number information loaded from env.")
}
phoneAuthToken, ok := os.LookupEnv("phone_auth_token")
if ok {
newCoreConfig.PhoneAuthToken = &phoneAuthToken
log.Printf("[INFO]: Phone Auth Token information loaded from env.")
}
phoneAuthId, ok := os.LookupEnv("phone_auth_id")
if ok {
newCoreConfig.PhoneAuthId = &phoneAuthId
log.Printf("[INFO]: Phone Auth Id information loaded from env.")
}
dbType, ok := os.LookupEnv("db_type")
if ok {
newCoreConfig.DBType = &dbType
log.Printf("[INFO]: Database type information loaded from env.")
}
return &newCoreConfig
}
// loadSecretsFromFile Load secrets from file
func loadSecretsFromFile(newCoreConfig *config.Configuration) {
filesConfig := getAllConfigFromFile()
// Load from files //
if filesConfig[basePath+payloadSecretKey] != nil {
payloadSecret := string(filesConfig[basePath+payloadSecretKey])
newCoreConfig.PayloadSecret = &payloadSecret
log.Printf("[INFO]: Payload secret information loaded from env.")
}
if filesConfig[basePath+publicKeySecretKey] != nil {
publicKey := string(filesConfig[basePath+publicKeySecretKey])
newCoreConfig.PublicKey = &publicKey
log.Printf("[INFO]: Public key information loaded from env.")
}
if filesConfig[basePath+privateKeySecretKey] != nil {
privateKey := string(filesConfig[basePath+privateKeySecretKey])
newCoreConfig.PrivateKey = &privateKey
log.Printf("[INFO]: Private key information loaded from env.")
}
if filesConfig[basePath+recaptchaSecretKey] != nil {
recaptchaKey := string(filesConfig[basePath+recaptchaSecretKey])
newCoreConfig.RecaptchaKey = &recaptchaKey
log.Printf("[INFO]: Recaptcha key information loaded from env.")
}
if filesConfig[basePath+mongoHostSecretKey] != nil {
mongoDBHost := string(filesConfig[basePath+mongoHostSecretKey])
newCoreConfig.MongoDBHost = &mongoDBHost
log.Printf("[INFO]: MongoDB host information loaded from env.")
}
if filesConfig[basePath+mongoDatabaseSecretKey] != nil {
mongoDB := string(filesConfig[basePath+mongoDatabaseSecretKey])
newCoreConfig.Database = &mongoDB
log.Printf("[INFO]: Database name information loaded from env.")
}
if filesConfig[basePath+refEmailPassSecretKey] != nil {
refEmailPass := string(filesConfig[basePath+refEmailPassSecretKey])
newCoreConfig.RefEmailPass = &refEmailPass
log.Printf("[INFO]: Ref email password information loaded from env.")
}
}
// loadSecretsFromEnv Load secrets from environment variables
func loadSecretsFromEnv(newCoreConfig *config.Configuration) {
payloadSecret, ok := os.LookupEnv("payload_secret")
if ok {
payloadSecret = decodeBase64(payloadSecret)
newCoreConfig.PayloadSecret = &payloadSecret
log.Printf("[INFO]: Payload secret information loaded from env.")
}
publicKey, ok := os.LookupEnv("key_pub")
if ok {
publicKey = decodeBase64(publicKey)
newCoreConfig.PublicKey = &publicKey
log.Printf("[INFO]: Public key information loaded from env.")
}
privateKey, ok := os.LookupEnv("key")
if ok {
privateKey = decodeBase64(privateKey)
newCoreConfig.PrivateKey = &privateKey
log.Printf("[INFO]: Private key information loaded from env.")
}
recaptchaKey, ok := os.LookupEnv("recaptcha_key")
if ok {
recaptchaKey = decodeBase64(recaptchaKey)
newCoreConfig.RecaptchaKey = &recaptchaKey
log.Printf("[INFO]: Recaptcha key information loaded from env.")
}
mongoDBHost, ok := os.LookupEnv("mongo_host")
if ok {
mongoDBHost = decodeBase64(mongoDBHost)
newCoreConfig.MongoDBHost = &mongoDBHost
log.Printf("[INFO]: MongoDB host information loaded from env.")
}
mongoDB, ok := os.LookupEnv("mongo_database")
if ok {
mongoDB = decodeBase64(mongoDB)
newCoreConfig.Database = &mongoDB
log.Printf("[INFO]: Database name information loaded from env.")
}
refEmailPass, ok := os.LookupEnv("ref_email_pass")
if ok {
refEmailPass = decodeBase64(refEmailPass)
newCoreConfig.RefEmailPass = &refEmailPass
log.Printf("[INFO]: Ref email password information loaded from env.")
}
}
// decodeBase64 Decode base64 string
func decodeBase64(encodedString string) string {
base64Value, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
fmt.Println("[ERROR] decode secret base64 value with value: ", encodedString, " - ", err.Error())
panic(err)
}
return string(base64Value)
}