提交 517e0572 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

cmd剔除 初始数据结构转移 修复初始携带apibug

上级 18ef7271
......@@ -16,7 +16,7 @@ import (
// @Produce application/json
// @Param data body request.InitDB true "初始化数据库参数"
// @Success 200 {string} string "{"code":0,"data":{},"msg":"自动创建数据库成功"}"
// @Router /initdb [post]
// @Router /init/initdb [post]
func InitDB(c *gin.Context) {
if global.GVA_DB != nil {
global.GVA_LOG.Error("非法访问")
......@@ -36,3 +36,24 @@ func InitDB(c *gin.Context) {
}
response.OkWithData("自动创建数据库成功", c)
}
// @Tags CheckDB
// @Summary 初始化用户数据库
// @Produce application/json
// @Success 200 {string} string "{"code":0,"data":{},"msg":"探测完成"}"
// @Router /init/checkdb [post]
func CheckDB(c *gin.Context) {
if global.GVA_DB == nil {
global.GVA_LOG.Info("数据库无需初始化")
response.OkWithDetailed(gin.H{
"needInit":false,
},"数据库无需初始化", c)
return
}else{
global.GVA_LOG.Info("前往初始化数据库")
response.OkWithDetailed(gin.H{
"needInit":true,
},"前往初始化数据库", c)
return
}
}
package initialize
import (
v1 "gin-vue-admin/api/v1"
_ "gin-vue-admin/docs"
"gin-vue-admin/global"
"gin-vue-admin/middleware"
......@@ -17,12 +16,11 @@ import (
func Routers() *gin.Engine {
var Router = gin.Default()
Router.POST("initdb", v1.InitDB)
Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.Path)) // 为用户头像和文件提供静态地址
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
global.GVA_LOG.Info("use middleware logger")
// 跨域
Router.Use(middleware.Cors())
//Router.Use(middleware.Cors()) // 如需跨域可以打开
global.GVA_LOG.Info("use middleware cors")
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
global.GVA_LOG.Info("register swagger handler")
......@@ -30,6 +28,7 @@ func Routers() *gin.Engine {
PublicGroup := Router.Group("")
{
router.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
router.InitInitRouter(PublicGroup) // 自动初始化相关
}
PrivateGroup := Router.Group("")
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
......
package model
type InitDBFunc interface {
Init() (err error)
}
package router
import (
"gin-vue-admin/api/v1"
"github.com/gin-gonic/gin"
)
func InitInitRouter(Router *gin.RouterGroup) {
ApiRouter := Router.Group("init")
{
ApiRouter.POST("initbd", v1.InitDB) // 创建Api
ApiRouter.POST("checkdb", v1.CheckDB) // 创建Api
}
}
package service
import (
"database/sql"
"fmt"
"gin-vue-admin/cmd/source"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/source"
"github.com/spf13/viper"
)
//@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: 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
}
func initDB(InitDBFunctions ...model.InitDBFunc) (err error) {
for _, v := range InitDBFunctions {
err = v.Init()
if err != nil {
return err
}
}
return nil
}
//@author: [songzhibin97](https://github.com/songzhibin97)
//@function: InitDB
//@description: 创建数据库并初始化
......@@ -23,7 +68,7 @@ func InitDB(conf request.InitDB) error {
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 := source.CreateTable(dsn, "mysql", createSql); err != nil {
if err := createTable(dsn, "mysql", createSql); err != nil {
return err
}
setting := map[string]interface{}{
......@@ -32,9 +77,23 @@ func InitDB(conf request.InitDB) error {
"mysql.username": conf.UserName,
"mysql.password": conf.Password,
}
if err := source.WriteConfig(global.GVA_VP, setting); err != nil {
if err := writeConfig(global.GVA_VP, setting); err != nil {
return err
}
err := initDB(source.Admin,
source.Api,
source.AuthorityMenu,
source.Authority,
source.AuthoritiesMenus,
source.Casbin,
source.DataAuthorities,
source.Dictionary,
source.DictionaryDetail,
source.File,
source.BaseMenu,
source.Workflow)
if err!=nil {
return err
}
source.InitDB()
return nil
}
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
package information
package source
import (
"gin-vue-admin/global"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册