Browse Source
新增功能-api生成数据库并初始化回写配置
新增功能-api生成数据库并初始化回写配置
修改gva initdb 判断是否已经创建的一些小问题 新增api生成数据库并初始化回写配置 修改文件: server/cmd/gva/initdb.go server/cmd/information/data_authorities.go server/cmd/information/workflow.go server/initialize/gorm server/main.go 新增文件: server/api/v1/sys.initdb.go server/middleware/init.go server/model/sys_init.go server/service/sys_initdb.go servcer/utils/init_db.go 迁移文件 server/cmd/gva/mysql.go->server/utils/mysql.gomain
songzhibin97
4 years ago
12 changed files with 219 additions and 29 deletions
-
39server/api/v1/sys_initdb.go
-
14server/cmd/gva/initdb.go
-
3server/cmd/information/system/data_authorities.go
-
27server/cmd/information/system/workflow.go
-
11server/initialize/gorm.go
-
5server/initialize/router.go
-
3server/main.go
-
20server/middleware/init.go
-
9server/model/sys_init.go
-
40server/service/sys_initdb.go
-
54server/utils/init_db.go
-
7server/utils/mysql.go
@ -0,0 +1,39 @@ |
|||||
|
package v1 |
||||
|
|
||||
|
import ( |
||||
|
"gin-vue-admin/global" |
||||
|
"gin-vue-admin/model" |
||||
|
"gin-vue-admin/model/response" |
||||
|
"gin-vue-admin/service" |
||||
|
|
||||
|
"go.uber.org/zap" |
||||
|
|
||||
|
"github.com/gin-gonic/gin" |
||||
|
) |
||||
|
|
||||
|
// @Tags InitDB
|
||||
|
// @Summary 初始化用户数据库
|
||||
|
// @Security ApiKeyAuth
|
||||
|
// @Produce application/json
|
||||
|
// @Param data body request.Empty true "空"
|
||||
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
|
// @Router /init/db [post]
|
||||
|
func InitDB(c *gin.Context) { |
||||
|
if global.GVA_DB != nil { |
||||
|
global.GVA_LOG.Error("非法访问") |
||||
|
response.FailWithMessage("非法访问", c) |
||||
|
return |
||||
|
} |
||||
|
var dbInfo model.InitDB |
||||
|
if err := c.ShouldBindJSON(&dbInfo); err != nil { |
||||
|
global.GVA_LOG.Error("参数校验不通过", zap.Any("err", err)) |
||||
|
response.FailWithMessage("参数校验不通过", c) |
||||
|
return |
||||
|
} |
||||
|
if err := service.InitDB(dbInfo); err != nil { |
||||
|
global.GVA_LOG.Error("自动创建数据库失败", zap.Any("err", err)) |
||||
|
response.FailWithMessage("自动创建数据库失败", c) |
||||
|
return |
||||
|
} |
||||
|
response.OkWithData("自动创建数据库成功", c) |
||||
|
} |
@ -0,0 +1,20 @@ |
|||||
|
package middleware |
||||
|
|
||||
|
import ( |
||||
|
"gin-vue-admin/global" |
||||
|
"gin-vue-admin/model/response" |
||||
|
|
||||
|
"github.com/gin-gonic/gin" |
||||
|
) |
||||
|
|
||||
|
func InitCheck() gin.HandlerFunc { |
||||
|
return func(c *gin.Context) { |
||||
|
if global.GVA_DB == nil { |
||||
|
// 未初始化
|
||||
|
response.FailWithDetailed(gin.H{"database": true}, "数据库未初始化", c) |
||||
|
c.Abort() |
||||
|
return |
||||
|
} |
||||
|
c.Next() |
||||
|
} |
||||
|
} |
@ -0,0 +1,9 @@ |
|||||
|
package model |
||||
|
|
||||
|
type InitDB struct { |
||||
|
Host string `json:"host"` |
||||
|
Port string `json:"port"` |
||||
|
UserName string `json:"user_name" binding:"required"` |
||||
|
Password string `json:"password"` |
||||
|
DBName string `json:"db_name" binding:"required"` |
||||
|
} |
@ -0,0 +1,40 @@ |
|||||
|
package service |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"gin-vue-admin/global" |
||||
|
"gin-vue-admin/model" |
||||
|
"gin-vue-admin/utils" |
||||
|
) |
||||
|
|
||||
|
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
|
//@function: InitDB
|
||||
|
//@description: 创建数据库并初始化
|
||||
|
//@param: authorityId string
|
||||
|
//@return: err error, treeMap map[string][]model.SysMenu
|
||||
|
|
||||
|
func InitDB(conf model.InitDB) error { |
||||
|
if conf.Host == "" { |
||||
|
conf.Host = "127.0.0.1" |
||||
|
} |
||||
|
if conf.Port == "" { |
||||
|
conf.Port = "3306" |
||||
|
} |
||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/", conf.UserName, conf.Password, conf.Host, conf.Port) |
||||
|
fmt.Println(dsn) |
||||
|
createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName) |
||||
|
if err := utils.CreateTable(dsn, "mysql", createSql); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
setting := map[string]interface{}{ |
||||
|
"mysql.path": fmt.Sprintf("%s:%s", conf.Host, conf.Port), |
||||
|
"mysql.db-name": conf.DBName, |
||||
|
"mysql.username": conf.UserName, |
||||
|
"mysql.password": conf.Password, |
||||
|
} |
||||
|
if err := utils.WriteConfig(global.GVA_VP, setting); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
utils.InitDB() |
||||
|
return nil |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
package utils |
||||
|
|
||||
|
import ( |
||||
|
"database/sql" |
||||
|
|
||||
|
"github.com/spf13/viper" |
||||
|
) |
||||
|
|
||||
|
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
|
//@function: CreateTable
|
||||
|
//@description: 创建数据库(mysql)
|
||||
|
//@param: dsn string, driver string, createSql
|
||||
|
//@return: error
|
||||
|
|
||||
|
func CreateTable(dsn string, driver string, createSql string) error { |
||||
|
db, err := sql.Open(driver, dsn) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
defer db.Close() |
||||
|
if err = db.Ping(); err != nil { |
||||
|
return err |
||||
|
} |
||||
|
_, err = db.Exec(createSql) |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
//@author: [songzhibin97](https://github.com/songzhibin97)
|
||||
|
//@function: WriteConfig
|
||||
|
//@description: 回写配置
|
||||
|
//@param:
|
||||
|
//@return: error
|
||||
|
|
||||
|
func WriteConfig(viper *viper.Viper, conf map[string]interface{}) error { |
||||
|
for k, v := range conf { |
||||
|
viper.Set(k, v) |
||||
|
} |
||||
|
return viper.WriteConfig() |
||||
|
} |
||||
|
|
||||
|
//@author: [Songzhibin97](https://github.com/Songzhibin97)
|
||||
|
//@function: InitDB
|
||||
|
//@description: 初始化db
|
||||
|
//@param:
|
||||
|
//@return: error
|
||||
|
|
||||
|
func InitDB() { |
||||
|
Mysql.CheckDatabase() |
||||
|
Mysql.CheckUtf8mb4() |
||||
|
Mysql.Info() |
||||
|
Mysql.Init() |
||||
|
Mysql.AutoMigrateTables() |
||||
|
Mysql.InitData() |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue