-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (70 loc) · 1.69 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
85
package config
import (
"os"
"path"
"strconv"
"strings"
"github.com/spf13/viper"
)
const (
DEFAULT_CONFIG_PATH = "conf"
DEFAULT_CONFIG_NAME = "zero-framework"
DEFAULT_CONFIG_TYPE = "yaml"
)
var (
_servAbsPath string
_aViper *viper.Viper
)
func cfgString(aViper *viper.Viper, cfgName string) string {
envName := strings.ToUpper(strings.Replace(cfgName, ".", "_", -1))
envValue := os.Getenv(envName)
if len(envValue) > 0 {
return envValue
}
return aViper.GetString(cfgName)
}
func cfgInt(aViper *viper.Viper, cfgName string) int {
envName := strings.ToUpper(strings.Replace(cfgName, ".", "_", -1))
envValue := os.Getenv(envName)
if len(envValue) > 0 {
v, err := strconv.Atoi(envValue)
if err != nil {
panic(err)
}
return v
}
return aViper.GetInt(cfgName)
}
func cfgStringSlice(aViper *viper.Viper, cfgName string) []string {
envName := strings.ToUpper(strings.Replace(cfgName, ".", "_", -1))
envValue := os.Getenv(envName)
if len(envValue) > 0 {
return strings.Split(envValue, ",")
}
return aViper.GetStringSlice(cfgName)
}
func NewConfigs(sysAbsPath string) {
if len(_servAbsPath) > 0 {
return
}
_servAbsPath = sysAbsPath
_aViper = viper.New()
_aViper.AddConfigPath(path.Join(_servAbsPath, DEFAULT_CONFIG_PATH))
_aViper.SetConfigName(DEFAULT_CONFIG_NAME)
_aViper.SetConfigType(DEFAULT_CONFIG_TYPE)
if err := _aViper.ReadInConfig(); err != nil {
panic(err)
}
}
func ServerAbsPath() string {
return _servAbsPath
}
func StringValue(cfgName string) string {
return cfgString(_aViper, cfgName)
}
func IntValue(cfgName string) int {
return cfgInt(_aViper, cfgName)
}
func SliceStringValue(cfgName string) []string {
return cfgStringSlice(_aViper, cfgName)
}