mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
refactor: config management
This commit is contained in:
parent
eb9ede5a4e
commit
53ae1a1ef9
12 changed files with 405 additions and 203 deletions
|
@ -2,10 +2,15 @@ package config
|
|||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"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/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type EditConfigJson struct {
|
||||
|
@ -14,15 +19,39 @@ type EditConfigJson struct {
|
|||
|
||||
func EditConfig(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
var request EditConfigJson
|
||||
err := c.BindJSON(&request)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
var json struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Filepath string `json:"filepath" binding:"required"`
|
||||
NewFilepath string `json:"new_filepath" binding:"required"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if !api.BindAndValid(c, &json) {
|
||||
return
|
||||
}
|
||||
path := nginx.GetConfPath("/", name)
|
||||
content := request.Content
|
||||
|
||||
path := json.Filepath
|
||||
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"message": "filepath is not under the nginx conf path",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"message": "new filepath is not under the nginx conf path",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
c.JSON(http.StatusNotFound, gin.H{
|
||||
"message": "file not found",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
content := json.Content
|
||||
origContent, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
|
@ -37,8 +66,28 @@ func EditConfig(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
output := nginx.Reload()
|
||||
g := query.ChatGPTLog
|
||||
|
||||
// handle rename
|
||||
if path != json.NewFilepath {
|
||||
if helper.FileExists(json.NewFilepath) {
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||
"message": "File exists",
|
||||
})
|
||||
return
|
||||
}
|
||||
err := os.Rename(json.Filepath, json.NewFilepath)
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// update ChatGPT record
|
||||
_, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete()
|
||||
_, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath)
|
||||
}
|
||||
|
||||
output := nginx.Reload()
|
||||
if nginx.GetLogLevel(output) >= nginx.Warn {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": output,
|
||||
|
@ -46,5 +95,21 @@ func EditConfig(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
GetConfig(c)
|
||||
chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
|
||||
if err != nil {
|
||||
api.ErrHandler(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
if chatgpt.Content == nil {
|
||||
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, config.Config{
|
||||
Name: name,
|
||||
Content: content,
|
||||
ChatGPTMessages: chatgpt.Content,
|
||||
FilePath: json.NewFilepath,
|
||||
ModifiedAt: time.Now(),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue