From 0e203cf11ee1a8333473eaf1c7e4aa3412855a51 Mon Sep 17 00:00:00 2001 From: Yonatan Sasson Date: Thu, 4 Apr 2024 09:52:01 +0300 Subject: [PATCH] refactor: use environment variables for Redis connection configuration --- main.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index a9afc6b..ef89295 100755 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "io" "log" "net/http" + "os" "time" "github.com/go-redis/redis" @@ -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")))