Skip to content

Commit

Permalink
configuring redis from redis uri
Browse files Browse the repository at this point in the history
  • Loading branch information
daithihearn committed May 26, 2024
1 parent 071a065 commit 081591e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"github.com/go-redis/redis/v8"
"log"
"net/url"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -68,20 +69,31 @@ func main() {
dbName = "cards-110"
}

// Configure redis
redisUrl := os.Getenv("REDIS_URL")
if redisUrl == "" {
redisUrl = "localhost:6379"
// Get the Redis URL from the environment
redisUri := os.Getenv("REDIS_URL")
if redisUri == "" {
redisUri = "redis://:password@localhost:6379/0"
}
redisPassword := os.Getenv("REDIS_PASSWORD")
if redisPassword == "" {
redisPassword = "password"

// Parse the Redis URL
parsedUrl, err := url.Parse(redisUri)
if err != nil {
log.Fatalf("Failed to parse Redis URL: %v", err)
}

// Extract the password from the URL
redisPassword, _ := parsedUrl.User.Password()

// Extract the address from the URL
redisAddr := parsedUrl.Host

// Configure the Redis client
rdb := redis.NewClient(&redis.Options{
Addr: redisUrl, // use your Redis Address
Addr: redisAddr,
Password: redisPassword,
DB: 0, // use default DB
})

gameCache := cache.NewRedisCache[game.State](rdb, ctx)
statsCache := cache.NewRedisCache[[]stats.PlayerStats](rdb, ctx)

Expand Down

0 comments on commit 081591e

Please sign in to comment.