Template
1
0
Fork 0

PLSPLSPLS

This commit is contained in:
Atridad Lahiji 2024-04-09 11:58:08 -06:00
parent c7bfda3f8e
commit 9e5da7a63b
No known key found for this signature in database
2 changed files with 14 additions and 10 deletions

View file

@ -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) {

View file

@ -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 {