You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.5 KiB
52 lines
1.5 KiB
2 years ago
|
package config
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"message-demo/global"
|
||
|
"message-demo/model"
|
||
|
"os"
|
||
|
"path"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/fsnotify/fsnotify"
|
||
|
|
||
|
"github.com/spf13/viper"
|
||
|
)
|
||
|
|
||
|
func InitialConfig() model.Config {
|
||
|
workPath, _ := os.Executable()
|
||
|
filePath := path.Dir(workPath)
|
||
|
filePath = filepath.Join(filePath, "/config/config.yml")
|
||
|
viper.SetConfigName("config")
|
||
|
viper.SetConfigType("yml")
|
||
|
viper.AddConfigPath("./config")
|
||
|
viper.AddConfigPath("../config")
|
||
|
viper.AddConfigPath(filePath)
|
||
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
||
|
log.Printf("[conf] 配置文件变动: %v\n", e.Name)
|
||
|
global.GLO_CONF_CH <- getConfig()
|
||
|
})
|
||
|
viper.WatchConfig()
|
||
|
err := viper.ReadInConfig()
|
||
|
if err != nil {
|
||
|
log.Fatalln("[conf] 未找到配置,请先添加配置...")
|
||
|
}
|
||
|
return getConfig()
|
||
|
}
|
||
|
|
||
|
func getConfig() model.Config {
|
||
|
var config model.Config
|
||
|
config.MsgEnabled = viper.GetBool("message.enabled")
|
||
|
config.MsgType = viper.GetString("message.type")
|
||
|
config.BarkUrl = viper.GetString("message.bark.url")
|
||
|
config.BarkKey = viper.GetString("message.bark.key")
|
||
|
config.MailHost = viper.GetString("message.mail.host")
|
||
|
config.MailProtocol = viper.GetString("message.mail.protocol")
|
||
|
config.MailPort = viper.GetInt("message.mail.port")
|
||
|
config.MailUser = viper.GetString("message.mail.username")
|
||
|
config.MailPwd = viper.GetString("message.mail.password")
|
||
|
config.MailFromName = viper.GetString("message.mail.from_name")
|
||
|
config.MailTo = viper.GetStringSlice("message.mail.to")
|
||
|
return config
|
||
|
}
|