-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
161 lines (150 loc) · 2.91 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ghost
import (
"github.com/gin-gonic/gin"
"github.com/limoxi/ghost/utils"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
var Config *config
type config struct {
GMap
Mode string
}
// setDefaultConfigData 默认配置
func (this *config) setDefaultConfigData() {
this.GMap = NewEmptyGMap()
}
// findConfDir 查找配置文件夹conf
// 1、在当前工作目录下查找
// 2、逐级向上查找,查找深度默认3
func findConfDir(p string, args ...int) string {
count := 0
switch len(args) {
case 1:
count = args[0]
}
if count >= 3 {
return ""
}
ds, err := ioutil.ReadDir(p)
if err != nil {
panic(err)
}
for _, d := range ds {
if d.Name() == "conf" {
return p
}
}
p = path.Join(p, "..")
count += 1
return findConfDir(p, count)
}
// load
// 固定加载项目路径下conf文件夹中的yaml配置文件
func (this *config) load() {
workPath, err := os.Getwd()
if err != nil {
panic(err)
}
workPath = findConfDir(workPath)
if workPath == "" {
this.setDefaultConfigData()
return
}
pre := "prod"
if this.Mode == gin.DebugMode {
pre = "dev"
}
filename := pre + ".conf.yaml"
confPath := filepath.Join(workPath, "conf", filename)
if utils.FileExist(confPath) {
if err := this.parseYamlFile(confPath); err == nil {
return
}
} else {
confPath = filepath.Join(workPath, "conf", "conf.yaml")
if utils.FileExist(confPath) {
if err := this.parseYamlFile(confPath); err == nil {
return
}
}
}
this.setDefaultConfigData()
}
func (this *config) parseYamlFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
Warn(err)
return err
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
Warn(err)
return err
}
var data Map
err = yaml.Unmarshal(content, &data)
if err != nil {
Warn(err)
return err
}
this.GMap = NewGMapFromData(parseEnvArgs(data))
return nil
}
// parseEnvArgs 从字符串中解析出环境变量
func parseEnvArgs(data Map) Map {
for k, v := range data {
switch v.(type) {
case string:
data[k] = parseEnvFromString(v.(string))
case Map:
ns := k + "."
for ik, iv := range parseEnvArgs(v.(Map)) {
fullKey := ns + ik
data[fullKey] = iv
}
}
}
return data
}
func parseEnvFromString(str string) string {
str = strings.Replace(str, " ", "", -1)
if !strings.HasPrefix(str, "${") {
return str
}
envV := ""
defaultV := ""
sps := strings.Split(str, "||")
if len(sps) == 2 {
defaultV = sps[1]
}
sqIndex := strings.Index(str, "}")
envKey := str[2:sqIndex]
if envKey != "" {
envV = os.Getenv(envKey)
if envV == "" {
envV = defaultV
}
}
return envV
}
func LoadConfigFromFile(path string) *config {
c := new(config)
c.parseYamlFile(path)
return c
}
func init() {
Config = new(config)
mode := os.Getenv("GIN_MODE")
if mode == "" {
mode = gin.DebugMode
}
Infof("loading config in %s mode...", mode)
Config.Mode = mode
Config.load()
}