You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

4 years ago
  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. // InitDB
  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.Error(err))
  26. response.FailWithMessage("参数校验不通过", c)
  27. return
  28. }
  29. if err := initDBService.InitDB(dbInfo); err != nil {
  30. global.GVA_LOG.Error("自动创建数据库失败!", zap.Error(err))
  31. response.FailWithMessage("自动创建数据库失败,请查看后台日志,检查后在进行初始化", c)
  32. return
  33. }
  34. response.OkWithData("自动创建数据库成功", c)
  35. }
  36. // CheckDB
  37. // @Tags CheckDB
  38. // @Summary 初始化用户数据库
  39. // @Produce application/json
  40. // @Success 200 {string} string "{"code":0,"data":{},"msg":"探测完成"}"
  41. // @Router /init/checkdb [post]
  42. func (i *DBApi) CheckDB(c *gin.Context) {
  43. if global.GVA_DB != nil {
  44. global.GVA_LOG.Info("数据库无需初始化")
  45. response.OkWithDetailed(gin.H{"needInit": false}, "数据库无需初始化", c)
  46. return
  47. } else {
  48. global.GVA_LOG.Info("前往初始化数据库")
  49. response.OkWithDetailed(gin.H{"needInit": true}, "前往初始化数据库", c)
  50. return
  51. }
  52. }