v0.1.1 improved config, added ratelimiter (buggy), readme not fully updated
This commit is contained in:
@ -5,8 +5,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
lconfig "github.com/lixenwraith/config"
|
||||
)
|
||||
|
||||
// Config holds the complete configuration
|
||||
@ -24,72 +25,237 @@ type MonitorConfig struct {
|
||||
|
||||
// MonitorTarget represents a path to monitor
|
||||
type MonitorTarget struct {
|
||||
Path string `toml:"path"`
|
||||
Pattern string `toml:"pattern"`
|
||||
Path string `toml:"path"` // File or directory path
|
||||
Pattern string `toml:"pattern"` // Glob pattern for directories
|
||||
IsFile bool `toml:"is_file"` // True if monitoring specific file
|
||||
}
|
||||
|
||||
// StreamConfig holds streaming settings
|
||||
type StreamConfig struct {
|
||||
BufferSize int `toml:"buffer_size"`
|
||||
BufferSize int `toml:"buffer_size"`
|
||||
RateLimit RateLimitConfig `toml:"rate_limit"`
|
||||
}
|
||||
|
||||
// DefaultConfig returns configuration with sensible defaults
|
||||
func DefaultConfig() *Config {
|
||||
// RateLimitConfig holds rate limiting settings
|
||||
type RateLimitConfig struct {
|
||||
Enabled bool `toml:"enabled"`
|
||||
RequestsPerSecond int `toml:"requests_per_second"`
|
||||
BurstSize int `toml:"burst_size"`
|
||||
CleanupIntervalS int64 `toml:"cleanup_interval_s"`
|
||||
}
|
||||
|
||||
// defaults returns configuration with default values
|
||||
func defaults() *Config {
|
||||
return &Config{
|
||||
Port: 8080,
|
||||
Monitor: MonitorConfig{
|
||||
CheckIntervalMs: 100,
|
||||
Targets: []MonitorTarget{
|
||||
{
|
||||
Path: "./",
|
||||
Pattern: "*.log",
|
||||
},
|
||||
{Path: "./", Pattern: "*.log", IsFile: false},
|
||||
},
|
||||
},
|
||||
Stream: StreamConfig{
|
||||
BufferSize: 1000,
|
||||
RateLimit: RateLimitConfig{
|
||||
Enabled: false,
|
||||
RequestsPerSecond: 10,
|
||||
BurstSize: 20,
|
||||
CleanupIntervalS: 60,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Load reads configuration from default location or returns defaults
|
||||
// Load reads configuration using lixenwraith/config Builder pattern
|
||||
// CHANGED: Now uses config.Builder for all source handling
|
||||
func Load() (*Config, error) {
|
||||
cfg := DefaultConfig()
|
||||
configPath := GetConfigPath()
|
||||
|
||||
// CHANGED: Use Builder pattern with custom environment transform
|
||||
cfg, err := lconfig.NewBuilder().
|
||||
WithDefaults(defaults()).
|
||||
WithEnvPrefix("LOGWISP_").
|
||||
WithFile(configPath).
|
||||
WithEnvTransform(customEnvTransform).
|
||||
WithSources(
|
||||
// CHANGED: CLI args removed here - handled separately in LoadWithCLI
|
||||
lconfig.SourceEnv,
|
||||
lconfig.SourceFile,
|
||||
lconfig.SourceDefault,
|
||||
).
|
||||
Build()
|
||||
|
||||
// Determine config path
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return cfg, nil // Return defaults if can't find home
|
||||
// Only fail on actual errors, not missing config file
|
||||
if !strings.Contains(err.Error(), "not found") {
|
||||
return nil, fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// configPath := filepath.Join(homeDir, ".config", "logwisp.toml")
|
||||
configPath := filepath.Join(homeDir, "git", "lixenwraith", "logwisp", "config", "logwisp.toml")
|
||||
|
||||
// Check if config file exists
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
// No config file, use defaults
|
||||
return cfg, nil
|
||||
// Special handling for LOGWISP_MONITOR_TARGETS env var
|
||||
if err := handleMonitorTargetsEnv(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read and parse config file
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config: %w", err)
|
||||
// Scan into final config
|
||||
finalConfig := &Config{}
|
||||
if err := cfg.Scan("", finalConfig); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan config: %w", err)
|
||||
}
|
||||
|
||||
if err := toml.Unmarshal(data, cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||||
}
|
||||
|
||||
// Validate
|
||||
if err := cfg.validate(); err != nil {
|
||||
return nil, fmt.Errorf("invalid config: %w", err)
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
return finalConfig, finalConfig.validate()
|
||||
}
|
||||
|
||||
// validate checks configuration sanity
|
||||
// LoadWithCLI loads configuration and applies CLI arguments
|
||||
// CHANGED: New function that properly integrates CLI args with config package
|
||||
func LoadWithCLI(cliArgs []string) (*Config, error) {
|
||||
configPath := GetConfigPath()
|
||||
|
||||
// Convert CLI args to config format
|
||||
convertedArgs := convertCLIArgs(cliArgs)
|
||||
|
||||
cfg, err := lconfig.NewBuilder().
|
||||
WithDefaults(defaults()).
|
||||
WithEnvPrefix("LOGWISP_").
|
||||
WithFile(configPath).
|
||||
WithArgs(convertedArgs). // CHANGED: Use WithArgs for CLI
|
||||
WithEnvTransform(customEnvTransform).
|
||||
WithSources(
|
||||
lconfig.SourceCLI, // CLI highest priority
|
||||
lconfig.SourceEnv,
|
||||
lconfig.SourceFile,
|
||||
lconfig.SourceDefault,
|
||||
).
|
||||
Build()
|
||||
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "not found") {
|
||||
return nil, fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle special env var
|
||||
if err := handleMonitorTargetsEnv(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Scan into final config
|
||||
finalConfig := &Config{}
|
||||
if err := cfg.Scan("", finalConfig); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan config: %w", err)
|
||||
}
|
||||
|
||||
return finalConfig, finalConfig.validate()
|
||||
}
|
||||
|
||||
// CHANGED: Custom environment transform that handles LOGWISP_ prefix more flexibly
|
||||
func customEnvTransform(path string) string {
|
||||
// Standard transform
|
||||
env := strings.ReplaceAll(path, ".", "_")
|
||||
env = strings.ToUpper(env)
|
||||
env = "LOGWISP_" + env
|
||||
|
||||
// Also check for some common variations
|
||||
// This allows both LOGWISP_STREAM_RATE_LIMIT_REQUESTS_PER_SEC
|
||||
// and LOGWISP_STREAM_RATE_LIMIT_REQUESTS_PER_SECOND
|
||||
switch env {
|
||||
case "LOGWISP_STREAM_RATE_LIMIT_REQUESTS_PER_SECOND":
|
||||
if _, exists := os.LookupEnv("LOGWISP_STREAM_RATE_LIMIT_REQUESTS_PER_SEC"); exists {
|
||||
return "LOGWISP_STREAM_RATE_LIMIT_REQUESTS_PER_SEC"
|
||||
}
|
||||
case "LOGWISP_STREAM_RATE_LIMIT_CLEANUP_INTERVAL_S":
|
||||
if _, exists := os.LookupEnv("LOGWISP_STREAM_RATE_LIMIT_CLEANUP_INTERVAL"); exists {
|
||||
return "LOGWISP_STREAM_RATE_LIMIT_CLEANUP_INTERVAL"
|
||||
}
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
// CHANGED: Convert CLI args to config package format
|
||||
func convertCLIArgs(args []string) []string {
|
||||
var converted []string
|
||||
|
||||
for _, arg := range args {
|
||||
switch {
|
||||
case arg == "-c" || arg == "--color":
|
||||
// Color mode is handled separately by main.go
|
||||
continue
|
||||
case strings.HasPrefix(arg, "--config="):
|
||||
// Config file path handled separately
|
||||
continue
|
||||
case strings.HasPrefix(arg, "--"):
|
||||
// Pass through other long flags
|
||||
converted = append(converted, arg)
|
||||
}
|
||||
}
|
||||
|
||||
return converted
|
||||
}
|
||||
|
||||
// GetConfigPath returns the configuration file path
|
||||
// CHANGED: Exported and simplified - now just returns the path, no manual env handling
|
||||
func GetConfigPath() string {
|
||||
// Check explicit config file paths
|
||||
if configFile := os.Getenv("LOGWISP_CONFIG_FILE"); configFile != "" {
|
||||
if filepath.IsAbs(configFile) {
|
||||
return configFile
|
||||
}
|
||||
if configDir := os.Getenv("LOGWISP_CONFIG_DIR"); configDir != "" {
|
||||
return filepath.Join(configDir, configFile)
|
||||
}
|
||||
return configFile
|
||||
}
|
||||
|
||||
if configDir := os.Getenv("LOGWISP_CONFIG_DIR"); configDir != "" {
|
||||
return filepath.Join(configDir, "logwisp.toml")
|
||||
}
|
||||
|
||||
// Default location
|
||||
if homeDir, err := os.UserHomeDir(); err == nil {
|
||||
return filepath.Join(homeDir, ".config", "logwisp.toml")
|
||||
}
|
||||
|
||||
return "logwisp.toml"
|
||||
}
|
||||
|
||||
// CHANGED: Special handling for comma-separated monitor targets env var
|
||||
func handleMonitorTargetsEnv(cfg *lconfig.Config) error {
|
||||
if targetsStr := os.Getenv("LOGWISP_MONITOR_TARGETS"); targetsStr != "" {
|
||||
// Clear any existing targets from file/defaults
|
||||
cfg.Set("monitor.targets", []MonitorTarget{})
|
||||
|
||||
// Parse comma-separated format: path:pattern:isfile,path2:pattern2:isfile
|
||||
parts := strings.Split(targetsStr, ",")
|
||||
for i, part := range parts {
|
||||
targetParts := strings.Split(part, ":")
|
||||
if len(targetParts) >= 1 && targetParts[0] != "" {
|
||||
path := fmt.Sprintf("monitor.targets.%d.path", i)
|
||||
cfg.Set(path, targetParts[0])
|
||||
|
||||
if len(targetParts) >= 2 && targetParts[1] != "" {
|
||||
pattern := fmt.Sprintf("monitor.targets.%d.pattern", i)
|
||||
cfg.Set(pattern, targetParts[1])
|
||||
} else {
|
||||
pattern := fmt.Sprintf("monitor.targets.%d.pattern", i)
|
||||
cfg.Set(pattern, "*.log")
|
||||
}
|
||||
|
||||
if len(targetParts) >= 3 {
|
||||
isFile := fmt.Sprintf("monitor.targets.%d.is_file", i)
|
||||
cfg.Set(isFile, targetParts[2] == "true")
|
||||
} else {
|
||||
isFile := fmt.Sprintf("monitor.targets.%d.is_file", i)
|
||||
cfg.Set(isFile, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validate ensures configuration is valid
|
||||
func (c *Config) validate() error {
|
||||
if c.Port < 1 || c.Port > 65535 {
|
||||
return fmt.Errorf("invalid port: %d", c.Port)
|
||||
@ -99,10 +265,6 @@ func (c *Config) validate() error {
|
||||
return fmt.Errorf("check interval too small: %d ms", c.Monitor.CheckIntervalMs)
|
||||
}
|
||||
|
||||
if c.Stream.BufferSize < 1 {
|
||||
return fmt.Errorf("buffer size must be positive: %d", c.Stream.BufferSize)
|
||||
}
|
||||
|
||||
if len(c.Monitor.Targets) == 0 {
|
||||
return fmt.Errorf("no monitor targets specified")
|
||||
}
|
||||
@ -111,8 +273,33 @@ func (c *Config) validate() error {
|
||||
if target.Path == "" {
|
||||
return fmt.Errorf("target %d: empty path", i)
|
||||
}
|
||||
if target.Pattern == "" {
|
||||
return fmt.Errorf("target %d: empty pattern", i)
|
||||
|
||||
if !target.IsFile && target.Pattern == "" {
|
||||
return fmt.Errorf("target %d: pattern required for directory monitoring", i)
|
||||
}
|
||||
|
||||
// SECURITY: Validate paths don't contain directory traversal
|
||||
if strings.Contains(target.Path, "..") {
|
||||
return fmt.Errorf("target %d: path contains directory traversal", i)
|
||||
}
|
||||
}
|
||||
|
||||
if c.Stream.BufferSize < 1 {
|
||||
return fmt.Errorf("buffer size must be positive: %d", c.Stream.BufferSize)
|
||||
}
|
||||
|
||||
if c.Stream.RateLimit.Enabled {
|
||||
if c.Stream.RateLimit.RequestsPerSecond < 1 {
|
||||
return fmt.Errorf("rate limit requests per second must be positive: %d",
|
||||
c.Stream.RateLimit.RequestsPerSecond)
|
||||
}
|
||||
if c.Stream.RateLimit.BurstSize < 1 {
|
||||
return fmt.Errorf("rate limit burst size must be positive: %d",
|
||||
c.Stream.RateLimit.BurstSize)
|
||||
}
|
||||
if c.Stream.RateLimit.CleanupIntervalS < 1 {
|
||||
return fmt.Errorf("rate limit cleanup interval must be positive: %d",
|
||||
c.Stream.RateLimit.CleanupIntervalS)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user