-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
108 lines (87 loc) · 2.23 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package app
import (
"fmt"
"github.com/astaxie/beego/logs"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/spf13/viper"
"log"
"os"
)
// 参考:http://gorm.io/docs/connecting_to_the_database.html
var DB *Database
type Database struct {
Self *gorm.DB
Docker *gorm.DB
}
func InitDb() {
DB = &Database{
Self: GetSelfDB(),
Docker: GetDockerDB(),
}
}
// used for cli
func InitSelfDB() *gorm.DB {
return InitDB("db")
}
func GetSelfDB() *gorm.DB {
return InitSelfDB()
}
func InitDockerDB() *gorm.DB {
return InitDB("docker_db")
}
func GetDockerDB() *gorm.DB {
return InitDockerDB()
}
func InitDB(dbName string) *gorm.DB {
driver := viper.GetString(fmt.Sprintf("%s.driver", dbName))
if driver == "" {
driver = "mysql"
}
if driver == "sqlite" {
return openSqliteDB(viper.GetString(fmt.Sprintf("%s.source", dbName)))
}
return openMySQLDB(viper.GetString(fmt.Sprintf("%s.username", dbName)),
viper.GetString(fmt.Sprintf("%s.password", dbName)),
viper.GetString(fmt.Sprintf("%s.addr", dbName)),
viper.GetString(fmt.Sprintf("%s.name", dbName)))
}
func openSqliteDB(source string) *gorm.DB {
db, err := gorm.Open("sqlite3", source)
if err != nil {
logs.Error(fmt.Sprintf("Database connection failed. Source: %s, err: %s", source, err.Error()))
log.Printf("初始化 db 服务出错:%s\n", err.Error())
os.Exit(1)
}
return db
}
func openMySQLDB(username, password, addr, name string) *gorm.DB {
config := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=%t&loc=%s",
username,
password,
addr,
name,
true,
//"Asia/Shanghai"),
"Local")
db, err := gorm.Open("mysql", config)
if err != nil {
logs.Error(fmt.Sprintf("Database connection failed. Database name: %s, err: %s", name, err.Error()))
log.Printf("初始化 db 服务出错:%s\n", err.Error())
os.Exit(1)
}
setupDB(db)
return db
}
func setupDB(db *gorm.DB) {
if viper.GetString("gormMode") == "debug" {
db.LogMode(true)
}
// 用于设置闲置的连接数.设置闲置的连接数则当开启的一个连接使用完成后可以放在池里等候下一次使用
db.DB().SetMaxIdleConns(0)
}
func (db *Database) Close() {
DB.Self.Close()
DB.Docker.Close()
}