// Package config loads rule-engine-service settings from environment // variables, matching the names used in the repo-root .env.example. package config import ( "fmt" "os" "time" ) type Config struct { PostgresDSN string DeviceControlGRPCTarget string RabbitMQURL string RuleCacheRefreshInterval time.Duration } func Load() (Config, error) { cfg := Config{ PostgresDSN: fmt.Sprintf("postgres://%s:%s@%s:%s/%s", getEnv("POSTGRES_USER", "home_automation"), getEnv("POSTGRES_PASSWORD", ""), getEnv("POSTGRES_HOST", "localhost"), getEnv("POSTGRES_PORT", "5432"), getEnv("POSTGRES_DB", "home_automation"), ), DeviceControlGRPCTarget: fmt.Sprintf("%s:%s", getEnv("DEVICE_CONTROL_HOST", "localhost"), getEnv("DEVICE_CONTROL_GRPC_PORT", "50051"), ), RabbitMQURL: fmt.Sprintf("amqp://%s:%s@%s:%s/", getEnv("RABBITMQ_USER", "guest"), getEnv("RABBITMQ_PASSWORD", "guest"), getEnv("RABBITMQ_HOST", "localhost"), getEnv("RABBITMQ_PORT", "5672"), ), } interval, err := getEnvDuration("RULE_ENGINE_CACHE_REFRESH_INTERVAL", 15*time.Second) if err != nil { return Config{}, err } cfg.RuleCacheRefreshInterval = interval return cfg, nil } func getEnv(key, fallback string) string { if v := os.Getenv(key); v != "" { return v } return fallback } func getEnvDuration(key string, fallback time.Duration) (time.Duration, error) { v := os.Getenv(key) if v == "" { return fallback, nil } d, err := time.ParseDuration(v) if err != nil { return 0, fmt.Errorf("%s: %w", key, err) } return d, nil }