-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
91 lines (86 loc) · 3.86 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
package main
import (
"math"
"os"
"strconv"
"github.com/smarthome-go/smarthome/core"
)
// Scans environment variables
// `SMARTHOME_ADMIN_PASSWORD`: (String) If specified, the admin user that is created on first launch will receive this password instead of `admin`
// `SMARTHOME_ENV_PRODUCTION`: (Bool ) Whether the server should use production presets
// `SMARTHOME_SESSION_KEY` : (String) (Only during production) Specifies a manual key for session encryption (used for larger instances): random key generation is skipped
// `SMARTHOME_DB_DATABASE` : (String) Sets the database name
// `SMARTHOME_DB_HOSTNAME` : (String) Sets the database hostname
// `SMARTHOME_DB_PORT` : (Int ) Sets the database port
// `SMARTHOME_DB_PASSWORD` : (String) Sets the database user's password
// `SMARTHOME_DB_USER` : (String) Sets the database user
func scanEnv(configStruct *core.Config) string {
// Admin password
newAdminPassword := "admin"
if adminPassword, adminPasswordOk := os.LookupEnv("SMARTHOME_ADMIN_PASSWORD"); adminPasswordOk {
newAdminPassword = adminPassword
}
// Operational mode
if productionEnvStr, productionEnvStrOk := os.LookupEnv("SMARTHOME_ENV_PRODUCTION"); productionEnvStrOk {
switch productionEnvStr {
case "TRUE":
configStruct.Server.Production = true
log.Debug("Detected `SMARTHOME_ENV_PRODUCTION` (TRUE), server will start using production presets")
case "FALSE":
configStruct.Server.Production = false
log.Debug("Detected `SMARTHOME_ENV_PRODUCTION` (FALSE), server will start in development mode")
default:
log.Warn("Could not use `SMARTHOME_ENV_PRODUCTION` as boolean value, using development mode\nValid modes are `TRUE` and `FALSE`")
}
}
// Web server session-key
if sessionKey, sessionKeyOk := os.LookupEnv("SMARTHOME_SESSION_KEY"); sessionKeyOk {
if !configStruct.Server.Production {
log.Warn("Using manually specified session encryption key during development mode. This will have no effect unless using production")
} else {
if configStruct.Server.SessionKey != "" {
log.Debug("Selected SMARTHOME_SESSION_KEY over value from config file")
} else {
log.Info("Using manually specified session encryption key from SMARTHOME_SESSION_KEY")
}
configStruct.Server.SessionKey = sessionKey
}
}
// DB variables
if dbUsername, dbUsernameOk := os.LookupEnv("SMARTHOME_DB_USER"); dbUsernameOk {
log.Debug("Selected SMARTHOME_DB_USER over value from config file")
configStruct.Database.Username = dbUsername
}
if dbPassword, dbPasswordOk := os.LookupEnv("SMARTHOME_DB_PASSWORD"); dbPasswordOk {
log.Debug("Selected SMARTHOME_DB_PASSWORD over value from config file")
configStruct.Database.Password = dbPassword
}
if dbDatabase, dbDatabaseOk := os.LookupEnv("SMARTHOME_DB_DATABASE"); dbDatabaseOk {
log.Debug("Selected SMARTHOME_DB_DATABASE over value from config file")
configStruct.Database.Database = dbDatabase
}
if dbHostname, dbHostnameOk := os.LookupEnv("SMARTHOME_DB_HOSTNAME"); dbHostnameOk {
log.Debug("Selected SMARTHOME_DB_HOSTNAME over value from config file")
configStruct.Database.Hostname = dbHostname
}
if dbPort, dbPortOk := os.LookupEnv("SMARTHOME_DB_PORT"); dbPortOk {
portInt, err := strconv.Atoi(dbPort)
if err != nil || portInt > math.MaxUint16 || portInt < 0 {
log.Warn("Could not parse `SMARTHOME_DB_PORT` to uint16, using value from config.json")
} else {
log.Debug("Selected SMARTHOME_DB_PORT over value from config file")
configStruct.Database.Port = uint16(portInt)
}
}
// Port of the webserver
if webPort, webPortOk := os.LookupEnv("SMARTHOME_PORT"); webPortOk {
webPortInt, err := strconv.Atoi(webPort)
if err != nil || webPortInt > math.MaxUint16 || webPortInt < 0 {
log.Warn("Could not parse `SMARTHOME_PORT` to uint16, using 8082")
} else {
log.Debug("Selected `SMARTHOME_PORT` over default")
port = uint16(webPortInt)
}
}
return newAdminPassword
}