diff --git a/api/config/modify.go b/api/config/modify.go index a53df2db..48ce1901 100644 --- a/api/config/modify.go +++ b/api/config/modify.go @@ -3,7 +3,6 @@ package config import ( "net/http" "net/url" - "os" "path/filepath" "time" @@ -55,36 +54,15 @@ func EditConfig(c *gin.Context) { return } - content := json.Content - origContent, err := os.ReadFile(absPath) - if err != nil { - cosy.ErrHandler(c, err) - return - } - - err = config.CheckAndCreateHistory(absPath, content) - if err != nil { - cosy.ErrHandler(c, err) - return - } - - if content != "" && content != string(origContent) { - err = os.WriteFile(absPath, []byte(content), 0644) - if err != nil { - cosy.ErrHandler(c, err) - return - } - } - q := query.Config cfg, err := q.Assign(field.Attrs(&model.Config{ - Name: filepath.Base(absPath), + Filepath: absPath, })).Where(q.Filepath.Eq(absPath)).FirstOrCreate() if err != nil { - cosy.ErrHandler(c, err) return } + // Update database record _, err = q.Where(q.Filepath.Eq(absPath)). Select(q.SyncNodeIds, q.SyncOverwrite). Updates(&model.Config{ @@ -92,29 +70,20 @@ func EditConfig(c *gin.Context) { SyncOverwrite: json.SyncOverwrite, }) if err != nil { - cosy.ErrHandler(c, err) return } - // use the new values cfg.SyncNodeIds = json.SyncNodeIds cfg.SyncOverwrite = json.SyncOverwrite - g := query.ChatGPTLog - err = config.SyncToRemoteServer(cfg) + content := json.Content + err = config.Save(absPath, content, cfg) if err != nil { cosy.ErrHandler(c, err) return } - output := nginx.Reload() - if nginx.GetLogLevel(output) >= nginx.Warn { - c.JSON(http.StatusInternalServerError, gin.H{ - "message": output, - }) - return - } - + g := query.ChatGPTLog chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate() if err != nil { cosy.ErrHandler(c, err) diff --git a/api/nginx/performance.go b/api/nginx/performance.go index b976beec..ee715738 100644 --- a/api/nginx/performance.go +++ b/api/nginx/performance.go @@ -3,6 +3,7 @@ package nginx import ( "net/http" + "github.com/0xJacky/Nginx-UI/internal/config" "github.com/0xJacky/Nginx-UI/internal/nginx" "github.com/gin-gonic/gin" "github.com/uozi-tech/cosy" @@ -21,12 +22,12 @@ func GetPerformanceSettings(c *gin.Context) { // UpdatePerformanceSettings updates Nginx performance settings func UpdatePerformanceSettings(c *gin.Context) { - var perfOpt nginx.PerfOpt + var perfOpt config.PerfOpt if !cosy.BindAndValid(c, &perfOpt) { return } - err := nginx.UpdatePerfOpt(&perfOpt) + err := config.UpdatePerfOpt(&perfOpt) if err != nil { cosy.ErrHandler(c, err) return diff --git a/internal/config/errors.go b/internal/config/errors.go index feec1b01..0e6d4482 100644 --- a/internal/config/errors.go +++ b/internal/config/errors.go @@ -5,4 +5,5 @@ import "github.com/uozi-tech/cosy" var ( e = cosy.NewErrorScope("config") ErrPathIsNotUnderTheNginxConfDir = e.New(50006, "path: {0} is not under the nginx conf dir: {1}") + ErrDstFileExists = e.New(50007, "destination file: {0} already exists") ) diff --git a/internal/nginx/perf_opt.go b/internal/config/perf_opt.go similarity index 91% rename from internal/nginx/perf_opt.go rename to internal/config/perf_opt.go index bd896003..6b84bb5c 100644 --- a/internal/nginx/perf_opt.go +++ b/internal/config/perf_opt.go @@ -1,11 +1,10 @@ -package nginx +package config import ( - "fmt" "os" "sort" - "time" + "github.com/0xJacky/Nginx-UI/internal/nginx" "github.com/pkg/errors" "github.com/tufanbarisyildirim/gonginx/config" "github.com/tufanbarisyildirim/gonginx/dumper" @@ -28,7 +27,7 @@ type PerfOpt struct { // UpdatePerfOpt updates the Nginx performance optimization settings func UpdatePerfOpt(opt *PerfOpt) error { - confPath := GetConfPath("nginx.conf") + confPath := nginx.GetConfPath("nginx.conf") if confPath == "" { return errors.New("failed to get nginx.conf path") } @@ -39,13 +38,6 @@ func UpdatePerfOpt(opt *PerfOpt) error { return errors.Wrap(err, "failed to read nginx.conf") } - // Create a backup file - backupPath := fmt.Sprintf("%s.backup.%d", confPath, time.Now().Unix()) - err = os.WriteFile(backupPath, content, 0644) - if err != nil { - return errors.Wrap(err, "failed to create backup file") - } - // Parse the configuration p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr()) conf, err := p.Parse() @@ -59,13 +51,8 @@ func UpdatePerfOpt(opt *PerfOpt) error { // Dump the updated configuration updatedConf := dumper.DumpBlock(conf.Block, dumper.IndentedStyle) - // Write the updated configuration - err = os.WriteFile(confPath, []byte(updatedConf), 0644) - if err != nil { - return errors.Wrap(err, "failed to write updated nginx.conf") - } + return Save(confPath, updatedConf, nil) - return nil } // updateNginxConfig updates the performance settings in the Nginx configuration diff --git a/internal/config/save.go b/internal/config/save.go new file mode 100644 index 00000000..72efc02d --- /dev/null +++ b/internal/config/save.go @@ -0,0 +1,59 @@ +package config + +import ( + "fmt" + "os" + + "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" + "gorm.io/gen/field" +) + +func Save(absPath string, content string, cfg *model.Config) (err error) { + q := query.Config + if cfg == nil { + cfg, err = q.Assign(field.Attrs(&model.Config{ + Filepath: absPath, + })).Where(q.Filepath.Eq(absPath)).FirstOrCreate() + if err != nil { + return + } + } + + if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) { + return ErrPathIsNotUnderTheNginxConfDir + } + + origContent, err := os.ReadFile(absPath) + if err != nil { + return + } + + if content == string(origContent) { + return + } + + err = CheckAndCreateHistory(absPath, content) + if err != nil { + return + } + + err = os.WriteFile(absPath, []byte(content), 0644) + if err != nil { + return + } + + output := nginx.Reload() + if nginx.GetLogLevel(output) >= nginx.Warn { + return fmt.Errorf("%s", output) + } + + err = SyncToRemoteServer(cfg) + if err != nil { + return + } + + return +} diff --git a/internal/config/sync.go b/internal/config/sync.go index d7f4151a..61a89f36 100644 --- a/internal/config/sync.go +++ b/internal/config/sync.go @@ -55,7 +55,7 @@ func SyncToRemoteServer(c *model.Config) (err error) { } q := query.Environment - envs, _ := q.Where(q.ID.In(c.SyncNodeIds...)).Find() + envs, _ := q.Where(q.ID.In(c.SyncNodeIds...), q.Enabled.Is(true)).Find() for _, env := range envs { go func() { err := payload.deploy(env, c, payloadBytes)