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

@ -3,7 +3,6 @@ package config
import ( import (
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"time" "time"
@ -55,36 +54,15 @@ func EditConfig(c *gin.Context) {
return 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 q := query.Config
cfg, err := q.Assign(field.Attrs(&model.Config{ cfg, err := q.Assign(field.Attrs(&model.Config{
Name: filepath.Base(absPath), Filepath: absPath,
})).Where(q.Filepath.Eq(absPath)).FirstOrCreate() })).Where(q.Filepath.Eq(absPath)).FirstOrCreate()
if err != nil { if err != nil {
cosy.ErrHandler(c, err)
return return
} }
// Update database record
_, err = q.Where(q.Filepath.Eq(absPath)). _, err = q.Where(q.Filepath.Eq(absPath)).
Select(q.SyncNodeIds, q.SyncOverwrite). Select(q.SyncNodeIds, q.SyncOverwrite).
Updates(&model.Config{ Updates(&model.Config{
@ -92,29 +70,20 @@ func EditConfig(c *gin.Context) {
SyncOverwrite: json.SyncOverwrite, SyncOverwrite: json.SyncOverwrite,
}) })
if err != nil { if err != nil {
cosy.ErrHandler(c, err)
return return
} }
// use the new values
cfg.SyncNodeIds = json.SyncNodeIds cfg.SyncNodeIds = json.SyncNodeIds
cfg.SyncOverwrite = json.SyncOverwrite cfg.SyncOverwrite = json.SyncOverwrite
g := query.ChatGPTLog content := json.Content
err = config.SyncToRemoteServer(cfg) err = config.Save(absPath, content, cfg)
if err != nil { if err != nil {
cosy.ErrHandler(c, err) cosy.ErrHandler(c, err)
return return
} }
output := nginx.Reload() g := query.ChatGPTLog
if nginx.GetLogLevel(output) >= nginx.Warn {
c.JSON(http.StatusInternalServerError, gin.H{
"message": output,
})
return
}
chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate() chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
if err != nil { if err != nil {
cosy.ErrHandler(c, err) cosy.ErrHandler(c, err)

View file

@ -3,6 +3,7 @@ package nginx
import ( import (
"net/http" "net/http"
"github.com/0xJacky/Nginx-UI/internal/config"
"github.com/0xJacky/Nginx-UI/internal/nginx" "github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy" "github.com/uozi-tech/cosy"
@ -21,12 +22,12 @@ func GetPerformanceSettings(c *gin.Context) {
// UpdatePerformanceSettings updates Nginx performance settings // UpdatePerformanceSettings updates Nginx performance settings
func UpdatePerformanceSettings(c *gin.Context) { func UpdatePerformanceSettings(c *gin.Context) {
var perfOpt nginx.PerfOpt var perfOpt config.PerfOpt
if !cosy.BindAndValid(c, &perfOpt) { if !cosy.BindAndValid(c, &perfOpt) {
return return
} }
err := nginx.UpdatePerfOpt(&perfOpt) err := config.UpdatePerfOpt(&perfOpt)
if err != nil { if err != nil {
cosy.ErrHandler(c, err) cosy.ErrHandler(c, err)
return return

View file

@ -5,4 +5,5 @@ import "github.com/uozi-tech/cosy"
var ( var (
e = cosy.NewErrorScope("config") e = cosy.NewErrorScope("config")
ErrPathIsNotUnderTheNginxConfDir = e.New(50006, "path: {0} is not under the nginx conf dir: {1}") 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 ( import (
"fmt"
"os" "os"
"sort" "sort"
"time"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/tufanbarisyildirim/gonginx/config" "github.com/tufanbarisyildirim/gonginx/config"
"github.com/tufanbarisyildirim/gonginx/dumper" "github.com/tufanbarisyildirim/gonginx/dumper"
@ -28,7 +27,7 @@ type PerfOpt struct {
// UpdatePerfOpt updates the Nginx performance optimization settings // UpdatePerfOpt updates the Nginx performance optimization settings
func UpdatePerfOpt(opt *PerfOpt) error { func UpdatePerfOpt(opt *PerfOpt) error {
confPath := GetConfPath("nginx.conf") confPath := nginx.GetConfPath("nginx.conf")
if confPath == "" { if confPath == "" {
return errors.New("failed to get nginx.conf path") 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") 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 // Parse the configuration
p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr()) p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())
conf, err := p.Parse() conf, err := p.Parse()
@ -59,13 +51,8 @@ func UpdatePerfOpt(opt *PerfOpt) error {
// Dump the updated configuration // Dump the updated configuration
updatedConf := dumper.DumpBlock(conf.Block, dumper.IndentedStyle) updatedConf := dumper.DumpBlock(conf.Block, dumper.IndentedStyle)
// Write the updated configuration return Save(confPath, updatedConf, nil)
err = os.WriteFile(confPath, []byte(updatedConf), 0644)
if err != nil {
return errors.Wrap(err, "failed to write updated nginx.conf")
}
return nil
} }
// updateNginxConfig updates the performance settings in the Nginx configuration // 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 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 { for _, env := range envs {
go func() { go func() {
err := payload.deploy(env, c, payloadBytes) err := payload.deploy(env, c, payloadBytes)