mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat: config history
This commit is contained in:
parent
771859d3b8
commit
57b8dfd2f9
15 changed files with 823 additions and 75 deletions
51
internal/config/history.go
Normal file
51
internal/config/history.go
Normal 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
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue