config_backup.go 911 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package model
  2. import (
  3. "github.com/0xJacky/Nginx-UI/server/internal/logger"
  4. "os"
  5. "path/filepath"
  6. )
  7. type ConfigBackup struct {
  8. Model
  9. Name string `json:"name"`
  10. FilePath string `json:"file_path"`
  11. Content string `json:"content" gorm:"type:text"`
  12. }
  13. type ConfigBackupListItem struct {
  14. Model
  15. Name string `json:"name"`
  16. FilePath string `json:"file_path"`
  17. }
  18. func GetBackupList(path string) (configs []ConfigBackupListItem) {
  19. db.Model(&ConfigBackup{}).
  20. Where(&ConfigBackup{FilePath: path}).
  21. Find(&configs)
  22. return
  23. }
  24. func GetBackup(id int) (config ConfigBackup) {
  25. db.First(&config, id)
  26. return
  27. }
  28. func CreateBackup(path string) {
  29. content, err := os.ReadFile(path)
  30. if err != nil {
  31. logger.Error(err)
  32. }
  33. config := ConfigBackup{Name: filepath.Base(path), FilePath: path, Content: string(content)}
  34. result := db.Create(&config)
  35. if result.Error != nil {
  36. logger.Error(result.Error)
  37. }
  38. }