-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
65 lines (54 loc) · 1.21 KB
/
database.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
package intercom
import (
"fmt"
"github.com/getevo/evo/v2/lib/args"
"github.com/getevo/evo/v2/lib/db"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"time"
)
var conn *gorm.DB
func setupDatabase() {
var host = args.Get("-db-host")
if host == "" {
host = "127.0.0.1"
}
var username = args.Get("-db-username")
if username == "" {
username = "root"
}
var database = args.Get("-db-database")
var password = args.Get("-db-password")
if password == "" {
password = ""
}
var params = args.Get("-db-params")
var newLog = logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: 200 * time.Millisecond, // Slow SQL threshold
LogLevel: logger.Warn, // Log level
Colorful: true, // Disable color
},
)
cfg := &gorm.Config{
Logger: newLog,
}
var err error
connectionString := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s", username, password, host, database, params)
conn, err = gorm.Open(mysql.Open(connectionString), cfg)
if err != nil {
log.Fatal(err)
}
db.Register(conn)
}
// GetDBO return database object instance
func GetDBO() *gorm.DB {
if conn == nil {
setupDatabase()
}
return conn
}