/
mysql_connector.go
executable file
·47 lines (39 loc) · 1.42 KB
/
mysql_connector.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
package utils
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql" // mysql package required for gorm
"github.com/wantedly/gorm-zap"
"go.uber.org/zap"
)
func NewMySQLConnector(address string, port int, dbName, user, password string) MySQLConnector {
return MySQLConnector{address: address, port: port, dbName: dbName, user: user, password: password}
}
type MySQLConnector struct {
address string
port int
dbName string
user string
password string
db *gorm.DB
}
func (msc *MySQLConnector) Connect() (*gorm.DB, error) {
zap.S().Debug("Connecting to DB")
var err error
msc.db, err = gorm.Open("mysql", msc.connectionString())
if err != nil {
zap.S().Error("Generic error on MySQLConnector.Connect", err)
return nil, err
}
// Config stuff
msc.db.LogMode(true) // TODO: remove this magic number, use environment variable
msc.db.SetLogger(gormzap.New(zap.L()))
msc.db.DB().SetMaxIdleConns(10) // TODO: remove this magic number, use environment variable
msc.db.DB().SetMaxOpenConns(100) // TODO: remove this magic number, use environment variable
msc.db.DB().SetConnMaxLifetime(time.Hour) // TODO: remove this magic number, use environment variable
return msc.db, nil
}
func (msc MySQLConnector) connectionString() string {
return fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8&parseTime=True", msc.user, msc.password, msc.address, msc.port, msc.dbName)
}