mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-04 19:02:33 -06:00
71 lines
1.8 KiB
Go
71 lines
1.8 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
|
|
}
|
|
|
|
type AppConfig struct {
|
|
Port string `mapstructure:"port"`
|
|
Mode string `mapstructure:"mode"` // debug/release
|
|
DropTables bool `mapstructure:"drop_tables"`
|
|
}
|
|
|
|
type DBConfig struct {
|
|
DSN string `mapstructure:"dsn"`
|
|
}
|
|
|
|
type RedisConfig struct {
|
|
Addr string `mapstructure:"addr"`
|
|
Password string `mapstructure:"password"`
|
|
DB int `mapstructure:"db"`
|
|
}
|
|
|
|
type OCRConfig struct {
|
|
ServiceURL string `mapstructure:"service_url"`
|
|
}
|
|
|
|
type RMSConfig struct {
|
|
BaseURL string `mapstructure:"base_url"`
|
|
Login string `mapstructure:"login"`
|
|
Password string `mapstructure:"password"` // Исходный пароль, хеширование будет в клиенте
|
|
}
|
|
|
|
type TelegramConfig struct {
|
|
Token string `mapstructure:"token"`
|
|
AdminIDs []int64 `mapstructure:"admin_ids"`
|
|
WebAppURL string `mapstructure:"web_app_url"`
|
|
}
|
|
|
|
// 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
|
|
}
|