41 lines
815 B
Go
41 lines
815 B
Go
package configs
|
|
|
|
import (
|
|
"gopkg.in/yaml.v3"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Configs struct {
|
|
Server `yaml:"server"`
|
|
Redis `yaml:"redis"`
|
|
Mysql `yaml:"mysql"`
|
|
Captcha `json:"captcha"`
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|