Skip to content

Commit

Permalink
refactor: use environment variables for Redis connection configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuni-sa committed May 25, 2024
1 parent e48409b commit 0e203cf
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"net/http"
"os"
"time"

"github.com/go-redis/redis"
Expand Down Expand Up @@ -137,30 +138,41 @@ func messageClient(client *websocket.Conn, msg ChatMessage) {
}
}

func main() {
port := "8000"
// port := os.Getenv("PORT")
func connectDB() *redis.Client {

// redisURL := "redis://:localhost:6379"
// redisURL := "redis://:172.17.0.2:6379"
// _, err := redis.ParseURL(redisURL)
// if err != nil {
// panic(err)
// }
redisAddr := "redis-service.socialhub.svc.cluster.local:6379"
if r := os.Getenv("REDIS_ADDR"); r != "" {
redisAddr = r
}

redisPass := ""
if r := os.Getenv("REDIS_PASS"); r != "" {
redisAddr = r
}

// defines redis connection
rdb = redis.NewClient(&redis.Options{
// Addr: "localhost:6379",
// Addr: "redis:6379",
Addr: "redis-service.socialhub.svc.cluster.local:6379",
Password: "",
Addr: redisAddr,
Password: redisPass,
DB: 0,
})

// simple ping / connection check
pong, err := rdb.Ping().Result()
log.Println(pong, err)
if err != nil {
log.Fatalf("Could not connect to Redis: %v", err)
}
log.Printf("Redis connected: %s", pong)
return rdb
}

func main() {
port := "8000"
// port := os.Getenv("PORT")

rdb = connectDB()
// creates an echo server
// http.Handle("/", http.FileServer(http.Dir("./public")))

Expand Down

0 comments on commit 0e203cf

Please sign in to comment.