-
Notifications
You must be signed in to change notification settings - Fork 20
/
store.go
46 lines (40 loc) · 995 Bytes
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"time"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
)
func initDB() {
var err error
// mysql conn
for {
db, err = gorm.Open("mysql", config.DB.User+":"+config.DB.Password+
"@tcp("+config.DB.Host+":"+config.DB.Port+")/"+config.DB.Name+
"?charset=utf8mb4&parseTime=True&loc=Local&timeout=90s")
if err != nil {
logrus.Warnf("waiting to connect to db: %s", err.Error())
time.Sleep(time.Second * 2)
continue
}
logrus.Info("Mysql connect successful.")
break
}
// gorm debug log
if config.APP.Debug {
db.LogMode(true)
}
}
// createTable gorm auto migrate tables
func createTables() {
db.AutoMigrate(&Note{})
}
func initRedis() {
// redis conn
rdb = redis.NewClient(&redis.Options{
Addr: config.Redis.Host + ":" + config.Redis.Port,
Password: config.Redis.Password,
DB: config.Redis.DB,
})
logrus.Info("Redis connect successful.")
}