add models and jwt
This commit is contained in:
56
models/init.go
Normal file
56
models/init.go
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
@Time : 2020/6/28 21:46
|
||||
@Author : xuyiqing
|
||||
@File : init.py
|
||||
*/
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_blog/conf"
|
||||
"go_blog/pkg/util"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func SetUp(isOrmDebug bool) {
|
||||
conUri := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local",
|
||||
conf.DataBase.User,
|
||||
conf.DataBase.Password,
|
||||
conf.DataBase.Host,
|
||||
conf.DataBase.Port,
|
||||
conf.DataBase.DB,
|
||||
conf.DataBase.Charset)
|
||||
db, err := gorm.Open(conf.DataBase.Type, conUri)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
DB = db
|
||||
DB.LogMode(isOrmDebug)
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return conf.DataBase.Prefix + defaultTableName
|
||||
}
|
||||
|
||||
DB.AutoMigrate(&Account{})
|
||||
|
||||
}
|
||||
|
||||
type BaseModel struct {
|
||||
ID uint64 `gorm:"primary_key'" json:"id"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
DeletedAt *time.Time `sql:"index" json:"-"`
|
||||
}
|
||||
|
||||
// 生成全局唯一ID
|
||||
func (m *BaseModel) BeforeCreate(scope *gorm.Scope) error {
|
||||
if m.ID == 0 {
|
||||
m.ID = util.GenSonyFlakeId()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user