- The redis url is provided as an environment variable, REDIS_URL.
- To use redis from golang, I use redis-go. I install this library by typing "go get github.com/gopkg.in/redis.v3"
- Parse the url and extract the user before connecting(unless, you might end up with the error "dial tcp: too many colons in address redis://...")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "gopkg.in/redis.v3" | |
var ( | |
//Client for the database connection | |
client *redis.Client | |
) | |
func connect() { | |
var resolvedURL = os.Getenv("REDIS_URL") | |
var password = "" | |
if !strings.Contains(herokuURL, "localhost") { | |
parsedURL, _ := url.Parse(herokuURL) | |
password, _ = parsedURL.User.Password() | |
resolvedURL = parsedURL.Host | |
} | |
fmt.Printf("connecting to %s", herokuURL) | |
client = redis.NewClient(&redis.Options{ | |
Addr: resolvedURL, | |
Password: password, | |
DB: 0, // use default DB | |
}) | |
} |
No comments:
Post a Comment