-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-config.go
99 lines (84 loc) · 2.85 KB
/
read-config.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
package db
import (
"fmt"
"log/slog"
"os"
"strconv"
)
func ReadDBConfigFromEnv(
dbLabel string,
connectionStrEnv string,
usernameEnv string,
passwordEnv string,
connectionPrefixEnv string,
timeoutEnv string,
idleConnTimeoutEnv string,
maxPoolSizeEnv string,
useNoCursorTimeoutEnv string,
dbNamePrefixEnv string,
instanceIDs []string,
) DBConfig {
connStr := os.Getenv(connectionStrEnv)
username := os.Getenv(usernameEnv)
password := os.Getenv(passwordEnv)
prefix := os.Getenv(connectionPrefixEnv) // Used in test mode
if connStr == "" || username == "" || password == "" {
slog.Error("couldn't read DB credentials", slog.String("db", dbLabel))
panic("couldn't read DB credentials")
}
URI := fmt.Sprintf(`mongodb%s://%s:%s@%s`, prefix, username, password, connStr)
var err error
Timeout, err := strconv.Atoi(os.Getenv(timeoutEnv))
if err != nil {
slog.Error("DB config could not parse timeout", slog.String("error", err.Error()), slog.String(timeoutEnv, os.Getenv(timeoutEnv)), slog.String("db", dbLabel))
panic(err)
}
IdleConnTimeout, err := strconv.Atoi(os.Getenv(idleConnTimeoutEnv))
if err != nil {
slog.Error("DB config could not parse idle connection timeout", slog.String("error", err.Error()), slog.String(idleConnTimeoutEnv, os.Getenv(idleConnTimeoutEnv)), slog.String("db", dbLabel))
panic(err)
}
mps, err := strconv.Atoi(os.Getenv(maxPoolSizeEnv))
MaxPoolSize := uint64(mps)
if err != nil {
slog.Error("DB config could not parse max pool size", slog.String("error", err.Error()), slog.String(maxPoolSizeEnv, os.Getenv(maxPoolSizeEnv)), slog.String("db", dbLabel))
panic(err)
}
noCursorTimeout := os.Getenv(useNoCursorTimeoutEnv) == "true"
DBNamePrefix := os.Getenv(dbNamePrefixEnv)
return DBConfig{
URI: URI,
Timeout: Timeout,
IdleConnTimeout: IdleConnTimeout,
MaxPoolSize: MaxPoolSize,
NoCursorTimeout: noCursorTimeout,
DBNamePrefix: DBNamePrefix,
InstanceIDs: instanceIDs,
}
}
func DBConfigFromYamlObj(yamlObj DBConfigYaml, instanceIDs []string) DBConfig {
connStr := yamlObj.ConnectionStr
username := yamlObj.Username
password := yamlObj.Password
prefix := yamlObj.ConnectionPrefix // Used in test mode
if connStr == "" || username == "" || password == "" {
panic("couldn't read DB credentials")
}
URI := fmt.Sprintf(`mongodb%s://%s:%s@%s`, prefix, username, password, connStr)
Timeout := yamlObj.Timeout
IdleConnTimeout := yamlObj.IdleConnTimeout
mps := yamlObj.MaxPoolSize
MaxPoolSize := uint64(mps)
noCursorTimeout := yamlObj.UseNoCursorTimeout
DBNamePrefix := yamlObj.DBNamePrefix
return DBConfig{
URI: URI,
Timeout: Timeout,
IdleConnTimeout: IdleConnTimeout,
MaxPoolSize: MaxPoolSize,
NoCursorTimeout: noCursorTimeout,
DBNamePrefix: DBNamePrefix,
InstanceIDs: instanceIDs,
RunIndexCreation: yamlObj.RunIndexCreation,
}
}