BsyErp\Contract\Init\DBInit.go:
package Init
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"log"
)
var db *gorm.DB
func init() {
var err error
db, err = gorm.Open("mysql", MYSQL_DSN)
if err != nil {
log.Fatal(err)
}
db.SingularTable(true)
db.DB().SetMaxIdleConns(MYSQL_MAXIDLE) // 同名包,直接使用
db.DB().SetMaxOpenConns(MYSQL_MAXCONN)
db.LogMode(true) // 会打印SQL语句
}
func GetDB() *gorm.DB {
return db
}
package Init
import (
"github.com/gin-gonic/gin"
"io"
"log"
"os"
)
func init () {
logFile, err := os.OpenFile(LOGGER_PATH, os.O_CREATE | os.O_APPEND, 0666)
gin.DefaultWriter = io.MultiWriter(logFile)
if err != nil {
log.Fatal("日志文件创建失败", err)
}
}
注:
①. 文档:
https://github.com/gin-gonic/gin#using-middleware