-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
73 lines (68 loc) · 2.15 KB
/
mysql.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
package starter
import (
"fmt"
"github.com/archine/ioc"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)
type M struct {
Db *gorm.DB
}
type config struct {
Mysql mysqlConfig `mapstructure:"mysql"`
}
type mysqlConfig struct {
LogLevel string `mapstructure:"log_level"` // Log level: debug or error
URL string `mapstructure:"url"` // Mysql url, IP:PORT
UserName string `mapstructure:"username"`
Password string `mapstructure:"password"`
Database string `mapstructure:"database"`
MaxIdle int `mapstructure:"max_idle"`
MaxConnect int `mapstructure:"max_connect"`
IdleTime time.Duration `mapstructure:"max_idle_time"`
}
func (m *M) CreateBean() ioc.Bean {
var conf config
v := ioc.GetBeanByName("viper.Viper").(*viper.Viper)
v.SetDefault("mysql.log_level", "error")
v.SetDefault("mysql.max_idle", 10)
v.SetDefault("mysql.max_connect", 50)
v.SetDefault("mysql.max_idle_time", 30*time.Second)
if err := v.Unmarshal(&conf); err != nil {
log.Fatalf("Failed to create a mysql bean, %s", err.Error())
}
dsn := conf.Mysql.URL
if conf.Mysql.Database != "" {
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", conf.Mysql.UserName, conf.Mysql.Password, conf.Mysql.URL, conf.Mysql.Database)
}
gormConf := &gorm.Config{
PrepareStmt: true,
}
if conf.Mysql.LogLevel == "error" {
gormConf.Logger = logger.Default.LogMode(logger.Error)
} else {
gormConf.Logger = logger.Default.LogMode(logger.Info)
}
db, err := gorm.Open(mysql.New(mysql.Config{
DSN: dsn,
DisableDatetimePrecision: true,
DontSupportRenameIndex: true,
DontSupportRenameColumn: true,
SkipInitializeWithVersion: false,
}), gormConf)
if err != nil {
log.Fatalf("Failed to create a mysql bean, %s", err.Error())
}
sqlDB, err := db.DB()
if err != nil {
log.Fatalf("Failed to create a mysql bean, %s", err.Error())
}
sqlDB.SetMaxIdleConns(conf.Mysql.MaxIdle)
sqlDB.SetMaxOpenConns(conf.Mysql.MaxConnect)
sqlDB.SetConnMaxLifetime(conf.Mysql.IdleTime)
return &M{Db: db}
}