package config import ( "fmt" "strings" "github.com/spf13/viper" ) type Config struct { App AppConfig DB DBConfig Redis RedisConfig RMS RMSConfig OCR OCRConfig Telegram TelegramConfig Security SecurityConfig } type AppConfig struct { Port string `mapstructure:"port"` Mode string `mapstructure:"mode"` // debug/release DropTables bool `mapstructure:"drop_tables"` StoragePath string `mapstructure:"storage_path"` PublicURL string `mapstructure:"public_url"` } type DBConfig struct { DSN string `mapstructure:"dsn"` } type RedisConfig struct { Addr string `mapstructure:"addr"` Password string `mapstructure:"password"` DB int `mapstructure:"db"` } type RMSConfig struct { BaseURL string `mapstructure:"base_url"` Login string `mapstructure:"login"` Password string `mapstructure:"password"` // Исходный пароль, хеширование будет в клиенте } type OCRConfig struct { ServiceURL string `mapstructure:"service_url"` } type TelegramConfig struct { Token string `mapstructure:"token"` AdminIDs []int64 `mapstructure:"admin_ids"` WebAppURL string `mapstructure:"web_app_url"` } type SecurityConfig struct { SecretKey string `mapstructure:"secret_key"` // 32 bytes for AES-256 } // LoadConfig загружает конфигурацию из файла и переменных окружения func LoadConfig(path string) (*Config, error) { viper.AddConfigPath(path) viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) if err := viper.ReadInConfig(); err != nil { return nil, fmt.Errorf("ошибка чтения конфига: %w", err) } var cfg Config if err := viper.Unmarshal(&cfg); err != nil { return nil, fmt.Errorf("ошибка анмаршалинга конфига: %w", err) } return &cfg, nil }