feat: config history

This commit is contained in:
Jacky 2025-04-06 10:40:49 +08:00
parent 771859d3b8
commit 57b8dfd2f9
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
15 changed files with 823 additions and 75 deletions

View file

@ -0,0 +1,51 @@
package config
import (
"os"
"path/filepath"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/uozi-tech/cosy/logger"
)
// CheckAndCreateHistory compares the provided content with the current content of the file
// at the specified path and creates a history record if they are different.
// The path must be under nginx.GetConfPath().
func CheckAndCreateHistory(path string, content string) error {
// Check if path is under nginx.GetConfPath()
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
return ErrPathIsNotUnderTheNginxConfDir
}
// Read the current content of the file
currentContent, err := os.ReadFile(path)
if err != nil {
return err
}
// Compare the contents
if string(currentContent) == content {
// Contents are identical, no need to create history
return nil
}
// Contents are different, create a history record (config backup)
backup := &model.ConfigBackup{
Name: filepath.Base(path),
FilePath: path,
Content: string(currentContent),
}
// Save the backup to the database
cb := query.ConfigBackup
err = cb.Create(backup)
if err != nil {
logger.Error("Failed to create config backup:", err)
return err
}
return nil
}

View file

@ -7,6 +7,7 @@ import (
"runtime"
"sync"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
@ -23,6 +24,11 @@ func Save(name string, content string, overwrite bool, envGroupId uint64, syncNo
return ErrDstFileExists
}
err = config.CheckAndCreateHistory(path, content)
if err != nil {
return
}
err = os.WriteFile(path, []byte(content), 0644)
if err != nil {
return

View file

@ -7,6 +7,7 @@ import (
"runtime"
"sync"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/internal/notification"
@ -23,6 +24,11 @@ func Save(name string, content string, overwrite bool, syncNodeIds []uint64, pos
return ErrDstFileExists
}
err = config.CheckAndCreateHistory(path, content)
if err != nil {
return
}
err = os.WriteFile(path, []byte(content), 0644)
if err != nil {
return