diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..d416c1f --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,134 @@ +/* + * Copyright 2026 Safronov Grigorii + * + * Licensed under the CDDL, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * https://opensource.org/licenses/CDDL-1.0 + */ + +// Файл: 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"` + API APIConfig `toml:"api"` + Replication ReplicationConfig `toml:"replication"` + Plugins PluginsConfig `toml:"plugins"` + Compression CompressionConfig `toml:"compression"` + WebUI WebUIConfig `toml:"webui"` +} + +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 APIConfig struct { + Port int `toml:"port"` // Порт для HTTP API сервера +} + +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"` // Минимальный размер для сжатия (байт) +} + +type WebUIConfig struct { + Enabled bool `toml:"enabled"` // Включить веб-интерфейс + Port int `toml:"port"` // Порт для веб-интерфейса + Theme string `toml:"theme"` // Тема оформления (dark, light) +} + +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" + } + + // Установка значений по умолчанию для API + if cfg.API.Port == 0 { + cfg.API.Port = 8080 + } + + // Установка значений по умолчанию для сжатия + 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 // Средний уровень сжатия + } + + // Установка значений по умолчанию для WebUI + if cfg.WebUI.Port == 0 { + cfg.WebUI.Port = 8080 + } + if cfg.WebUI.Theme == "" { + cfg.WebUI.Theme = "dark" + } + + return &cfg, nil +}