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.

58 lines
1.6 KiB

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