-
-
Save sakishum/d815b0a1ca1fe48bc4e3fc63258acd38 to your computer and use it in GitHub Desktop.
Parsing a Redis connection string for use with go-workers
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
package main | |
import ( | |
"fmt" | |
"net/url" | |
"strings" | |
) | |
func main() { | |
s := "redis://username:[email protected]: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