Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sakishum/d815b0a1ca1fe48bc4e3fc63258acd38 to your computer and use it in GitHub Desktop.
Save sakishum/d815b0a1ca1fe48bc4e3fc63258acd38 to your computer and use it in GitHub Desktop.
Parsing a Redis connection string for use with go-workers
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment