29 lines
446 B
Go
29 lines
446 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/gorm/logger"
|
||
|
"joylink.club/bj-rtsts-server/config"
|
||
|
)
|
||
|
|
||
|
var DB *gorm.DB
|
||
|
|
||
|
func InitDb() error {
|
||
|
dsn := config.Config.Datasource.Dsn
|
||
|
if dsn == "" {
|
||
|
return errors.New("未设置数据库连接dsn")
|
||
|
}
|
||
|
|
||
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||
|
Logger: logger.Default.LogMode(logger.Silent),
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
DB = db
|
||
|
return nil
|
||
|
}
|