api_ignore.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package system
  2. import (
  3. "context"
  4. sysModel "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  5. "github.com/flipped-aurora/gin-vue-admin/server/service/system"
  6. "github.com/pkg/errors"
  7. "gorm.io/gorm"
  8. )
  9. type initApiIgnore struct{}
  10. const initOrderApiIgnore = initOrderApi + 1
  11. // auto run
  12. func init() {
  13. system.RegisterInit(initOrderApiIgnore, &initApiIgnore{})
  14. }
  15. func (i *initApiIgnore) InitializerName() string {
  16. return sysModel.SysIgnoreApi{}.TableName()
  17. }
  18. func (i *initApiIgnore) MigrateTable(ctx context.Context) (context.Context, error) {
  19. db, ok := ctx.Value("db").(*gorm.DB)
  20. if !ok {
  21. return ctx, system.ErrMissingDBContext
  22. }
  23. return ctx, db.AutoMigrate(&sysModel.SysIgnoreApi{})
  24. }
  25. func (i *initApiIgnore) TableCreated(ctx context.Context) bool {
  26. db, ok := ctx.Value("db").(*gorm.DB)
  27. if !ok {
  28. return false
  29. }
  30. return db.Migrator().HasTable(&sysModel.SysIgnoreApi{})
  31. }
  32. func (i *initApiIgnore) InitializeData(ctx context.Context) (context.Context, error) {
  33. db, ok := ctx.Value("db").(*gorm.DB)
  34. if !ok {
  35. return ctx, system.ErrMissingDBContext
  36. }
  37. entities := []sysModel.SysIgnoreApi{
  38. {Method: "GET", Path: "/swagger/*any"},
  39. {Method: "GET", Path: "/api/freshCasbin"},
  40. {Method: "GET", Path: "/uploads/file/*filepath"},
  41. {Method: "GET", Path: "/health"},
  42. {Method: "HEAD", Path: "/uploads/file/*filepath"},
  43. {Method: "POST", Path: "/autoCode/llmAuto"},
  44. {Method: "POST", Path: "/system/reloadSystem"},
  45. {Method: "POST", Path: "/base/login"},
  46. {Method: "POST", Path: "/base/captcha"},
  47. {Method: "POST", Path: "/init/initdb"},
  48. {Method: "POST", Path: "/init/checkdb"},
  49. {Method: "GET", Path: "/info/getInfoDataSource"},
  50. {Method: "GET", Path: "/info/getInfoPublic"},
  51. }
  52. if err := db.Create(&entities).Error; err != nil {
  53. return ctx, errors.Wrap(err, sysModel.SysIgnoreApi{}.TableName()+"表数据初始化失败!")
  54. }
  55. next := context.WithValue(ctx, i.InitializerName(), entities)
  56. return next, nil
  57. }
  58. func (i *initApiIgnore) DataInserted(ctx context.Context) bool {
  59. db, ok := ctx.Value("db").(*gorm.DB)
  60. if !ok {
  61. return false
  62. }
  63. if errors.Is(db.Where("path = ? AND method = ?", "/swagger/*any", "GET").
  64. First(&sysModel.SysIgnoreApi{}).Error, gorm.ErrRecordNotFound) {
  65. return false
  66. }
  67. return true
  68. }