package config import ( "fmt" "github.com/spf13/viper" ) type Config struct { App AppConfig `mapstructure:"app"` Server ServerConfig `mapstructure:"server"` Database DatabaseConfig `mapstructure:"database"` Storage StorageConfig `mapstructure:"storage"` AI AIConfig `mapstructure:"ai"` } type AppConfig struct { Name string `mapstructure:"name"` Version string `mapstructure:"version"` Debug bool `mapstructure:"debug"` } type ServerConfig struct { Port int `mapstructure:"port"` Host string `mapstructure:"host"` CORSOrigins []string `mapstructure:"cors_origins"` ReadTimeout int `mapstructure:"read_timeout"` WriteTimeout int `mapstructure:"write_timeout"` } type DatabaseConfig struct { Type string `mapstructure:"type"` // sqlite, mysql Path string `mapstructure:"path"` // SQLite数据库文件路径 Host string `mapstructure:"host"` Port int `mapstructure:"port"` User string `mapstructure:"user"` Password string `mapstructure:"password"` Database string `mapstructure:"database"` Charset string `mapstructure:"charset"` MaxIdle int `mapstructure:"max_idle"` MaxOpen int `mapstructure:"max_open"` } type StorageConfig struct { Type string `mapstructure:"type"` // local, minio LocalPath string `mapstructure:"local_path"` // 本地存储路径 BaseURL string `mapstructure:"base_url"` // 访问URL前缀 } type AIConfig struct { DefaultTextProvider string `mapstructure:"default_text_provider"` DefaultImageProvider string `mapstructure:"default_image_provider"` DefaultVideoProvider string `mapstructure:"default_video_provider"` } func LoadConfig() (*Config, error) { viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath("./configs") viper.AddConfigPath(".") viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { return nil, fmt.Errorf("failed to read config: %w", err) } var config Config if err := viper.Unmarshal(&config); err != nil { return nil, fmt.Errorf("failed to unmarshal config: %w", err) } return &config, nil } func (c *DatabaseConfig) DSN() string { if c.Type == "sqlite" { return c.Path } // MySQL DSN return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", c.User, c.Password, c.Host, c.Port, c.Database, c.Charset, ) }