-
Notifications
You must be signed in to change notification settings - Fork 86
/
redis.go
43 lines (37 loc) · 1.15 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package bindings
import (
"context"
"crypto/tls"
"log"
"time"
redis "github.com/go-redis/redis/v8"
)
// RedisBinding checks if a connection can be established with a Redis instance using the provided environment parameters
// and returns a BindingStatus indicating if the connection was successful.
func RedisBinding(envParams map[string]string) BindingStatus {
// session from your local session pool.
var err error
redisHost := envParams["HOST"] + ":" + envParams["PORT"]
if redisHost == ":" {
log.Println("redis HOST and PORT are required")
return BindingStatus{false, "Redis HOST and PORT are required"}
}
redisPassword := envParams["PASSWORD"]
op := &redis.Options{
Addr: redisHost,
Password: redisPassword,
WriteTimeout: 5 * time.Second,
}
// Enable TLS if the port is 6380.
if envParams["PORT"] == "6380" {
op.TLSConfig = &tls.Config{}
}
client := redis.NewClient(op)
ctx := context.Background()
err = client.Ping(ctx).Err()
if err != nil {
log.Printf("failed to connect with redis instance at %s - %v\n", redisHost, err.Error())
return BindingStatus{false, "not connected"}
}
return BindingStatus{true, "connected"}
}