Files
futriis/internal/config/config.go

100 lines
3.3 KiB
Go
Raw Normal View History

2026-04-08 21:43:35 +03:00
// Файл: internal/config/config.go
// Назначение: Загрузка и парсинг TOML-конфигурации, валидация параметров,
// предоставление доступа к настройкам кластера, хранилища и REPL.
package config
import (
"github.com/BurntSushi/toml"
)
type Config struct {
Cluster ClusterConfig `toml:"cluster"`
Storage StorageConfig `toml:"storage"`
Repl ReplConfig `toml:"repl"`
Log LogConfig `toml:"log"`
Replication ReplicationConfig `toml:"replication"`
Plugins PluginsConfig `toml:"plugins"`
Compression CompressionConfig `toml:"compression"`
}
type ClusterConfig struct {
Name string `toml:"name"`
NodeIP string `toml:"node_ip"`
NodePort int `toml:"node_port"`
RaftPort int `toml:"raft_port"`
RaftDataDir string `toml:"raft_data_dir"`
Bootstrap bool `toml:"bootstrap"` // Флаг бутстрапа кластера
Nodes []string `toml:"nodes"` // Список узлов кластера
}
type StorageConfig struct {
PageSizeMB int `toml:"page_size_mb"`
MaxCollections int `toml:"max_collections"`
MaxDocumentsPerCollection int `toml:"max_documents_per_collection"`
}
type ReplConfig struct {
PromptColor string `toml:"prompt_color"`
HistorySize int `toml:"history_size"`
}
type LogConfig struct {
LogFile string `toml:"log_file"`
LogLevel string `toml:"log_level"`
}
type ReplicationConfig struct {
Enabled bool `toml:"enabled"`
MasterMaster bool `toml:"master_master"`
SyncReplication bool `toml:"sync_replication"`
ReplicationTimeoutMs int `toml:"replication_timeout_ms"`
}
type PluginsConfig struct {
Enabled bool `toml:"enabled"`
ScriptDir string `toml:"script_dir"`
AllowList []string `toml:"allow_list"`
}
type CompressionConfig struct {
Enabled bool `toml:"enabled"` // Включено ли сжатие
Algorithm string `toml:"algorithm"` // Алгоритм сжатия (snappy, lz4, zstd)
Level int `toml:"level"` // Уровень сжатия (1-9, зависит от алгоритма)
MinSize int `toml:"min_size"` // Минимальный размер для сжатия (байт)
}
func LoadConfig(path string) (*Config, error) {
var cfg Config
if _, err := toml.DecodeFile(path, &cfg); err != nil {
return nil, err
}
// Установка значений по умолчанию, если не указаны
if cfg.Cluster.RaftPort == 0 {
cfg.Cluster.RaftPort = 9878
}
if cfg.Cluster.RaftDataDir == "" {
cfg.Cluster.RaftDataDir = "raft_data"
}
if cfg.Replication.ReplicationTimeoutMs == 0 {
cfg.Replication.ReplicationTimeoutMs = 5000
}
if cfg.Plugins.ScriptDir == "" {
cfg.Plugins.ScriptDir = "plugins"
}
// Установка значений по умолчанию для сжатия
if cfg.Compression.Algorithm == "" {
cfg.Compression.Algorithm = "snappy"
}
if cfg.Compression.MinSize == 0 {
cfg.Compression.MinSize = 1024 // 1KB - сжимаем только документы больше 1KB
}
if cfg.Compression.Level == 0 {
cfg.Compression.Level = 3 // Средний уровень сжатия
}
return &cfg, nil
}