Skip to content

Commit

Permalink
redis ssl support
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Nov 5, 2023
1 parent c689399 commit d5509f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ DB_NAME=
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASS=
REDIS_DB=0
REDIS_DB=0
REDIS_USE_SSL=false
REDIS_SSL_SERVER_NAME="*.c.db.ondigitalocean.com"
19 changes: 13 additions & 6 deletions app/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"crypto/tls"
"fmt"
"time"

Expand All @@ -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

Expand Down
17 changes: 13 additions & 4 deletions common/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d5509f9

Please sign in to comment.