/
config.go
84 lines (70 loc) · 2.48 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
package config
import (
"os"
servertools "github.com/Festivals-App/festivals-server-tools"
"github.com/pelletier/go-toml"
"github.com/rs/zerolog/log"
)
type Config struct {
ServiceBindAddress string
ServiceBindHost string
ServicePort int
ServiceKey string
TLSRootCert string
TLSCert string
TLSKey string
DatabaseBindAddress string
DatabasePort int
LoversEar string
Interval int
IdentityEndpoint string
}
func DefaultConfig() *Config {
// first we try to parse the config at the global configuration path
if servertools.FileExists("/etc/festivals-database-node.conf") {
config := ParseConfig("/etc/festivals-database-node.conf")
if config != nil {
return config
}
}
// if there is no global configuration check the current folder for the template config file
// this is mostly so the application will run in development environment
path, err := os.Getwd()
if err != nil {
log.Fatal().Msg("server initialize: could not read default config file with error:" + err.Error())
}
path = path + "/config_template.toml"
return ParseConfig(path)
}
func ParseConfig(cfgFile string) *Config {
content, err := toml.LoadFile(cfgFile)
if err != nil {
log.Fatal().Msg("server initialize: could not read config file at '" + cfgFile + "' with error: " + err.Error())
}
serviceBindAdress := content.Get("service.bind-address").(string)
serviceBindHost := content.Get("service.bind-host").(string)
serverPort := content.Get("service.port").(int64)
serviceKey := content.Get("service.key").(string)
tlsrootcert := content.Get("tls.festivaslapp-root-ca").(string)
tlscert := content.Get("tls.cert").(string)
tlskey := content.Get("tls.key").(string)
databaseBindAdress := content.Get("database.bind-address").(string)
databasePort := content.Get("database.port").(int64)
loversear := content.Get("heartbeat.endpoint").(string)
interval := content.Get("heartbeat.interval").(int64)
identity := content.Get("authentication.endpoint").(string)
return &Config{
ServiceBindAddress: serviceBindAdress,
ServiceBindHost: serviceBindHost,
ServicePort: int(serverPort),
ServiceKey: serviceKey,
TLSRootCert: tlsrootcert,
TLSCert: tlscert,
TLSKey: tlskey,
DatabaseBindAddress: databaseBindAdress,
DatabasePort: int(databasePort),
LoversEar: loversear,
Interval: int(interval),
IdentityEndpoint: identity,
}
}