forked from gocw/wepay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
39 lines (34 loc) · 786 Bytes
/
db.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
package main
import (
"time"
"github.com/Sirupsen/logrus"
"github.com/jinzhu/gorm"
"gopkg.in/mgo.v2"
_"github.com/go-sql-driver/mysql"
)
var session *mgo.Session
var db *gorm.DB
func initDB() {
switch config.DB {
case "mongodb":
session, err := mgo.Dial(config.DBURL)
if err != nil {
panic(err)
}
session.SetMode(mgo.Monotonic, true)
case "mysql":
var err error
path := config.DBURL //从env获取数据库连接地址
logrus.Info("path:", string(path)) //打印数据库连接地址
for {
db, err = gorm.Open("mysql", string(path)) //使用gorm连接数据库
if err != nil {
logrus.Error(err, "Retry in 2 seconds!")
time.Sleep(time.Second * 2)
continue
}
logrus.Info("DB connect successful!")
break
}
}
}