-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
45 lines (36 loc) · 1.05 KB
/
init.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
package dao
import (
"fmt"
"os"
"time"
configs "github.com/mike955/zebra/cellphone/configs"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
type CommonTimeModel struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func Init(config configs.Mysql) {
var err error
addr, username, password, database := config.MysqlAddr, config.MysqlUsername, config.MysqlPassword, config.MysqlDatabase
if os.Getenv("MYSQL_ADDR") != "" && os.Getenv("MYSQL_PORT") != "" {
addr = os.Getenv("MYSQL_ADDR") + ":" + os.Getenv("MYSQL_PORT")
}
if os.Getenv("MYSQL_USERNAME") != "" {
username = os.Getenv("MYSQL_USERNAME")
}
if os.Getenv("MYSQL_PASSWORD") != "" {
password = os.Getenv("MYSQL_PASSWORD")
}
if os.Getenv("MYSQL_DATABASE") != "" {
database = os.Getenv("MYSQL_DATABASE")
}
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, addr, database)
DB, err = gorm.Open(mysql.Open(dsn))
DB = DB.Debug()
if err != nil {
panic("connect mysql error: " + err.Error())
}
}