-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
57 lines (46 loc) · 1.49 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
package config
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/cloudfoundry/bosh-utils/errors"
)
type ServerConfig struct {
Port int
CertificateFilePath string `json:"certificate_file_path"`
PrivateKeyFilePath string `json:"private_key_file_path"`
JwtVerificationKeyPath string `json:"jwt_verification_key_path"`
Store string
Database DBConfig
}
type DBConnectionConfig struct {
MaxOpenConnections int `json:"max_open_connections"`
MaxIdleConnections int `json:"max_idle_connections"`
}
type DBConfig struct {
Adapter string
User string
Password string
Host string
Port int
Name string `json:"db_name"`
ConnectionOptions DBConnectionConfig `json:"connection_options"`
}
func ParseConfig(filename string) (ServerConfig, error) {
config := ServerConfig{}
data, err := ioutil.ReadFile(filename)
if err != nil {
return config, errors.WrapError(err, "Failed to read config file")
}
err = json.Unmarshal([]byte(data), &config)
if err != nil {
return config, errors.WrapError(err, "Failed to parse config file")
}
if config.CertificateFilePath == "" || config.PrivateKeyFilePath == "" {
return config, errors.Error("Certificate file path and key file path should be defined")
}
if (&config.Database != nil) && (&config.Database.Adapter != nil) {
config.Database.Adapter = strings.ToLower(config.Database.Adapter)
}
return config, nil
}