forked from b3scale/b3scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.go
123 lines (111 loc) · 3.06 KB
/
environment.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
package config
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/rs/zerolog/log"
)
// Well Known Environment Keys
const (
EnvDbURL = "B3SCALE_DB_URL"
EnvDbPoolSize = "B3SCALE_DB_POOL_SIZE"
EnvLogLevel = "B3SCALE_LOG_LEVEL"
EnvLogFormat = "B3SCALE_LOG_FORMAT"
EnvListenHTTP = "B3SCALE_LISTEN_HTTP"
EnvReverseProxy = "B3SCALE_REVERSE_PROXY_MODE"
EnvLoadFactor = "B3SCALE_LOAD_FACTOR"
EnvJWTSecret = "B3SCALE_API_JWT_SECRET"
EnvAPIURL = "B3SCALE_API_URL"
EnvAPIAccessToken = "B3SCALE_API_ACCESS_TOKEN"
EnvBBBConfig = "BBB_CONFIG"
EnvRecordingsPublishedPath = "B3SCALE_RECORDINGS_PUBLISHED_PATH"
EnvRecordingsUnpublishedPath = "B3SCALE_RECORDINGS_UNPUBLISHED_PATH"
EnvRecordingsPlaybackHost = "B3SCALE_RECORDINGS_PLAYBACK_HOST"
)
// Defaults
const (
EnvDbPoolSizeDefault = "128"
EnvDbURLDefault = "postgres://postgres:postgres@localhost:5432/b3scale"
EnvLogLevelDefault = "info"
EnvLogFormatDefault = "structured"
EnvListenHTTPDefault = "127.0.0.1:42353" // :B3S
EnvReverseProxyDefault = "false"
EnvBBBConfigDefault = "/etc/bigbluebutton/bbb-web.properties"
EnvLoadFactorDefault = "1.0"
)
// LoadEnv loads the environment from a file and
// updates the os.Environment
func LoadEnv(envfiles []string) {
for _, filename := range envfiles {
loadEnvFile(filename)
}
}
// Internal load a single env file
func loadEnvFile(filename string) {
f, err := os.Open(filename)
if err != nil {
// We could not open the file - so we ignore this.
return
}
defer f.Close()
fmt.Println("using environment from:", filename)
// Read lines and set env
scanner := bufio.NewScanner(f)
for scanner.Scan() {
l := scanner.Text()
if strings.HasPrefix(l, "#") {
continue // comment
}
tokens := strings.SplitN(l, "=", 2)
if len(tokens) != 2 {
continue // invalid
}
k := strings.Trim(strings.TrimSpace(tokens[0]), "\"'")
v := strings.Trim(strings.TrimSpace(tokens[1]), "\"'")
os.Setenv(k, v)
}
}
// EnvOpt gets a configuration from the environment
// with a default fallback.
func EnvOpt(key, fallback string) string {
value, ok := GetEnvOpt(key)
if !ok {
return fallback
}
return value
}
// GetEnvOpt gets a configuration from the environment,
// but will fail if the variable is not present.
func GetEnvOpt(key string) (string, bool) {
value := os.Getenv(key)
if value == "" {
return "", false
}
return value, true
}
// IsEnabled returns true if the input is trueish
func IsEnabled(value string) bool {
value = strings.ToLower(value)
switch value {
case "yes":
return true
case "true":
return true
case "1":
return true
}
return false
}
// GetLoadFactor retrievs the load factor
// from the environment.
func GetLoadFactor() float64 {
val := EnvOpt(EnvLoadFactor, EnvLoadFactorDefault)
factor, err := strconv.ParseFloat(val, 64)
if err != nil {
log.Error().Err(err).Msg("invalid value for " + EnvLoadFactor)
return 1.0
}
return factor
}