utils.go
1 package main 2 3 import ( 4 "os" 5 "strings" 6 7 "go.uber.org/zap" 8 ) 9 10 const ( 11 DefaultKeyDBURL = "redis://keydb:6379" 12 ) 13 14 // GetKeyDBURL returns KeyDB URL with the following priority: 15 // 1. KEYDB_URL environment variable 16 // 2. CACHE_KEYDB_URL_FILE file content 17 // 3. Default value 18 func GetKeyDBURL(logger *zap.Logger) string { 19 // Priority 1: Environment variable 20 if keydbURL := strings.TrimSpace(os.Getenv("KEYDB_URL")); keydbURL != "" { 21 logger.Info("Using KeyDB URL from environment variable", zap.String("url", keydbURL)) 22 return keydbURL 23 } 24 25 // Priority 2: Configurable connection file path 26 connectionFile := os.Getenv("CACHE_KEYDB_URL_FILE") 27 if connectionFile == "" { 28 connectionFile = "/app/.keydb-url" 29 } 30 31 if content, err := os.ReadFile(connectionFile); err == nil { 32 keydbURL := strings.TrimSpace(string(content)) 33 if len(keydbURL) > 0 { 34 logger.Info("Using KeyDB URL from connection file", zap.String("file", connectionFile), zap.String("url", keydbURL)) 35 return keydbURL 36 } else { 37 logger.Warn("KeyDB connection file is empty", zap.String("file", connectionFile)) 38 } 39 } else { 40 logger.Debug("KeyDB connection file not found", zap.String("file", connectionFile), zap.Error(err)) 41 } 42 43 // Priority 3: Default 44 logger.Info("Using default KeyDB URL", zap.String("url", DefaultKeyDBURL)) 45 return DefaultKeyDBURL 46 }