URL parsing for redis (#64)

* URL parsing for redis

Co-authored-by: TroyDota <49777269+TroyDota@users.noreply.github.com>
This commit is contained in:
RW
2021-03-25 08:14:30 +01:00
committed by GitHub
parent 39d7f4ff11
commit 79bb924d44
4 changed files with 60 additions and 6 deletions

View File

@@ -19,12 +19,25 @@ func New(config ...Config) *Storage {
cfg := configDefault(config...)
// Create new redis client
db := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
DB: cfg.Database,
Username: cfg.Username,
Password: cfg.Password,
})
var options *redis.Options
var err error
if cfg.URL != "" {
options, err = redis.ParseURL(cfg.URL)
if err != nil {
panic(err)
}
} else {
options = &redis.Options{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
DB: cfg.Database,
Username: cfg.Username,
Password: cfg.Password,
}
}
db := redis.NewClient(options)
// Test connection
if err := db.Ping(context.Background()).Err(); err != nil {