sys_initdb.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  6. "go.uber.org/zap"
  7. "github.com/gin-gonic/gin"
  8. )
  9. type DBApi struct {
  10. }
  11. // @Tags InitDB
  12. // @Summary 初始化用户数据库
  13. // @Produce application/json
  14. // @Param data body request.InitDB true "初始化数据库参数"
  15. // @Success 200 {string} string "{"code":0,"data":{},"msg":"自动创建数据库成功"}"
  16. // @Router /init/initdb [post]
  17. func (i *DBApi) InitDB(c *gin.Context) {
  18. if global.GVA_DB != nil {
  19. global.GVA_LOG.Error("已存在数据库配置!")
  20. response.FailWithMessage("已存在数据库配置", c)
  21. return
  22. }
  23. var dbInfo request.InitDB
  24. if err := c.ShouldBindJSON(&dbInfo); err != nil {
  25. global.GVA_LOG.Error("参数校验不通过!", zap.Any("err", err))
  26. response.FailWithMessage("参数校验不通过", c)
  27. return
  28. }
  29. if err := initDBService.InitDB(dbInfo); err != nil {
  30. global.GVA_LOG.Error("自动创建数据库失败!", zap.Any("err", err))
  31. response.FailWithMessage("自动创建数据库失败,请查看后台日志,检查后在进行初始化", c)
  32. return
  33. }
  34. response.OkWithData("自动创建数据库成功", c)
  35. }
  36. // @Tags CheckDB
  37. // @Summary 初始化用户数据库
  38. // @Produce application/json
  39. // @Success 200 {string} string "{"code":0,"data":{},"msg":"探测完成"}"
  40. // @Router /init/checkdb [post]
  41. func (i *DBApi) CheckDB(c *gin.Context) {
  42. if global.GVA_DB != nil {
  43. global.GVA_LOG.Info("数据库无需初始化")
  44. response.OkWithDetailed(gin.H{"needInit": false}, "数据库无需初始化", c)
  45. return
  46. } else {
  47. global.GVA_LOG.Info("前往初始化数据库")
  48. response.OkWithDetailed(gin.H{"needInit": true}, "前往初始化数据库", c)
  49. return
  50. }
  51. }