package config import ( "time" "github.com/spf13/viper" ) // Config 全局配置 type Config struct { Server ServerConfig `mapstructure:"server"` Database DatabaseConfig `mapstructure:"database"` Redis RedisConfig `mapstructure:"redis"` Security SecurityConfig `mapstructure:"security"` JWT JWTConfig `mapstructure:"jwt"` Notify NotifyConfig `mapstructure:"notify"` Reconciliation ReconciliationConfig `mapstructure:"reconciliation"` Merchant MerchantConfig `mapstructure:"merchant"` Log LogConfig `mapstructure:"log"` Channels ChannelsConfig `mapstructure:"channels"` } // ChannelsConfig 各渠道网关地址配置 type ChannelsConfig struct { Heepay HeepayURLConfig `mapstructure:"heepay"` } // HeepayURLConfig 汇元网关地址 type HeepayURLConfig struct { PayURL string `mapstructure:"pay_url"` // 支付网关 MerchantURL string `mapstructure:"merchant_url"` // 进件网关 } type ServerConfig struct { Port int `mapstructure:"port"` ReadTimeout time.Duration `mapstructure:"read_timeout"` WriteTimeout time.Duration `mapstructure:"write_timeout"` } type DatabaseConfig struct { DSN string `mapstructure:"dsn"` MaxOpenConns int `mapstructure:"max_open_conns"` MaxIdleConns int `mapstructure:"max_idle_conns"` ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"` } type RedisConfig struct { Addr string `mapstructure:"addr"` Password string `mapstructure:"password"` DB int `mapstructure:"db"` PoolSize int `mapstructure:"pool_size"` } type SecurityConfig struct { FieldEncryptKey string `mapstructure:"field_encrypt_key"` AppSecretSalt string `mapstructure:"app_secret_salt"` } type JWTConfig struct { Secret string `mapstructure:"secret"` ExpireHours int `mapstructure:"expire_hours"` } type NotifyConfig struct { PollerInterval time.Duration `mapstructure:"poller_interval"` PollerBatch int `mapstructure:"poller_batch"` HTTPTimeout time.Duration `mapstructure:"http_timeout"` } type ReconciliationConfig struct { Cron string `mapstructure:"cron"` BillRetryTimes int `mapstructure:"bill_retry_times"` } type MerchantConfig struct { StatusCheckCron string `mapstructure:"status_check_cron"` } type LogConfig struct { Level string `mapstructure:"level"` Format string `mapstructure:"format"` } // Load 加载配置文件 func Load(cfgFile string) (*Config, error) { v := viper.New() if cfgFile != "" { v.SetConfigFile(cfgFile) } else { v.SetConfigName("config.local") v.SetConfigType("yaml") v.AddConfigPath("./configs") v.AddConfigPath(".") } v.AutomaticEnv() if err := v.ReadInConfig(); err != nil { return nil, err } var cfg Config if err := v.Unmarshal(&cfg); err != nil { return nil, err } return &cfg, nil }