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.

40 lines
1.1 KiB

  1. package service
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/utils"
  7. )
  8. //@author: [songzhibin97](https://github.com/songzhibin97)
  9. //@function: InitDB
  10. //@description: 创建数据库并初始化
  11. //@param: authorityId string
  12. //@return: err error, treeMap map[string][]model.SysMenu
  13. func InitDB(conf model.InitDB) error {
  14. if conf.Host == "" {
  15. conf.Host = "127.0.0.1"
  16. }
  17. if conf.Port == "" {
  18. conf.Port = "3306"
  19. }
  20. dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/", conf.UserName, conf.Password, conf.Host, conf.Port)
  21. fmt.Println(dsn)
  22. createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName)
  23. if err := utils.CreateTable(dsn, "mysql", createSql); err != nil {
  24. return err
  25. }
  26. setting := map[string]interface{}{
  27. "mysql.path": fmt.Sprintf("%s:%s", conf.Host, conf.Port),
  28. "mysql.db-name": conf.DBName,
  29. "mysql.username": conf.UserName,
  30. "mysql.password": conf.Password,
  31. }
  32. if err := utils.WriteConfig(global.GVA_VP, setting); err != nil {
  33. return err
  34. }
  35. utils.InitDB()
  36. return nil
  37. }