diff options
Diffstat (limited to 'services/worker/internal/configuration/configuration.go')
| -rw-r--r-- | services/worker/internal/configuration/configuration.go | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/services/worker/internal/configuration/configuration.go b/services/worker/internal/configuration/configuration.go new file mode 100644 index 0000000..84a5995 --- /dev/null +++ b/services/worker/internal/configuration/configuration.go @@ -0,0 +1,110 @@ +package configuration + +import ( + "fmt" + "os" + "strconv" + "time" +) + +type Configuration struct { + DatabaseURL string + WorkerConcurrency int + PollInterval time.Duration + FetchTimeout time.Duration + QueuePollInterval time.Duration + BatchSize int + HealthPort int + EncryptionKey string + LogLevel string + LogJSON bool +} + +func Load() (Configuration, error) { + databaseURL := os.Getenv("DATABASE_URL") + + if databaseURL == "" { + return Configuration{}, fmt.Errorf("DATABASE_URL is required") + } + + workerConcurrency := getEnvironmentInteger("WORKER_CONCURRENCY", 10) + pollInterval := getEnvironmentDuration("POLL_INTERVAL", 30*time.Second) + fetchTimeout := getEnvironmentDuration("FETCH_TIMEOUT", 30*time.Second) + queuePollInterval := getEnvironmentDuration("QUEUE_POLL_INTERVAL", 5*time.Second) + batchSize := getEnvironmentInteger("BATCH_SIZE", 50) + healthPort := getEnvironmentInteger("HEALTH_PORT", 8080) + encryptionKey := os.Getenv("ENCRYPTION_KEY") + logLevel := getEnvironmentString("LOG_LEVEL", "info") + logJSON := getEnvironmentBoolean("LOG_JSON", false) + + return Configuration{ + DatabaseURL: databaseURL, + WorkerConcurrency: workerConcurrency, + PollInterval: pollInterval, + FetchTimeout: fetchTimeout, + QueuePollInterval: queuePollInterval, + BatchSize: batchSize, + HealthPort: healthPort, + EncryptionKey: encryptionKey, + LogLevel: logLevel, + LogJSON: logJSON, + }, nil +} + +func getEnvironmentString(key string, defaultValue string) string { + value := os.Getenv(key) + + if value == "" { + return defaultValue + } + + return value +} + +func getEnvironmentInteger(key string, defaultValue int) int { + raw := os.Getenv(key) + + if raw == "" { + return defaultValue + } + + parsed, parseError := strconv.Atoi(raw) + + if parseError != nil { + return defaultValue + } + + return parsed +} + +func getEnvironmentDuration(key string, defaultValue time.Duration) time.Duration { + raw := os.Getenv(key) + + if raw == "" { + return defaultValue + } + + parsed, parseError := time.ParseDuration(raw) + + if parseError != nil { + return defaultValue + } + + return parsed +} + +func getEnvironmentBoolean(key string, defaultValue bool) bool { + raw := os.Getenv(key) + + if raw == "" { + return defaultValue + } + + parsed, parseError := strconv.ParseBool(raw) + + if parseError != nil { + return defaultValue + } + + return parsed +} |