forked from ryyst/kalzu-value-store
- Move defaultConfig() and loadConfig() functions to config package - Remove unused yaml import from main.go - Clean separation of configuration logic - Update main() to use config.Load() Reduced main.go from ~3650 to ~3570 lines 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"kvs/types"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Default configuration
|
|
func Default() *types.Config {
|
|
hostname, _ := os.Hostname()
|
|
return &types.Config{
|
|
NodeID: hostname,
|
|
BindAddress: "127.0.0.1",
|
|
Port: 8080,
|
|
DataDir: "./data",
|
|
SeedNodes: []string{},
|
|
ReadOnly: false,
|
|
LogLevel: "info",
|
|
GossipIntervalMin: 60, // 1 minute
|
|
GossipIntervalMax: 120, // 2 minutes
|
|
SyncInterval: 300, // 5 minutes
|
|
CatchupInterval: 120, // 2 minutes
|
|
BootstrapMaxAgeHours: 720, // 30 days
|
|
ThrottleDelayMs: 100,
|
|
FetchDelayMs: 50,
|
|
|
|
// Phase 2: Default compression settings
|
|
CompressionEnabled: true,
|
|
CompressionLevel: 3, // Balance between performance and compression ratio
|
|
|
|
// Phase 2: Default TTL and size limit settings
|
|
DefaultTTL: "0", // No default TTL
|
|
MaxJSONSize: 1048576, // 1MB default max JSON size
|
|
|
|
// Phase 2: Default rate limiting settings
|
|
RateLimitRequests: 100, // 100 requests per window
|
|
RateLimitWindow: "1m", // 1 minute window
|
|
|
|
// Phase 2: Default tamper-evident logging settings
|
|
TamperLogActions: []string{"data_write", "user_create", "auth_failure"},
|
|
|
|
// Phase 2: Default backup system settings
|
|
BackupEnabled: true,
|
|
BackupSchedule: "0 0 * * *", // Daily at midnight
|
|
BackupPath: "./backups",
|
|
BackupRetention: 7, // Keep backups for 7 days
|
|
|
|
// Default feature toggle settings (all enabled by default)
|
|
AuthEnabled: true,
|
|
TamperLoggingEnabled: true,
|
|
ClusteringEnabled: true,
|
|
RateLimitingEnabled: true,
|
|
RevisionHistoryEnabled: true,
|
|
}
|
|
}
|
|
|
|
// Load configuration from file or create default
|
|
func Load(configPath string) (*types.Config, error) {
|
|
config := Default()
|
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
// Create default config file
|
|
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
|
|
return nil, fmt.Errorf("failed to create config directory: %v", err)
|
|
}
|
|
|
|
data, err := yaml.Marshal(config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to marshal default config: %v", err)
|
|
}
|
|
|
|
if err := os.WriteFile(configPath, data, 0644); err != nil {
|
|
return nil, fmt.Errorf("failed to write default config: %v", err)
|
|
}
|
|
|
|
fmt.Printf("Created default configuration at %s\n", configPath)
|
|
return config, nil
|
|
}
|
|
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read config file: %v", err)
|
|
}
|
|
|
|
if err := yaml.Unmarshal(data, config); err != nil {
|
|
return nil, fmt.Errorf("failed to parse config file: %v", err)
|
|
}
|
|
|
|
return config, nil
|
|
} |