/
config.go
58 lines (44 loc) · 1.22 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
package config
import (
"github.com/spf13/viper"
"strings"
)
func setDefaults() {
viper.SetDefault("config.app.path", "./app.yml")
viper.SetDefault("config.discovery.path", "./discovery.yml")
}
func GetString(key string) string {
return viper.GetString(key)
}
func GetInt(key string) int {
return viper.GetInt(key)
}
func GetIntStrict(key string) int {
return viper.GetInt(key)
}
func GetStringStrict(key string) string {
return viper.GetString(key)
}
func GetStringSlice(key string) []string {
return viper.GetStringSlice(key)
}
func InitConfigurations() error {
var err error
// lets environment variable override config file
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
setDefaults()
// Load discovery configurations
viper.SetConfigFile(viper.GetString("config.discovery.path"))
err = viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return err
}
// Load app specific configurations
viper.SetConfigFile(viper.GetString("config.app.path"))
err = viper.MergeInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return err
}
return nil
}