This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
96 lines (83 loc) · 2.78 KB
/
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
package cas
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var CONFIG_ENV_OVERRIDE_MAP map[string]string = map[string]string{
"host": "CASGO_HOST",
"port": "CASGO_PORT",
"dbHost": "CASGO_DBHOST",
"dbName": "CASGO_DBNAME",
"cookieSecret": "CASGO_SECRET",
"templatesDirectory": "CASGO_TEMPLATES",
"companyName": "CASGO_COMPNAME",
"authMethod": "CASGO_DEFAULT_AUTH",
"logLevel": "CASGO_LOG_LVL",
"tlsCertFile": "CASGO_TLS_CERT",
"tlsKeyFile": "CASGO_TLS_KEY",
}
var CONFIG_DEFAULTS map[string]string = map[string]string{
"host": "0.0.0.0",
"port": "9090",
"dbHost": "localhost:28015",
"dbName": "casgo",
"cookieSecret": "secret-casgo-secret",
"templatesDirectory": "templates/",
"companyName": "companyABC",
"authMethod": "password",
"logLevel": "WARN",
"tlsCertFile": "fixtures/ssl/cert.pem",
"tlsKeyFile": "fixtures/ssl/eckey.pem",
}
// Create default casgo configuration, with user overrides if any
func NewCASServerConfig(configFilePath string) (map[string]string, error) {
// Set default config values
serverConfig := make(map[string]string)
for k, v := range CONFIG_DEFAULTS {
serverConfig[k] = v
}
// Read config from file, if non-empty config file path is passed
if len(configFilePath) > 0 {
// Read file
buf, err := ioutil.ReadFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("[ERROR] Failed to read given configuration file [%s] %v", configFilePath, err)
}
if len(buf) == 0 {
log.Printf("[WARNING] Loaded configuration file was empty")
}
// Load JSON
fileConfig := make(map[string]string)
err = json.Unmarshal(buf, &fileConfig)
if err != nil {
return nil, fmt.Errorf("[ERROR] Failed to unmarshal JSON in configuration file [%s], %v", configFilePath, err)
}
// Copy values from loaded file config to actual server config
for k, v := range fileConfig {
serverConfig[k] = v
}
}
// Override config with what is stored in env
serverConfig = overrideConfigWithEnv(serverConfig)
// Update filepath with absolute path
absDirPath, err := filepath.Abs(serverConfig["templatesDirectory"])
if err != nil {
log.Printf("[WARNING] Failed to resolve absolute path for templatesDirectory %s", serverConfig["templatesDirectory"])
} else {
serverConfig["templatesDirectory"] = absDirPath
}
return serverConfig, nil
}
// Override a configuration hash with values provided by ENV
func overrideConfigWithEnv(config map[string]string) map[string]string {
for configKey, envVarName := range CONFIG_ENV_OVERRIDE_MAP {
if envValue := os.Getenv(envVarName); len(envValue) > 0 {
config[configKey] = envValue
}
}
return config
}