feat(config): sync performance settings

This commit is contained in:
Jacky 2025-04-12 01:43:45 +00:00
parent 2d3a5e2a16
commit 3502227510
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
6 changed files with 73 additions and 56 deletions

View file

@ -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")
)

View file

@ -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

59
internal/config/save.go Normal file
View file

@ -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
}

View file

@ -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)