picgo/configs/configs.go

41 lines
815 B
Go
Raw Normal View History

2024-07-11 23:26:06 +08:00
package configs
import (
"gopkg.in/yaml.v3"
"log"
"os"
)
type Configs struct {
2024-07-12 20:32:33 +08:00
Server `yaml:"server"`
Redis `yaml:"redis"`
Mysql `yaml:"mysql"`
Captcha `json:"captcha"`
2024-07-11 23:26:06 +08:00
}
// Settings 定义配置结构体
var Settings Configs
func readYamlFile(configFilePath string) (yamlFile []byte, err error) {
// 读取YAML文件
if yamlFile, err = os.ReadFile(configFilePath); err != nil {
return nil, err
}
return yamlFile, err
}
func NewConfig(configFilePath string) {
var (
yamlFile []byte
err error
)
// 读取配置文件
if yamlFile, err = readYamlFile(configFilePath); err != nil {
log.Panic("读取配置文件失败: ", err)
}
// 解析YAML数据到配置结构体
if err = yaml.Unmarshal(yamlFile, &Settings); err != nil {
log.Panic("解析配置文件失败: ", err)
}
}