package ads_redis

import (
	"fmt"
	"miads/adslib"
	"time"

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

var RedisConn *redis.Pool

func Setup() error {
	host := adslib.GetConf().RedisHost
	password := adslib.GetConf().RedisPassword
	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
}
