/
config.go
52 lines (43 loc) · 900 Bytes
/
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
package gibson
import (
"fmt"
"gibson/utils"
"github.com/spf13/viper"
"reflect"
)
// TODO: add application config
var (
Configs map[string]string = map[string]string{
"routes": "conf/",
}
config *ConfigStruct
)
type ConfigStruct struct {
Routes *viper.Viper
}
func GetConfigInstance() *ConfigStruct {
if config == nil {
config = newConfig()
}
return config
}
func newConfig() *ConfigStruct {
config := &ConfigStruct{}
e := reflect.ValueOf(config).Elem()
for kind, path := range Configs {
v := reflect.ValueOf(createConfigObject(kind, path))
e.FieldByName(utils.Capitalize(kind)).Set(v)
}
return config
}
func createConfigObject(kind, path string) *viper.Viper {
v := viper.New()
v.SetConfigType("yaml")
v.SetConfigName(kind)
v.AddConfigPath(path)
err := v.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
return v
}