feat:Extraction of configuration parameters

This commit is contained in:
wenjianzhang
2020-07-06 13:53:15 +08:00
parent bfbc5081ea
commit 8a1614f64d
34 changed files with 99 additions and 79 deletions
+15 -10
View File
@@ -1,10 +1,12 @@
// +build sqlite3
package database
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
log "github.com/sirupsen/logrus"
"go-admin/global/orm"
"go-admin/global"
"go-admin/tools/config"
)
@@ -12,26 +14,25 @@ type SqLite struct {
}
func (e *SqLite) Setup() {
var err error
var db Database
db = new(SqLite)
orm.Source = db.GetConnect()
log.Info(orm.Source)
orm.Eloquent, err = db.Open(orm.Driver, orm.Source)
global.Source = db.GetConnect()
log.Info(global.Source)
global.Eloquent, err = db.Open(db.GetDriver(), db.GetConnect())
if err != nil {
log.Fatalf("%s connect error %v", orm.Driver, err)
log.Fatalf("%s connect error %v", db.GetDriver(), err)
} else {
log.Printf("%s connect success!", orm.Driver)
log.Printf("%s connect success!", db.GetDriver())
}
if orm.Eloquent.Error != nil {
log.Fatalf("database error %v", orm.Eloquent.Error)
if global.Eloquent.Error != nil {
log.Fatalf("database error %v", global.Eloquent.Error)
}
orm.Eloquent.LogMode(true)
global.Eloquent.LogMode(true)
}
// 打开数据库连接
@@ -42,4 +43,8 @@ func (*SqLite) Open(dbType string, conn string) (db *gorm.DB, err error) {
func (e *SqLite) GetConnect() string {
return config.DatabaseConfig.Source
}
func (e *SqLite) GetDriver() string {
return config.DatabaseConfig.Driver
}