Skip to content

Commit

Permalink
feat: allow connection to redis using uri
Browse files Browse the repository at this point in the history
closes: #1010
  • Loading branch information
iwpnd committed Nov 18, 2024
1 parent 0f3131f commit d1e1bcd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/cache"
"github.com/go-spatial/tegola/dict"
"github.com/go-spatial/tegola/internal/log"
)

const CacheType = "redis"
Expand All @@ -22,13 +23,15 @@ const (
ConfigKeyMaxZoom = "max_zoom"
ConfigKeyTTL = "ttl"
ConfigKeySSL = "ssl"
ConfigKeyURI = "uri"
)

var (
// default values
defaultNetwork = "tcp"
defaultAddress = "127.0.0.1:6379"
defaultPassword = ""
defaultURI = ""
defaultDB = 0
defaultMaxZoom = uint(tegola.MaxZ)
defaultTTL = 0
Expand All @@ -39,8 +42,25 @@ func init() {
cache.Register(CacheType, New)
}

// TODO @iwpnd: deprecate connection with Addr
// CreateOptions creates redis.Options from an implicit or explicit c
func CreateOptions(c dict.Dicter) (opts *redis.Options, err error) {
uri, err := c.String(ConfigKeyURI, &defaultURI)
if err != nil {
return nil, err
}

if uri != "" {
opts, err := redis.ParseURL(uri)
if err != nil {
return nil, err
}

return opts, nil
}

log.Warn("connecting to redis using 'Addr' is deprecated. use 'uri' instead.")

network, err := c.String(ConfigKeyNetwork, &defaultNetwork)
if err != nil {
return nil, err
Expand Down
36 changes: 36 additions & 0 deletions cache/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ func TestCreateOptions(t *testing.T) {
Password: "test",
},
},
"test with uri no ssl": {
config: map[string]interface{}{
"uri": "redis://user:[email protected]:6379/0",
},
expected: &goredis.Options{
Network: "tcp",
DB: 0,
Addr: "127.0.0.1:6379",
Password: "test",
},
},
"test with uri with ssl": {
config: map[string]interface{}{
"uri": "rediss://user:[email protected]:6379/0",
},
expected: &goredis.Options{
Network: "tcp",
DB: 0,
Addr: "127.0.0.1:6379",
Password: "test",
TLSConfig: &tls.Config{ /* no deep comparison */ },
},
},
"test empty config": {
config: map[string]interface{}{},
expected: &goredis.Options{
Expand Down Expand Up @@ -274,6 +297,11 @@ func TestNew(t *testing.T) {
"ssl": false,
},
},
"explicit config with uri": {
config: map[string]interface{}{
"uri": "redis://127.0.0.1:6379/0",
},
},
"implicit config": {
config: map[string]interface{}{},
},
Expand All @@ -285,6 +313,14 @@ func TestNew(t *testing.T) {
T: reflect.TypeOf(""),
},
},
"bad config uri": {
config: map[string]interface{}{"uri": 1},
expectedErr: dict.ErrKeyType{
Key: "uri",
Value: 1,
T: reflect.TypeOf(""),
},
},
"bad config ttl": {
config: map[string]interface{}{"ttl": "fails"},
expectedErr: dict.ErrKeyType{
Expand Down

0 comments on commit d1e1bcd

Please sign in to comment.