jrallison/go-workers

Use Redis URI scheme with auth to address the server

Closed this issue · 2 comments

It would be nice if go-workers could be configured via a redis-uri, in addition to the current way of server address, port.

This would be useful in Heroku where the Redis addons set a redis URI in the environment.

Thanks for this excellent package.

For example: redis://username:password@my.host:6389

You can do something like this if you need support for using a connection string as configuration input:

package main

import (
    "fmt"
    "net/url"
    "strings"
)

func main() {
    s := "redis://username:password@my.host:6389/4?pool=25&process=2"

    u, err := url.Parse(s)
    if err != nil {
        panic(err)
    }

    config := getConfig(u)

    fmt.Println(config)
}

func getConfig(u *url.URL) map[string]string {
    config := map[string]string{
        "server":   u.Host,
        "database": strings.Trim(u.Path, "/"),
        "pool":     "30",
        "process":  "1",
    }

    for k, v := range u.Query() {
        config[k] = v[0]
    }

    pass, exists := u.User.Password()
    if exists {
        config["password"] = pass
    }

    return config
}

https://gist.github.com/peterhellberg/7dec0edfc125fac893af

Thanks peter for the workaround. If anyone wants to send a pull request to incorporate this into the package, I'm open!