From 323b7f4032d19c06523f2eaaf1d20d32e1330b54 Mon Sep 17 00:00:00 2001 From: jloup Date: Mon, 30 Apr 2018 22:28:59 +0200 Subject: Redis connection. --- db/db.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'db') diff --git a/db/db.go b/db/db.go index 7f87201..078cd58 100644 --- a/db/db.go +++ b/db/db.go @@ -4,13 +4,17 @@ import ( migrate "github.com/go-pg/migrations" "github.com/go-pg/pg" "github.com/go-pg/pg/orm" + "github.com/go-redis/redis" "github.com/jloup/utils" ) -var DB *pg.DB - var log = utils.StandardL().WithField("module", "db") +var ( + DB *pg.DB + Redis *redis.Client +) + type DBConfig struct { Address string Database string @@ -18,7 +22,13 @@ type DBConfig struct { Password string } -func Init(config DBConfig) { +type RedisConfig struct { + Type string // tcp or unix + Address string + Password string +} + +func Init(config DBConfig, redisConfig RedisConfig) { var err error DB = connect(config) @@ -27,6 +37,20 @@ func Init(config DBConfig) { if err != nil { log.Fatalf("cannot migratedb '%v'\n", err) } + + Redis = redis.NewClient(&redis.Options{ + Network: redisConfig.Type, + Addr: redisConfig.Address, + Password: redisConfig.Password, + DB: 0, + }) + + _, err = Redis.Ping().Result() + + if err != nil { + log.Fatalf("redis init error %s", err) + } + } func migratedb() error { -- cgit v1.2.3