62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Server ServerConfig `yaml:"server"`
|
|
Database DatabaseConfig `yaml:"database"`
|
|
Youdao YoudaoConfig `yaml:"youdao"`
|
|
Audio AudioConfig `yaml:"audio"`
|
|
}
|
|
|
|
type ServerConfig struct {
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
Host string `yaml:"host"`
|
|
Port int `yaml:"port"`
|
|
User string `yaml:"user"`
|
|
Password string `yaml:"password"`
|
|
Name string `yaml:"name"`
|
|
Charset string `yaml:"charset"`
|
|
}
|
|
|
|
type YoudaoConfig struct {
|
|
AppID string `yaml:"app_id"`
|
|
AppKey string `yaml:"app_key"` // 兼容旧字段
|
|
AppSecret string `yaml:"app_secret"`
|
|
}
|
|
|
|
type AudioConfig struct {
|
|
Path string `yaml:"path"`
|
|
}
|
|
|
|
func (d *DatabaseConfig) DSN() string {
|
|
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
|
d.User, d.Password, d.Host, d.Port, d.Name, d.Charset)
|
|
}
|
|
|
|
var AppConfig *Config
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
AppConfig = &cfg
|
|
return &cfg, nil
|
|
}
|