-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
76 lines (62 loc) · 2.36 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
package main
import (
"fmt"
"os"
"time"
yaml "gopkg.in/yaml.v2"
)
// configSettings contains the key value pairs from the config file
type configSettings struct {
Timeout time.Duration `yaml:"timeout"`
IncludeDir string `yaml:"include_dir"`
ListenAddress string `yaml:"listen_address"`
ListenPort int `yaml:"listen_port"`
PrivateKey string `yaml:"ssl_private_key"`
CertificateFile string `yaml:"ssl_certificate_file"`
RequireAndVerifyClientCert bool `yaml:"ssl_require_and_verify_client_cert"`
ClientCertCaFile string `yaml:"ssl_client_cert_ca_file"`
SaveStateDir string `yaml:"save_state_dir"`
LogBaseDir string `yaml:"log_base_dir"`
}
// readConfigfile creates the configSettings struct from the config file
func readConfigfile(configFile string) configSettings {
fmt.Println("Trying to read config file: " + configFile)
data, err := os.ReadFile(configFile)
if err != nil {
Fatalf("readConfigfile(): There was an error parsing the config file " + configFile + ": " + err.Error())
}
var config configSettings
err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
Fatalf("In config file " + configFile + ": YAML unmarshal error: " + err.Error())
}
//fmt.Print("config: ")
//fmt.Printf("%+v\n", config)
// set default timeout to 5 seconds if no timeout setting found
if config.Timeout == 0 {
config.Timeout = 5
}
if !fileExists(config.PrivateKey) {
Fatalf("Failed to find configured ssl_private_key " + config.PrivateKey)
}
if !fileExists(config.CertificateFile) {
Fatalf("Failed to find configured ssl_certificate_file " + config.CertificateFile)
}
if !fileExists(config.ClientCertCaFile) {
Fatalf("Failed to find configured ssl_client_cert_ca_file " + config.ClientCertCaFile)
}
// set default listen address to 0.0.0.0
if len(config.ListenAddress) < 1 {
config.ListenAddress = "0.0.0.0"
}
// set default save state directory to "/tmp/goahead/"
if len(config.SaveStateDir) < 1 {
config.SaveStateDir = "/tmp/goahead/"
}
config.SaveStateDir = checkDirAndCreate(config.SaveStateDir, "config setting from config file "+configFile)
// set default listen port to 8443
if config.ListenPort == 0 {
config.ListenPort = 8443
}
return config
}