package ads_redis

import (
	"fmt"
	"time"

	"github.com/gomodule/redigo/redis"
)

var RedisConn *redis.Pool

var HOST = ""
var PASSWORD = ""

func Setup() error {
	RedisConn = &redis.Pool{
		MaxIdle:     10,
		MaxActive:   10,
		IdleTimeout: 50,
		Dial: func() (redis.Conn, error) {
			c, err := redis.Dial("tcp", HOST)
			if err != nil {
				fmt.Println(err)
				return nil, err
			}
			if PASSWORD != "" {
				if _, err := c.Do("AUTH", PASSWORD); err != nil {
					fmt.Println(err)
					c.Close()
					return nil, err
				}
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			fmt.Println(err)
			return err
		},
	}

	return nil
}

