refactor: Add config validation, defaults, and unit tests

This commit is contained in:
2024-11-11 14:58:09 +02:00
parent bea0d22c26
commit c7b0778b77
2 changed files with 83 additions and 0 deletions

View File

@@ -18,6 +18,15 @@ const (
EnvResponseTimeout = "RESPONSE_TIMEOUT"
)
// Default configuration values
const (
DefaultLogLevel = zerolog.InfoLevel
DefaultNumWorkers = 5
DefaultWorkerBatchSize = 100
DefaultMaxResponseSize = 1048576 // 1MB
DefaultResponseTimeout = 30 // seconds
)
// Config holds the application configuration loaded from environment variables
type Config struct {
LogLevel zerolog.Level // Logging level (debug, info, warn, error)
@@ -28,6 +37,14 @@ type Config struct {
WorkerBatchSize int // Batch size for worker processing
}
// String returns a string representation of the configuration
func (c *Config) String() string {
return fmt.Sprintf(
"Config{LogLevel: %s, RootPath: %s, MaxResponseSize: %d, NumWorkers: %d, ResponseTimeout: %d, WorkerBatchSize: %d}",
c.LogLevel, c.RootPath, c.MaxResponseSize, c.NumOfWorkers, c.ResponseTimeout, c.WorkerBatchSize,
)
}
var CONFIG Config
// parsePositiveInt parses and validates positive integer values
@@ -57,6 +74,9 @@ func GetConfig() *Config {
return nil
},
EnvRootPath: func(v string) error {
if _, err := os.Stat(v); err != nil {
return fmt.Errorf("invalid root path: %w", err)
}
config.RootPath = v
return nil
},