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.

94 lines
2.9 KiB

3 years ago
  1. package system
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "github.com/flipped-aurora/gin-vue-admin/server/config"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  9. "github.com/flipped-aurora/gin-vue-admin/server/source/example"
  10. "github.com/flipped-aurora/gin-vue-admin/server/source/system"
  11. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  12. uuid "github.com/satori/go.uuid"
  13. "gorm.io/driver/mysql"
  14. "gorm.io/gorm"
  15. )
  16. // writeMysqlConfig mysql回写配置
  17. // Author [SliverHorn](https://github.com/SliverHorn)
  18. // Author [songzhibin97](https://github.com/songzhibin97)
  19. func (initDBService *InitDBService) writeMysqlConfig(mysql config.Mysql) error {
  20. global.GVA_CONFIG.Mysql = mysql
  21. cs := utils.StructToMap(global.GVA_CONFIG)
  22. for k, v := range cs {
  23. global.GVA_VP.Set(k, v)
  24. }
  25. global.GVA_VP.Set("jwt.signing-key", uuid.NewV4().String())
  26. return global.GVA_VP.WriteConfig()
  27. }
  28. // initMsqlDB 创建数据库并初始化 mysql
  29. // Author [piexlmax](https://github.com/piexlmax)
  30. // Author [SliverHorn](https://github.com/SliverHorn)
  31. // Author: [songzhibin97](https://github.com/songzhibin97)
  32. func (initDBService *InitDBService) initMsqlDB(conf request.InitDB) error {
  33. dsn := conf.MysqlEmptyDsn()
  34. createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName)
  35. if err := initDBService.createDatabase(dsn, "mysql", createSql); err != nil {
  36. return err
  37. } // 创建数据库
  38. mysqlConfig := conf.ToMysqlConfig()
  39. if mysqlConfig.Dbname == "" {
  40. return nil
  41. } // 如果没有数据库名, 则跳出初始化数据
  42. if db, err := gorm.Open(mysql.New(mysql.Config{
  43. DSN: mysqlConfig.Dsn(), // DSN data source name
  44. DefaultStringSize: 191, // string 类型字段的默认长度
  45. SkipInitializeWithVersion: true, // 根据版本自动配置
  46. }), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil {
  47. return nil
  48. } else {
  49. global.GVA_DB = db
  50. }
  51. if err := initDBService.initTables(); err != nil {
  52. global.GVA_DB = nil
  53. return err
  54. }
  55. if err := initDBService.initMysqlData(); err != nil {
  56. global.GVA_DB = nil
  57. return err
  58. }
  59. if err := initDBService.writeMysqlConfig(mysqlConfig); err != nil {
  60. return err
  61. }
  62. global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
  63. return nil
  64. }
  65. // initData mysql 初始化数据
  66. // Author [SliverHorn](https://github.com/SliverHorn)
  67. // Author: [songzhibin97](https://github.com/songzhibin97)
  68. func (initDBService *InitDBService) initMysqlData() error {
  69. return model.MysqlDataInitialize(
  70. system.Api,
  71. system.User,
  72. system.Casbin,
  73. system.BaseMenu,
  74. system.Authority,
  75. system.Dictionary,
  76. system.UserAuthority,
  77. system.DataAuthorities,
  78. system.AuthoritiesMenus,
  79. system.DictionaryDetail,
  80. system.ViewAuthorityMenuMysql,
  81. example.FileMysql,
  82. )
  83. }