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.

56 lines
1.7 KiB

3 years ago
4 years ago
  1. package system
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model/common/response"
  5. "gin-vue-admin/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. }