From d5509f918668669eea53cae8fa9a509008eb4672 Mon Sep 17 00:00:00 2001 From: cmyui Date: Sun, 5 Nov 2023 11:55:56 -0500 Subject: [PATCH] redis ssl support --- .env.example | 4 +++- app/start.go | 19 +++++++++++++------ common/settings.go | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index c215833..0aa8c17 100644 --- a/.env.example +++ b/.env.example @@ -17,4 +17,6 @@ DB_NAME= REDIS_HOST=localhost REDIS_PORT=6379 REDIS_PASS= -REDIS_DB=0 \ No newline at end of file +REDIS_DB=0 +REDIS_USE_SSL=false +REDIS_SSL_SERVER_NAME="*.c.db.ondigitalocean.com" diff --git a/app/start.go b/app/start.go index 9d1d1a0..b6e229e 100644 --- a/app/start.go +++ b/app/start.go @@ -1,6 +1,7 @@ package app import ( + "crypto/tls" "fmt" "time" @@ -26,14 +27,20 @@ func Start(dbO *sqlx.DB) *fhr.Router { rawRouter := fhr.New() r := router{rawRouter} - // TODO: Implement datadog APM. - - // redis settings := common.GetSettings() + + // initialise redis + var tlsConfig *tls.Config + if settings.REDIS_USE_SSL { + tlsConfig = &tls.Config{ + ServerName: settings.REDIS_SSL_SERVER_NAME, + } + } red = redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", settings.REDIS_HOST, settings.REDIS_PORT), - Password: settings.REDIS_PASS, - DB: settings.REDIS_DB, + Addr: fmt.Sprintf("%s:%d", settings.REDIS_HOST, settings.REDIS_PORT), + Password: settings.REDIS_PASS, + DB: settings.REDIS_DB, + TLSConfig: tlsConfig, }) peppy.R = red diff --git a/common/settings.go b/common/settings.go index 7212b7e..e033ad3 100644 --- a/common/settings.go +++ b/common/settings.go @@ -20,6 +20,11 @@ func strToInt(s string) int { return val } +func strToBool(s string) bool { + val, _ := strconv.ParseBool(s) + return val +} + type Settings struct { APP_PORT int APP_DOMAIN string @@ -37,10 +42,12 @@ type Settings struct { DB_PASS string DB_NAME string - REDIS_HOST string - REDIS_PORT int - REDIS_PASS string - REDIS_DB int + REDIS_HOST string + REDIS_PORT int + REDIS_PASS string + REDIS_DB int + REDIS_USE_SSL bool + REDIS_SSL_SERVER_NAME string } var settings = Settings{} @@ -68,6 +75,8 @@ func LoadSettings() Settings { settings.REDIS_PORT = strToInt(getEnv("REDIS_PORT")) settings.REDIS_PASS = getEnv("REDIS_PASS") settings.REDIS_DB = strToInt(getEnv("REDIS_DB")) + settings.REDIS_USE_SSL = strToBool(getEnv("REDIS_USE_SSL")) + settings.REDIS_SSL_SERVER_NAME = getEnv("REDIS_SSL_SERVER_NAME") return settings }