aboutsummaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/db.go30
1 files changed, 27 insertions, 3 deletions
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 (
4 migrate "github.com/go-pg/migrations" 4 migrate "github.com/go-pg/migrations"
5 "github.com/go-pg/pg" 5 "github.com/go-pg/pg"
6 "github.com/go-pg/pg/orm" 6 "github.com/go-pg/pg/orm"
7 "github.com/go-redis/redis"
7 "github.com/jloup/utils" 8 "github.com/jloup/utils"
8) 9)
9 10
10var DB *pg.DB
11
12var log = utils.StandardL().WithField("module", "db") 11var log = utils.StandardL().WithField("module", "db")
13 12
13var (
14 DB *pg.DB
15 Redis *redis.Client
16)
17
14type DBConfig struct { 18type DBConfig struct {
15 Address string 19 Address string
16 Database string 20 Database string
@@ -18,7 +22,13 @@ type DBConfig struct {
18 Password string 22 Password string
19} 23}
20 24
21func Init(config DBConfig) { 25type RedisConfig struct {
26 Type string // tcp or unix
27 Address string
28 Password string
29}
30
31func Init(config DBConfig, redisConfig RedisConfig) {
22 var err error 32 var err error
23 33
24 DB = connect(config) 34 DB = connect(config)
@@ -27,6 +37,20 @@ func Init(config DBConfig) {
27 if err != nil { 37 if err != nil {
28 log.Fatalf("cannot migratedb '%v'\n", err) 38 log.Fatalf("cannot migratedb '%v'\n", err)
29 } 39 }
40
41 Redis = redis.NewClient(&redis.Options{
42 Network: redisConfig.Type,
43 Addr: redisConfig.Address,
44 Password: redisConfig.Password,
45 DB: 0,
46 })
47
48 _, err = Redis.Ping().Result()
49
50 if err != nil {
51 log.Fatalf("redis init error %s", err)
52 }
53
30} 54}
31 55
32func migratedb() error { 56func migratedb() error {