Files
rmser/config/config.go

86 lines
2.3 KiB
Go

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
YooKassa YooKassaConfig `mapstructure:"yookassa"`
}
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"`
MaintenanceMode bool `mapstructure:"maintenance_mode"`
DevIDs []int64 `mapstructure:"dev_ids"` // Whitelist для режима разработки
}
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
}
type YooKassaConfig struct {
ShopID string `mapstructure:"shop_id"`
SecretKey string `mapstructure:"secret_key"`
}
// 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
}