奇淼(piexlmax
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 220 additions and 32 deletions
-
38server/api/v1/sys_initdb.go
-
5server/api/v1/sys_system.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/request/sys_init.go
-
40server/service/sys_initdb.go
-
54server/utils/init_db.go
-
7server/utils/mysql.go
@ -0,0 +1,38 @@ |
|||
package v1 |
|||
|
|||
import ( |
|||
"gin-vue-admin/global" |
|||
"gin-vue-admin/model/request" |
|||
"gin-vue-admin/model/response" |
|||
"gin-vue-admin/service" |
|||
|
|||
"go.uber.org/zap" |
|||
|
|||
"github.com/gin-gonic/gin" |
|||
) |
|||
|
|||
// @Tags InitDB
|
|||
// @Summary 初始化用户数据库
|
|||
// @Produce application/json
|
|||
// @Param data body request.InitDB true "初始化数据库参数"
|
|||
// @Success 200 {string} string "{"code":0,"data":{},"msg":"自动创建数据库成功"}"
|
|||
// @Router /initdb [post]
|
|||
func InitDB(c *gin.Context) { |
|||
if global.GVA_DB != nil { |
|||
global.GVA_LOG.Error("非法访问") |
|||
response.FailWithMessage("非法访问", c) |
|||
return |
|||
} |
|||
var dbInfo request.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 request |
|||
|
|||
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/request" |
|||
"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 request.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