feat: deploy config to remote nodes #359

This commit is contained in:
Jacky 2024-07-26 13:53:38 +08:00
parent e75dce92ad
commit 1c1da92363
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
46 changed files with 1480 additions and 605 deletions

View file

@ -5,6 +5,7 @@ import (
"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/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
@ -24,6 +25,8 @@ func EditConfig(c *gin.Context) {
Filepath string `json:"filepath" binding:"required"`
NewFilepath string `json:"new_filepath" binding:"required"`
Content string `json:"content"`
Overwrite bool `json:"overwrite"`
SyncNodeIds []int `json:"sync_node_ids"`
}
if !api.BindAndValid(c, &json) {
return
@ -66,8 +69,25 @@ func EditConfig(c *gin.Context) {
}
}
g := query.ChatGPTLog
q := query.Config
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
if err != nil {
api.ErrHandler(c, err)
return
}
_, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{
Name: json.Name,
Filepath: json.NewFilepath,
SyncNodeIds: json.SyncNodeIds,
SyncOverwrite: json.Overwrite,
})
if err != nil {
api.ErrHandler(c, err)
return
}
g := query.ChatGPTLog
// handle rename
if path != json.NewFilepath {
if helper.FileExists(json.NewFilepath) {
@ -87,6 +107,12 @@ func EditConfig(c *gin.Context) {
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
}
err = config.SyncToRemoteServer(cfg, json.NewFilepath)
if err != nil {
api.ErrHandler(c, err)
return
}
output := nginx.Reload()
if nginx.GetLogLevel(output) >= nginx.Warn {
c.JSON(http.StatusInternalServerError, gin.H{