aiweek-reconstruction/option/option.go

78 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package option
import (
"gopkg.in/yaml.v2"
"io"
"log"
"os"
)
//自定义选项
type Option struct {
UDesk struct {
Filters []struct {
Filter7D string `yaml:"filter7d,omitempty"`
Filter14D string `yaml:"filter14d,omitempty"`
} `yaml:"filters"`
Macros []string `yaml:"macros"`
} `yaml:"uDesk"`
VolumePath string `yaml:"volumePath"`
FileServer struct {
Address string `yaml:"address"`
} `yaml:"fileServer"`
ModelConf struct {
API string `yaml:"api"`
Token string `yaml:"token"`
} `yaml:"modelConf"`
Wechat struct {
Webhook string `yaml:"webhook"`
} `yaml:"wechat"`
}
// 新选项对象
func newOptionObj() *Option {
return &Option{}
}
func (o *Option) readYaml(yamlFile string) *Option {
file, err := os.Open(yamlFile)
if err != nil {
log.Printf("打开yaml文件错误错误是%v", err)
return nil
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
log.Printf("读取yaml文件错误错误是%v", err)
return nil
}
var option Option
err = yaml.Unmarshal(data, &option)
if err != nil {
log.Printf("yaml文件内容反序列化失败错误是%v", err)
return nil
}
return &option
}
func loadALLOption() *Option {
option := newOptionObj()
option = option.readYaml("/conf/option.yaml")
return option
}
var (
UDESK_FILTER7D = loadALLOption().UDesk.Filters[0].Filter7D
UDSK_FILTER14D = loadALLOption().UDesk.Filters[1].Filter14D
UDESK_MACROS = loadALLOption().UDesk.Macros
FILESERVER_ADDRESS = loadALLOption().FileServer.Address
VOLUMEPATH = loadALLOption().VolumePath
MODEL_API = loadALLOption().ModelConf.API
MODEL_TOKEN = loadALLOption().ModelConf.Token
WECHAT_WEBHOOK = loadALLOption().Wechat.Webhook
)