自动初始化整体调试完成

This commit is contained in:
pixel
2021-03-04 19:38:07 +08:00
parent 4a995a1c56
commit 184b1a903f
14 changed files with 273 additions and 32 deletions
+7 -8
View File
@@ -5,7 +5,6 @@ import (
"gin-vue-admin/model/request"
"gin-vue-admin/model/response"
"gin-vue-admin/service"
"go.uber.org/zap"
"github.com/gin-gonic/gin"
@@ -31,7 +30,7 @@ func InitDB(c *gin.Context) {
}
if err := service.InitDB(dbInfo); err != nil {
global.GVA_LOG.Error("自动创建数据库失败", zap.Any("err", err))
response.FailWithMessage("自动创建数据库失败", c)
response.FailWithMessage("自动创建数据库失败,请查看后台日志", c)
return
}
response.OkWithData("自动创建数据库成功", c)
@@ -43,17 +42,17 @@ func InitDB(c *gin.Context) {
// @Success 200 {string} string "{"code":0,"data":{},"msg":"探测完成"}"
// @Router /init/checkdb [post]
func CheckDB(c *gin.Context) {
if global.GVA_DB == nil {
if global.GVA_DB != nil {
global.GVA_LOG.Info("数据库无需初始化")
response.OkWithDetailed(gin.H{
"needInit":false,
},"数据库无需初始化", c)
"needInit": false,
}, "数据库无需初始化", c)
return
}else{
} else {
global.GVA_LOG.Info("前往初始化数据库")
response.OkWithDetailed(gin.H{
"needInit":true,
},"前往初始化数据库", c)
"needInit": true,
}, "前往初始化数据库", c)
return
}
}
+5 -5
View File
@@ -54,11 +54,11 @@ captcha:
# mysql connect configuration
mysql:
path: '127.0.0.1:3306'
config: 'charset=utf8mb4&parseTime=True&loc=Local'
db-name: 'qmPlus'
username: 'root'
password: 'Aa@6447985'
path: ''
config: ''
db-name: ''
username: ''
password: ''
max-idle-conns: 10
max-open-conns: 100
log-mode: false
+22
View File
@@ -0,0 +1,22 @@
package middleware
import (
"gin-vue-admin/global"
"gin-vue-admin/model/response"
"github.com/gin-gonic/gin"
)
// 处理跨域请求,支持options访问
func NeedInit() gin.HandlerFunc {
return func(c *gin.Context) {
if global.GVA_DB == nil {
response.OkWithDetailed(gin.H{
"needInit": true,
}, "前往初始化数据库", c)
c.Abort()
} else {
c.Next()
}
// 处理请求
}
}
+2 -2
View File
@@ -3,7 +3,7 @@ package request
type InitDB struct {
Host string `json:"host"`
Port string `json:"port"`
UserName string `json:"user_name" binding:"required"`
UserName string `json:"userName" binding:"required"`
Password string `json:"password"`
DBName string `json:"db_name" binding:"required"`
DBName string `json:"dbName" binding:"required"`
}
+2 -2
View File
@@ -8,7 +8,7 @@ import (
func InitInitRouter(Router *gin.RouterGroup) {
ApiRouter := Router.Group("init")
{
ApiRouter.POST("initbd", v1.InitDB) // 创建Api
ApiRouter.POST("checkdb", v1.CheckDB) // 创建Api
ApiRouter.POST("initdb", v1.InitDB) // 创建Api
ApiRouter.POST("checkdb", v1.CheckDB) // 创建Api
}
}
+59 -2
View File
@@ -8,6 +8,8 @@ import (
"gin-vue-admin/model/request"
"gin-vue-admin/source"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//@author: [songzhibin97](https://github.com/songzhibin97)
@@ -62,6 +64,7 @@ func InitDB(conf request.InitDB) error {
if conf.Host == "" {
conf.Host = "127.0.0.1"
}
if conf.Port == "" {
conf.Port = "3306"
}
@@ -76,11 +79,65 @@ func InitDB(conf request.InitDB) error {
"mysql.db-name": conf.DBName,
"mysql.username": conf.UserName,
"mysql.password": conf.Password,
"mysql.config": "charset=utf8mb4&parseTime=True&loc=Local",
}
if err := writeConfig(global.GVA_VP, setting); err != nil {
return err
}
err := initDB(source.Admin,
m := global.GVA_CONFIG.Mysql
if m.Dbname == "" {
return nil
}
linkDns := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config
mysqlConfig := mysql.Config{
DSN: linkDns, // DSN data source name
DefaultStringSize: 191, // string 类型字段的默认长度
DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
SkipInitializeWithVersion: false, // 根据版本自动配置
}
if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
//global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
//os.Exit(0)
//return nil
return nil
} else {
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
global.GVA_DB = db
}
err := global.GVA_DB.AutoMigrate(
model.SysUser{},
model.SysAuthority{},
model.SysApi{},
model.SysBaseMenu{},
model.SysBaseMenuParameter{},
model.JwtBlacklist{},
model.SysDictionary{},
model.SysDictionaryDetail{},
model.ExaFileUploadAndDownload{},
model.ExaFile{},
model.ExaFileChunk{},
model.ExaSimpleUploader{},
model.ExaCustomer{},
model.SysOperationRecord{},
model.WorkflowProcess{},
model.WorkflowNode{},
model.WorkflowEdge{},
model.WorkflowStartPoint{},
model.WorkflowEndPoint{},
model.WorkflowMove{},
model.ExaWfLeave{},
)
if err != nil {
return err
}
err = initDB(
source.Admin,
source.Api,
source.AuthorityMenu,
source.Authority,
@@ -92,7 +149,7 @@ func InitDB(conf request.InitDB) error {
source.File,
source.BaseMenu,
source.Workflow)
if err!=nil {
if err != nil {
return err
}
return nil
+1
View File
@@ -176,6 +176,7 @@ var carbines = []gormadapter.CasbinRule{
//@author: [SliverHorn](https://github.com/SliverHorn)
//@description: casbin_rule 表数据初始化
func (c *casbin) Init() error {
global.GVA_DB.AutoMigrate(gormadapter.CasbinRule{})
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
if tx.Find(&[]gormadapter.CasbinRule{}).RowsAffected == 154 {
color.Danger.Println("\n[Mysql] --> casbin_rule 表的初始数据已存在!")