2022-07-27 10:21:49 +00:00
|
|
|
|
package conf
|
2021-11-12 07:46:33 +00:00
|
|
|
|
|
|
|
|
|
import (
|
2022-09-06 14:56:18 +00:00
|
|
|
|
"bytes"
|
2021-11-12 07:46:33 +00:00
|
|
|
|
_ "embed"
|
|
|
|
|
"os"
|
2022-08-27 05:12:12 +00:00
|
|
|
|
"strings"
|
2021-11-12 07:46:33 +00:00
|
|
|
|
|
2022-08-27 05:12:12 +00:00
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2021-11-12 07:46:33 +00:00
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-27 05:12:12 +00:00
|
|
|
|
"github.com/spf13/viper"
|
2022-09-22 12:17:06 +00:00
|
|
|
|
"gopkg.in/yaml.v3"
|
2021-11-12 07:46:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-04-20 13:31:46 +00:00
|
|
|
|
// Config
|
|
|
|
|
// @Description:
|
|
|
|
|
//
|
2021-11-12 07:46:33 +00:00
|
|
|
|
type Config struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Model int `json:"model" yaml:"model" mapstructure:"model"`
|
|
|
|
|
LogLevel string `json:"log_level" yaml:"log_level" mapstructure:"log_level"`
|
|
|
|
|
ShowBrowser bool `json:"show_browser" yaml:"show_browser" mapstructure:"show_browser"`
|
|
|
|
|
Scheme string `json:"scheme" yaml:"scheme" mapstructure:"scheme"`
|
2021-11-17 09:39:48 +00:00
|
|
|
|
Push struct {
|
|
|
|
|
Ding struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
AccessToken string `json:"access_token" yaml:"access_token" mapstructure:"access_token"`
|
|
|
|
|
Secret string `json:"secret" yaml:"secret" mapstructure:"secret"`
|
|
|
|
|
} `json:"ding" yaml:"ding" mapstructure:"ding"`
|
2021-11-22 12:07:55 +00:00
|
|
|
|
PushPlus struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
Token string `json:"token" yaml:"token" mapstructure:"token"`
|
2022-10-30 14:41:22 +00:00
|
|
|
|
Topic string `json:"topic" yaml:"topic" mapstructure:"topic"`
|
2022-08-27 05:12:12 +00:00
|
|
|
|
} `json:"push_plus" yaml:"push_plus" mapstructure:"push_plus"`
|
|
|
|
|
} `json:"push" yaml:"push" mapstructure:"push"`
|
2021-11-27 08:24:24 +00:00
|
|
|
|
TG struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
Token string `json:"token" yaml:"token" mapstructure:"token"`
|
|
|
|
|
ChatID int64 `json:"chat_id" yaml:"chat_id" mapstructure:"chat_id"`
|
|
|
|
|
Proxy string `json:"proxy" yaml:"proxy" mapstructure:"proxy"`
|
|
|
|
|
CustomApi string `json:"custom_api" yaml:"custom_api" mapstructure:"custom_api"`
|
|
|
|
|
WhiteList []int64 `json:"white_list" yaml:"white_list" mapstructure:"white_list"`
|
|
|
|
|
} `json:"tg" yaml:"tg" mapstructure:"tg"`
|
2022-02-16 07:10:38 +00:00
|
|
|
|
Web struct {
|
2022-08-30 07:13:09 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
Account string `json:"account" yaml:"account" mapstructure:"account"`
|
|
|
|
|
Password string `json:"password" yaml:"password" mapstructure:"password"`
|
|
|
|
|
Host string `json:"host" yaml:"host" mapstructure:"host"`
|
|
|
|
|
Port int `json:"port" yaml:"port" mapstructure:"port"`
|
2022-09-06 14:56:18 +00:00
|
|
|
|
CommonUser map[string]string `json:"common_user" yaml:"common_user" mapstructure:"common_user"`
|
|
|
|
|
} `json:"web" yaml:"web" mapstructure:"web"`
|
2022-11-08 12:22:58 +00:00
|
|
|
|
QQ struct {
|
2022-11-08 12:28:37 +00:00
|
|
|
|
Enable bool `json:"enable" mapstructure:"enable"`
|
|
|
|
|
PostAddr string `json:"post_addr" mapstructure:"post_addr"`
|
|
|
|
|
SuperUser int64 `json:"super_user" mapstructure:"super_user"`
|
|
|
|
|
WhiteList []int64 `json:"white_list" mapstructure:"white_list"`
|
|
|
|
|
AccessToken string `json:"access_token" mapstructure:"access_token"`
|
2022-11-08 12:22:58 +00:00
|
|
|
|
}
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Cron string `json:"cron" yaml:"cron" mapstructure:"cron"`
|
|
|
|
|
CronRandomWait int `json:"cron_random_wait" yaml:"cron_random_wait" mapstructure:"cron_random_wait"`
|
|
|
|
|
EdgePath string `json:"edge_path" yaml:"edge_path" mapstructure:"edge_path"`
|
|
|
|
|
StartWait int `json:"start_wait" yaml:"start_wait" mapstructure:"start_wait"`
|
2022-05-03 11:35:14 +00:00
|
|
|
|
// cookie强制过期时间,单位为h
|
2022-08-27 05:12:12 +00:00
|
|
|
|
ForceExpiration int `json:"force_expiration" yaml:"force_expiration" mapstructure:"force_expiration"`
|
2022-05-03 11:35:14 +00:00
|
|
|
|
Retry struct {
|
|
|
|
|
// 重试次数
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Times int `json:"times" yaml:"times" mapstructure:"times"`
|
2022-05-03 11:35:14 +00:00
|
|
|
|
// 重试时间
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Intervals int `json:"intervals" yaml:"intervals" mapstructure:"intervals"`
|
|
|
|
|
} `json:"retry" yaml:"retry" mapstructure:"retry"`
|
2022-07-30 10:55:00 +00:00
|
|
|
|
|
|
|
|
|
Wechat struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
Token string `json:"token" yaml:"token" mapstructure:"token"`
|
|
|
|
|
Secret string `json:"secret" yaml:"secret" mapstructure:"secret"`
|
|
|
|
|
AppID string `json:"app_id" yaml:"app_id" mapstructure:"app_id"`
|
|
|
|
|
LoginTempID string `json:"login_temp_id" yaml:"login_temp_id" mapstructure:"login_temp_id"`
|
|
|
|
|
NormalTempID string `json:"normal_temp_id" yaml:"normal_temp_id" mapstructure:"normal_temp_id"`
|
|
|
|
|
PushLoginWarn bool `json:"push_login_warn" yaml:"push_login_warn" mapstructure:"push_login_warn"`
|
|
|
|
|
SuperOpenID string `json:"super_open_id" yaml:"super_open_id" mapstructure:"super_open_id"`
|
|
|
|
|
} `json:"wechat" yaml:"wechat" mapstructure:"wechat"`
|
2022-05-27 12:20:15 +00:00
|
|
|
|
// 专项答题可接受的最小值
|
2022-08-27 05:12:12 +00:00
|
|
|
|
SpecialMinScore int `json:"special_min_score" yaml:"special_min_score" mapstructure:"special_min_score"`
|
2022-07-24 07:56:12 +00:00
|
|
|
|
|
2022-08-18 08:18:06 +00:00
|
|
|
|
PushDeer struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
Api string `json:"api" yaml:"api" mapstructure:"api"`
|
|
|
|
|
Token string `json:"token" yaml:"token" mapstructure:"token"`
|
|
|
|
|
} `json:"push_deer" yaml:"push_deer" mapstructure:"push_deer"`
|
2022-08-18 08:18:06 +00:00
|
|
|
|
|
2022-08-27 05:12:12 +00:00
|
|
|
|
ReverseOrder bool `json:"reverse_order" yaml:"reverse_order" mapstructure:"reverse_order"`
|
2022-08-02 01:32:15 +00:00
|
|
|
|
|
2022-07-24 07:56:12 +00:00
|
|
|
|
JiGuangPush struct {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`
|
|
|
|
|
Secret string `json:"secret" yaml:"secret" mapstructure:"secret"`
|
|
|
|
|
AppKey string `json:"app_key" yaml:"app_key" mapstructure:"app_key"`
|
|
|
|
|
} `json:"ji_guang_push" yaml:"ji_guang_push" mapstructure:"ji_guang_push"`
|
2022-08-05 10:08:05 +00:00
|
|
|
|
|
2022-08-26 15:00:28 +00:00
|
|
|
|
// github的代理地址,用于检查更新或者其他的
|
2022-08-27 05:12:12 +00:00
|
|
|
|
GithubProxy string `json:"github_proxy" yaml:"github_proxy" mapstructure:"github_proxy"`
|
2022-08-30 07:13:09 +00:00
|
|
|
|
// 热重载
|
2022-08-27 05:12:12 +00:00
|
|
|
|
HotReload bool `json:"hot_reload" yaml:"hot_reload" mapstructure:"hot_reload"`
|
2022-08-26 15:00:28 +00:00
|
|
|
|
|
2022-09-03 12:01:51 +00:00
|
|
|
|
// 自定义消息推送
|
|
|
|
|
CustomMessage string `json:"custom_message" yaml:"custom_message" mapstructure:"custom_message"`
|
|
|
|
|
|
|
|
|
|
CustomCron string `json:"custom_cron" yaml:"custom_cron" mapstructure:"custom_cron"`
|
|
|
|
|
|
2022-10-17 14:10:59 +00:00
|
|
|
|
PoolSize int `json:"pool_size" yaml:"pool_size" mapstructure:"pool_size"`
|
|
|
|
|
|
2022-08-27 05:12:12 +00:00
|
|
|
|
version string `mapstructure:"version"`
|
2021-11-12 07:46:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
config = Config{
|
|
|
|
|
Model: 1,
|
|
|
|
|
}
|
2022-09-25 15:58:04 +00:00
|
|
|
|
|
|
|
|
|
configPath = "./config/config.yml"
|
2021-11-12 07:46:33 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed config_default.yml
|
|
|
|
|
var defaultConfig []byte
|
|
|
|
|
|
2022-08-12 01:17:23 +00:00
|
|
|
|
// SetVersion
|
|
|
|
|
/* @Description: 设置应用程序版本号
|
|
|
|
|
* @param string2
|
|
|
|
|
*/
|
2022-08-05 10:08:05 +00:00
|
|
|
|
func SetVersion(string2 string) {
|
|
|
|
|
config.version = string2
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 01:17:23 +00:00
|
|
|
|
// GetVersion
|
|
|
|
|
/* @Description: 获取应用程序版本号
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2022-08-05 10:08:05 +00:00
|
|
|
|
func GetVersion() string {
|
|
|
|
|
return config.version
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 14:56:18 +00:00
|
|
|
|
func SetConfig(config2 Config) error {
|
|
|
|
|
data, err := yaml.Marshal(&config2)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("不能正确解析配置文件" + err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = viper.ReadConfig(bytes.NewReader(data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("viper不能正确解析配置文件" + err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = viper.WriteConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("保存到文件失败" + err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-12 01:17:23 +00:00
|
|
|
|
// InitConfig
|
|
|
|
|
/* @Description: 初始化配置文件
|
|
|
|
|
* @param path
|
2022-02-16 07:10:38 +00:00
|
|
|
|
*/
|
2022-08-27 05:12:12 +00:00
|
|
|
|
func InitConfig(path string, restart func()) {
|
2022-08-12 01:17:23 +00:00
|
|
|
|
if path == "" {
|
|
|
|
|
path = "./config/config.yml"
|
|
|
|
|
}
|
2022-09-25 15:58:04 +00:00
|
|
|
|
configPath = path
|
2022-08-27 05:12:12 +00:00
|
|
|
|
pathDir := strings.TrimSuffix(path, "config.yml")
|
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
|
viper.AddConfigPath(pathDir)
|
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
|
log.Warningln("检测到配置文件可能不存在,即将写入默认配置")
|
|
|
|
|
err := os.WriteFile(path, defaultConfig, 0666)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorln("写入到配置文件出现错误")
|
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Infoln("成功写入到配置文件,即将重启应用")
|
|
|
|
|
restart()
|
|
|
|
|
} else {
|
|
|
|
|
log.Panicln("读取配置文件出现未知错误" + err.Error())
|
2021-11-12 07:46:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-27 05:12:12 +00:00
|
|
|
|
viper.SetDefault("scheme", "https://johlanse.github.io/study_xxqg/scheme.html?")
|
|
|
|
|
viper.SetDefault("special_min_score", 10)
|
|
|
|
|
viper.SetDefault("tg.custom_api", "https://api.telegram.org")
|
2022-10-17 14:10:59 +00:00
|
|
|
|
viper.SetDefault("pool_size", 1)
|
2022-09-25 15:58:04 +00:00
|
|
|
|
viper.AutomaticEnv()
|
2022-08-27 05:12:12 +00:00
|
|
|
|
err := viper.Unmarshal(&config, func(decoderConfig *mapstructure.DecoderConfig) {
|
|
|
|
|
|
|
|
|
|
})
|
2021-11-12 07:46:33 +00:00
|
|
|
|
if err != nil {
|
2022-08-27 05:12:12 +00:00
|
|
|
|
log.Panicln("解析配置文件出现错误" + err.Error())
|
|
|
|
|
return
|
2022-05-27 12:20:15 +00:00
|
|
|
|
}
|
2022-08-27 05:12:12 +00:00
|
|
|
|
if viper.GetBool("hot_reload") {
|
|
|
|
|
log.Infoln("程序已开启热重载!")
|
|
|
|
|
viper.WatchConfig()
|
|
|
|
|
viper.OnConfigChange(func(in fsnotify.Event) {
|
|
|
|
|
log.Infoln("检测到配置文件变化,即将重启程序")
|
|
|
|
|
restart()
|
|
|
|
|
})
|
2022-08-11 06:08:28 +00:00
|
|
|
|
}
|
2022-08-12 01:17:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetConfig
|
|
|
|
|
/**
|
|
|
|
|
* @Description: 获取配置信息
|
|
|
|
|
* @return Config
|
|
|
|
|
*/
|
|
|
|
|
func GetConfig() Config {
|
2021-11-12 07:46:33 +00:00
|
|
|
|
return config
|
|
|
|
|
}
|
2022-09-25 15:58:04 +00:00
|
|
|
|
|
|
|
|
|
// GetConfigFile
|
|
|
|
|
/* @Description:
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
func GetConfigFile() string {
|
|
|
|
|
file, err := os.ReadFile(configPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err.Error()
|
|
|
|
|
}
|
|
|
|
|
return string(file)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SaveConfigFile(data string) error {
|
|
|
|
|
err := os.WriteFile(configPath, []byte(data), 0666)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|