Files
huangjin fe3ad20fe2 'init'
2026-06-29 17:34:59 +08:00

122 lines
3.4 KiB
Go

// 使用 Viper 从 YAML 文件加载配置
package config
import (
"fmt"
"github.com/spf13/viper"
)
// Config 全局配置,聚合所有子模块配置
type Config struct {
Server ServerConfig `mapstructure:"server"`
Database DatabaseConfig `mapstructure:"database"`
Redis RedisConfig `mapstructure:"redis"`
MinIO MinIOConfig `mapstructure:"minio"`
RabbitMQ RabbitMQConfig `mapstructure:"rabbitmq"`
Wechat WechatConfig `mapstructure:"wechat"`
DingTalk DingTalkConfig `mapstructure:"dingtalk"`
JWT JWTConfig `mapstructure:"jwt"`
Snowflake SnowflakeConfig `mapstructure:"snowflake"`
System SystemConfig `mapstructure:"system"`
}
type ServerConfig struct {
Port int `mapstructure:"port"`
Mode string `mapstructure:"mode"` // debug / release / test
}
type DatabaseConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DBName string `mapstructure:"dbname"`
SSLMode string `mapstructure:"sslmode"`
Timezone string `mapstructure:"timezone"`
}
type RedisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
type MinIOConfig struct {
Endpoint string `mapstructure:"endpoint"`
AccessKey string `mapstructure:"access_key"`
SecretKey string `mapstructure:"secret_key"`
Bucket string `mapstructure:"bucket"`
UseSSL bool `mapstructure:"use_ssl"`
BaseURL string `mapstructure:"base_url"`
}
type RabbitMQConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
VHost string `mapstructure:"vhost"`
}
type WechatConfig struct {
AppID string `mapstructure:"app_id"`
AppSecret string `mapstructure:"app_secret"`
Token string `mapstructure:"token"`
EncodingAESKey string `mapstructure:"encoding_aes_key"`
}
type JWTConfig struct {
Secret string `mapstructure:"secret"`
ExpireHours int `mapstructure:"expire_hours"`
}
type SnowflakeConfig struct {
WorkerID int64 `mapstructure:"worker_id"`
DatacenterID int64 `mapstructure:"datacenter_id"`
}
// DingTalkConfig 钉钉应用配置
type DingTalkConfig struct {
AppKey string `mapstructure:"app_key"`
AppSecret string `mapstructure:"app_secret"`
RobotCode string `mapstructure:"robot_code"`
}
type SystemConfig struct {
DefaultAvatar string `mapstructure:"default_avatar"`
}
// DSN 生成 PostgreSQL 连接字符串
func (d *DatabaseConfig) DSN() string {
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
d.Host, d.User, d.Password, d.DBName, d.Port, d.SSLMode, d.Timezone)
}
func (r *RedisConfig) Addr() string {
return fmt.Sprintf("%s:%d", r.Host, r.Port)
}
func (r *RabbitMQConfig) AMQPURL() string {
return fmt.Sprintf("amqp://%s:%s@%s:%d/%s", r.User, r.Password, r.Host, r.Port, r.VHost)
}
// LoadConfig 从 YAML 文件加载配置
func LoadConfig(configPath string) (*Config, error) {
v := viper.New()
v.SetConfigFile(configPath)
v.SetConfigType("yaml")
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("failed to read config file: %w", err)
}
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
return &cfg, nil
}