-
Notifications
You must be signed in to change notification settings - Fork 307
/
config.go
84 lines (74 loc) · 2.06 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
package config
import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"go.uber.org/zap"
"strconv"
"strings"
)
const (
LogBasePath string = "./log/"
WelcomeContent string = "欢迎使用E5SubBot!"
HelpContent string = `
命令:
/my 查看已绑定账户信息
/bind 绑定新账户
/unbind 解绑账户
/export 导出账户信息(JSON)
/help 帮助
源码及使用方法:https://github.com/iyear/E5SubBot
`
)
func Init() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
zap.S().Fatalw("failed to read config", "error", err)
}
BotToken = viper.GetString("bot_token")
Cron = viper.GetString("cron")
Socks5 = viper.GetString("socks5")
viper.SetDefault("errlimit", 5)
viper.SetDefault("bindmax", 5)
viper.SetDefault("goroutine", 10)
BindMaxNum = viper.GetInt("bindmax")
MaxErrTimes = viper.GetInt("errlimit")
Notice = viper.GetString("notice")
MaxGoroutines = viper.GetInt("goroutine")
Admins = getAdmins()
DB = viper.GetString("db")
Table = viper.GetString("table")
switch DB {
case "mysql":
Mysql = mysqlConfig{
Host: viper.GetString("mysql.host"),
Port: viper.GetInt("mysql.port"),
User: viper.GetString("mysql.user"),
Password: viper.GetString("mysql.password"),
DB: viper.GetString("mysql.database"),
SSLMode: viper.GetString("mysql.ssl_mode"),
EnabledTLSProtocols: viper.GetString("mysql.enabled_tls_protocols"),
}
case "sqlite":
Sqlite = sqliteConfig{
DB: viper.GetString("sqlite.db"),
}
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
MaxGoroutines = viper.GetInt("goroutine")
BindMaxNum = viper.GetInt("bindmax")
MaxErrTimes = viper.GetInt("errlimit")
Notice = viper.GetString("notice")
Admins = getAdmins()
})
}
func getAdmins() []int64 {
var result []int64
admins := strings.Split(viper.GetString("admin"), ",")
for _, v := range admins {
id, _ := strconv.ParseInt(v, 10, 64)
result = append(result, id)
}
return result
}