/
app_conf.go
55 lines (46 loc) · 872 Bytes
/
app_conf.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
package configs
import (
"fmt"
"io/ioutil"
"sync"
"gopkg.in/yaml.v2"
)
type AppConf struct {
Name string `yaml:"name"`
DbList map[string]*DbConf `yaml:"db"`
Baidu Baidu `yaml:"baidu"`
Upload Upload `yaml:"upload"`
}
type Upload struct {
Path string `yaml:"path"`
}
type Baidu struct {
Ak string `yaml:"ak"`
Sk string `yaml:"sk"`
}
type DbConf struct {
Dsn string `yaml:"dsn"`
}
var (
appConfig *AppConf
onceApp sync.Once
)
func GetAppConf() *AppConf {
initAppConf()
return appConfig
}
// initAppConf 初始化配置
func initAppConf() {
onceApp.Do(func() {
yamlFile, err := ioutil.ReadFile("./conf/app.yml")
if err != nil {
fmt.Println(err.Error())
}
var confTmp AppConf
err = yaml.Unmarshal(yamlFile, &confTmp)
if err != nil {
fmt.Println(err.Error())
}
appConfig = &confTmp
})
}