diff --git a/README.md b/README.md index 41baeb2..ad524aa 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,25 @@ import ( }, ``` +To enable TLS/SSL, you need to add the following configuration: + +``` +import "crypto/tls" + +// config/database.go +"redis": map[string]any{ + "default": map[string]any{ + "host": config.Env("REDIS_HOST", ""), + "password": config.Env("REDIS_PASSWORD", ""), + "port": config.Env("REDIS_PORT", 6379), + "database": config.Env("REDIS_DB", 0), + "tls": &tls.Config{ + // Add your tls configuration here + }, + }, +}, +``` + ## Testing Run command below to run test: diff --git a/redis.go b/redis.go index 282d31a..7523a24 100644 --- a/redis.go +++ b/redis.go @@ -2,6 +2,7 @@ package redis import ( "context" + "crypto/tls" "fmt" "strconv" "time" @@ -31,11 +32,18 @@ func NewRedis(ctx context.Context, config config.Config, store string) (*Redis, return nil, nil } - client := redis.NewClient(&redis.Options{ + option := &redis.Options{ Addr: fmt.Sprintf("%s:%s", host, config.GetString(fmt.Sprintf("database.redis.%s.port", connection))), Password: config.GetString(fmt.Sprintf("database.redis.%s.password", connection)), DB: config.GetInt(fmt.Sprintf("database.redis.%s.database", connection)), - }) + } + + tlsConfig, ok := config.Get(fmt.Sprintf("database.redis.%s.tls", connection)).(*tls.Config) + if ok { + option.TLSConfig = tlsConfig + } + + client := redis.NewClient(option) if _, err := client.Ping(context.Background()).Result(); err != nil { return nil, errors.WithMessage(err, "init connection error") diff --git a/redis_test.go b/redis_test.go index da90760..46759bf 100644 --- a/redis_test.go +++ b/redis_test.go @@ -399,6 +399,7 @@ func getRedisDocker() (*dockertest.Pool, *dockertest.Resource, *Redis, error) { mockConfig.On("GetString", "database.redis.default.port").Return(resource.GetPort("6379/tcp")).Once() mockConfig.On("GetString", "database.redis.default.password").Return(resource.GetPort("")).Once() mockConfig.On("GetInt", "database.redis.default.database").Return(0).Once() + mockConfig.On("Get", "database.redis.default.tls").Return(nil).Once() mockConfig.On("GetString", "cache.prefix").Return("goravel_cache").Once() store, err = NewRedis(context.Background(), mockConfig, "redis")