mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
feat(config): sync performance settings
This commit is contained in:
parent
2d3a5e2a16
commit
3502227510
6 changed files with 73 additions and 56 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
)
|
||||
|
|
|
@ -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
59
internal/config/save.go
Normal 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
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue