mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-05 03:12:34 -06:00
start rmser
This commit is contained in:
69
config/config.go
Normal file
69
config/config.go
Normal file
@@ -0,0 +1,69 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user