diff --git a/lib/pubsub/adapters/redispubsub.go b/lib/pubsub/adapters/redispubsub.go index d0eb35e..3900f95 100644 --- a/lib/pubsub/adapters/redispubsub.go +++ b/lib/pubsub/adapters/redispubsub.go @@ -2,6 +2,7 @@ package adapters import ( "context" + "errors" "os" "atri.dad/lib" @@ -21,20 +22,24 @@ type RedisPubSub struct { Client *redis.Client } -func NewRedisClient() *redis.Client { +func NewRedisClient() (*redis.Client, error) { if RedisClient != nil { - return RedisClient + return RedisClient, nil } godotenv.Load(".env") redis_url := os.Getenv("REDIS_URL") - opts, _ := redis.ParseURL(redis_url) + opts, err := redis.ParseURL(redis_url) - lib.LogInfo.Printf("\n[PUBSUB/REDIS]Connecting to Redis at") + if err != nil { + return nil, errors.New("math: square root of negative number") + } + + lib.LogInfo.Printf("\n[PUBSUB/REDIS]Connecting to Redis at %s\n", opts.Addr) RedisClient = redis.NewClient(opts) - return RedisClient + return RedisClient, nil } func (m *RedisPubSubMessage) ReceiveMessage(ctx context.Context) (*pubsub.Message, error) { diff --git a/main.go b/main.go index b34e933..c52350b 100755 --- a/main.go +++ b/main.go @@ -29,15 +29,14 @@ func main() { godotenv.Load(".env") // Initialize Redis client - adapters.RedisClient = adapters.NewRedisClient() + redisClient, redisError := adapters.NewRedisClient() - // Test Redis connection - _, err := adapters.RedisClient.Ping(context.Background()).Result() + adapters.RedisClient = redisClient // Initialize pubsub var pubSub pubsub.PubSub - if err != nil { - lib.LogWarning.Printf("\n[PUBSUB/INIT] Failed to connect to Redis: %v\n", err) + if redisError != nil { + lib.LogWarning.Printf("\n[PUBSUB/INIT] Failed to connect to Redis: %v\n", redisError) lib.LogWarning.Printf("\n[PUBSUB/INIT] Falling back to LocalPubSub\n") pubSub = &adapters.LocalPubSub{} } else {