study_xxqg/lib/config.go

81 lines
2.0 KiB
Go
Raw Normal View History

2021-11-12 07:46:33 +00:00
package lib
import (
_ "embed"
"os"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Config struct {
Model int `json:"model" yaml:"model"`
LogLevel string `json:"log_level" yaml:"log_level"`
ShowBrowser bool `json:"show_browser" yaml:"show_browser"`
Scheme string `json:"scheme" yaml:"scheme"`
Push struct {
Ding struct {
Enable bool `json:"enable" yaml:"enable"`
AccessToken string `json:"access_token" yaml:"access_token"`
Secret string `json:"secret" yaml:"secret"`
} `json:"ding" yaml:"ding"`
PushPlus struct {
Enable bool `json:"enable" yaml:"enable"`
Token string `json:"token" yaml:"token"`
} `json:"push_plus" yaml:"push_plus"`
} `json:"push" yaml:"push"`
2021-11-27 08:24:24 +00:00
TG struct {
Enable bool `json:"enable" yaml:"enable"`
Token string `json:"token" yaml:"token"`
ChatID int64 `json:"chat_id" yaml:"chat_id"`
Proxy string `json:"proxy" yaml:"proxy"`
} `json:"tg" yaml:"tg"`
2022-02-16 07:10:38 +00:00
Web struct {
Account string `json:"account"`
Password string `json:"password"`
Host string `json:"host"`
Port int `json:"port"`
} `json:"web"`
Cron string `json:"cron"`
2021-11-12 07:46:33 +00:00
}
var (
config = Config{
Model: 1,
}
)
//go:embed config_default.yml
var defaultConfig []byte
2022-02-16 07:10:38 +00:00
// GetConfig
/**
* @Description:
* @return Config
*/
2021-11-12 07:46:33 +00:00
func GetConfig() Config {
file, err := os.ReadFile("./config/config.yml")
if err != nil {
log.Warningln("检测到配置文件可能不存在")
err := os.WriteFile("./config/config.yml", defaultConfig, 0666)
if err != nil {
log.Errorln("写入到配置文件出现错误")
return Config{}
}
log.Infoln("成功写入到配置文件,请重启应用")
os.Exit(3)
}
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Errorln(err.Error())
return Config{}
}
2021-12-24 03:47:45 +00:00
if config.ShowBrowser {
log.Infoln("浏览器无头模式已禁用")
}
if config.Scheme == "" {
config.Scheme = "https://johlanse.github.io/study_xxqg/scheme.html?"
}
2021-11-12 07:46:33 +00:00
return config
}