-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
executable file
·114 lines (104 loc) · 2.92 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
package cfg
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type Config struct {
Name string
}
func Get_Info_Int(get_type string) int {
switch get_type {
case "allkeyfornum":
rediscfg_allkeyfornum := viper.GetInt("rediscfg.allkeyfornum")
return rediscfg_allkeyfornum
case "locktime":
rediscfg_locktime := viper.GetInt("rediscfg.locktime")
return rediscfg_locktime
case "biglocktime":
rediscfg_biglocktime := viper.GetInt("rediscfg.biglocktime")
return rediscfg_biglocktime
case "checksize":
rediscfg_checksize := viper.GetInt("rediscfg.checksize")
return rediscfg_checksize
default:
return 0
}
}
func Get_Info_String(get_type string) string {
switch get_type {
case "MYSQL":
mysql_name := viper.GetString("mysql.name")
mysql_addr := viper.GetString("mysql.addr")
mysql_username := viper.GetString("mysql.username")
mysql_password := viper.GetString("mysql.password")
mysql_url := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local", mysql_username, mysql_password, mysql_addr, mysql_name)
return mysql_url
case "REDIS":
redis_addr := viper.GetString("redis.addr")
redis_port := viper.GetString("redis.port")
redis_url := fmt.Sprintf("%s:%s", redis_addr, redis_port)
return redis_url
case "redispw":
local_redispw := viper.GetString("redis.password")
return local_redispw
case "addr":
local_addr := viper.GetString("local.addr")
return local_addr
case "logapipath":
local_logapipath := viper.GetString("local.logapipath")
return local_logapipath
case "logapppath":
local_logapppath := viper.GetString("local.logapppath")
return local_logapppath
case "secretkey":
rediscfg_secretkey := viper.GetString("local.secretkey")
return rediscfg_secretkey
case "cosaccesskey":
cos_cosaccesskey := viper.GetString("cos.cosaccesskey")
return cos_cosaccesskey
case "cosaccesskeyid":
cos_cosaccesskeyid := viper.GetString("cos.cosaccesskeyid")
return cos_cosaccesskeyid
case "cosendpointpub":
cos_cosendpointpub := viper.GetString("cos.cosendpointpub")
return cos_cosendpointpub
default:
return "noconfig"
}
}
func Init(cfg string) error {
c := Config{
Name: cfg,
}
// 初始化配置文件
if err := c.initConfig(); err != nil {
return err
}
c.watchConfig()
return nil
}
func (c *Config) initConfig() error {
if c.Name != "" {
// 如果指定了配置文件,则解析指定的配置文件
viper.SetConfigFile(c.Name)
} else {
// 如果没有指定配置文件,则解析默认的配置文件
viper.AddConfigPath("yaml")
viper.SetConfigName("config")
}
// 设置配置文件格式为YAML
viper.SetConfigType("yaml")
// viper解析配置文件
if err := viper.ReadInConfig(); err != nil {
return err
}
return nil
}
// 监听配置文件是否改变,用于热更新
func (c *Config) watchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Printf("Config file changed: %s\n", e.Name)
})
}