Skip to content

Commit

Permalink
Using REDIS DB from ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
Sulejman committed Aug 12, 2024
1 parent 1d8940e commit d51dd50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 18 additions & 2 deletions config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
log "github.com/sirupsen/logrus"
"os"
"strconv"
)

var SettingsObj *Settings
Expand All @@ -14,6 +15,7 @@ type Settings struct {
RedisPort string
SlackReportingUrl string
DataMarketAddress string
RedisDb int
}

func LoadConfig() {
Expand All @@ -37,14 +39,28 @@ func LoadConfig() {
if config.DataMarketAddress == "" {
missingEnvVars = append(missingEnvVars, "DATA_MARKET_ADDRESS")
}
if getEnv("REDIS_DB", "") == "" {
missingEnvVars = append(missingEnvVars, "REDIS_DB")
}
if config.RedisHost == "" {
missingEnvVars = append(missingEnvVars, "REDIS_HOST")
}
if config.RedisPort == "" {
missingEnvVars = append(missingEnvVars, "REDIS_PORT")
}

if len(missingEnvVars) > 0 {
log.Fatalf("Missing required environment variables: %v", missingEnvVars)
}

redisDb, err := strconv.Atoi(getEnv("REDIS_DB", ""))
if err != nil || redisDb < 0 {
log.Fatalf("Invalid REDIS_DB value: %s", config.RedisDb)
}

config.RedisDb = redisDb

checkOptionalEnvVar(config.SlackReportingUrl, "SLACK_REPORTING_URL")
checkOptionalEnvVar(config.RedisHost, "REDIS_HOST")
checkOptionalEnvVar(config.RedisPort, "REDIS_PORT")

SettingsObj = &config
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewRedisClient() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", config.SettingsObj.RedisHost, config.SettingsObj.RedisPort), // Redis server address
Password: "", // no password set
DB: 0,
DB: config.SettingsObj.RedisDb,
PoolSize: 1000,
ReadTimeout: 200 * time.Millisecond,
WriteTimeout: 200 * time.Millisecond,
Expand Down

0 comments on commit d51dd50

Please sign in to comment.