/
config.go
71 lines (63 loc) · 1.65 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
package config
import (
"fmt"
"github.com/spf13/viper"
)
var _config *Config
var _viper *viper.Viper
type Config struct {
Server Server `json:"server,omitempty" yaml:"server,omitempty"`
CORS CORS `json:"cors,omitempty" yaml:"cors,omitempty"`
Jwt Jwt `json:"jwt,omitempty" yaml:"jwt,omitempty"`
Captcha Captcha `json:"captcha,omitempty" yaml:"captcha,omitempty"`
Log Log `json:"log,omitempty" yaml:"log,omitempty"`
DB map[string]DB `json:"db,omitempty" yaml:"db,omitempty"`
Cache map[string]Cache `json:"cache,omitempty" yaml:"cache,omitempty"`
File map[string]File `json:"file,omitempty" yaml:"file,omitempty"`
}
func Init(cfgFilePath ...string) {
_config = &Config{}
var configPath string
if len(cfgFilePath) == 0 {
configPath = "./config.yaml"
} else {
configPath = cfgFilePath[0]
}
v := viper.New()
v.SetConfigFile(configPath)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("读取 %s 配置文件错误: %s", configPath, err))
}
if err := v.Unmarshal(_config); err != nil {
panic(fmt.Errorf("解析 %s.yaml 配置文件错误: %s", configPath, err))
}
_viper = v
}
func Cfg(path string) interface{} {
return _viper.Get(path)
}
func ServerCfg() Server {
return _config.Server
}
func CORSCfg() CORS {
return _config.CORS
}
func JwtCfg() Jwt {
return _config.Jwt
}
func CaptchaCfg() Captcha {
return _config.Captcha
}
func LogCfg() Log {
return _config.Log
}
func DBCfg() map[string]DB {
return _config.DB
}
func CacheCfg() map[string]Cache {
return _config.Cache
}
func FileCfg() map[string]File {
return _config.File
}