diff options
Diffstat (limited to 'db')
-rw-r--r-- | db/db.go | 30 |
1 files changed, 27 insertions, 3 deletions
@@ -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 | ||
10 | var DB *pg.DB | ||
11 | |||
12 | var log = utils.StandardL().WithField("module", "db") | 11 | var log = utils.StandardL().WithField("module", "db") |
13 | 12 | ||
13 | var ( | ||
14 | DB *pg.DB | ||
15 | Redis *redis.Client | ||
16 | ) | ||
17 | |||
14 | type DBConfig struct { | 18 | type 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 | ||
21 | func Init(config DBConfig) { | 25 | type RedisConfig struct { |
26 | Type string // tcp or unix | ||
27 | Address string | ||
28 | Password string | ||
29 | } | ||
30 | |||
31 | func 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 | ||
32 | func migratedb() error { | 56 | func migratedb() error { |