Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions api/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/sashabaranov/go-openai"
"net/http"
"os"
"path/filepath"
)

type APIConfigResp struct {
Expand All @@ -19,30 +20,30 @@ type APIConfigResp struct {
}

func GetConfig(c *gin.Context) {
name := c.Param("name")
relativePath := c.Param("path")

path := nginx.GetConfPath("/", name)
if !helper.IsUnderDirectory(path, nginx.GetConfPath()) {
absPath := nginx.GetConfPath(relativePath)
if !helper.IsUnderDirectory(absPath, nginx.GetConfPath()) {
c.JSON(http.StatusForbidden, gin.H{
"message": "path is not under the nginx conf path",
})
return
}

stat, err := os.Stat(path)
stat, err := os.Stat(absPath)
if err != nil {
api.ErrHandler(c, err)
return
}

content, err := os.ReadFile(path)
content, err := os.ReadFile(absPath)
if err != nil {
api.ErrHandler(c, err)
return
}
q := query.Config
g := query.ChatGPTLog
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate()
chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
if err != nil {
api.ErrHandler(c, err)
return
Expand All @@ -52,7 +53,7 @@ func GetConfig(c *gin.Context) {
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
}

cfg, err := q.Where(q.Filepath.Eq(path)).FirstOrInit()
cfg, err := q.Where(q.Filepath.Eq(absPath)).FirstOrInit()
if err != nil {
api.ErrHandler(c, err)
return
Expand All @@ -63,8 +64,9 @@ func GetConfig(c *gin.Context) {
Name: stat.Name(),
Content: string(content),
ChatGPTMessages: chatgpt.Content,
FilePath: path,
FilePath: absPath,
ModifiedAt: stat.ModTime(),
Dir: filepath.Dir(relativePath),
},
SyncNodeIds: cfg.SyncNodeIds,
SyncOverwrite: cfg.SyncOverwrite,
Expand Down
67 changes: 18 additions & 49 deletions api/config/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/sashabaranov/go-openai"
"net/http"
"os"
"path/filepath"
"time"
)

Expand All @@ -19,11 +20,9 @@ type EditConfigJson struct {
}

func EditConfig(c *gin.Context) {
name := c.Param("name")
relativePath := c.Param("path")
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"`
SyncOverwrite bool `json:"sync_overwrite"`
SyncNodeIds []uint64 `json:"sync_node_ids"`
Expand All @@ -32,55 +31,39 @@ func EditConfig(c *gin.Context) {
return
}

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 !helper.FileExists(path) {
absPath := nginx.GetConfPath(relativePath)
if !helper.FileExists(absPath) {
c.JSON(http.StatusNotFound, gin.H{
"message": "file not found",
})
return
}

content := json.Content
origContent, err := os.ReadFile(path)
origContent, err := os.ReadFile(absPath)
if err != nil {
api.ErrHandler(c, err)
return
}

if content != "" && content != string(origContent) {
err = os.WriteFile(path, []byte(content), 0644)
err = os.WriteFile(absPath, []byte(content), 0644)
if err != nil {
api.ErrHandler(c, err)
return
}
}

q := query.Config
cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate()
cfg, err := q.Where(q.Filepath.Eq(absPath)).FirstOrCreate()
if err != nil {
api.ErrHandler(c, err)
return
}

_, err = q.Where(q.Filepath.Eq(json.Filepath)).
Select(q.Name, q.Filepath, q.SyncNodeIds, q.SyncOverwrite).
_, err = q.Where(q.Filepath.Eq(absPath)).
Select(q.SyncNodeIds, q.SyncOverwrite).
Updates(&model.Config{
Name: json.Name,
Filepath: json.NewFilepath,
SyncNodeIds: json.SyncNodeIds,
SyncOverwrite: json.SyncOverwrite,
})
Expand All @@ -89,27 +72,12 @@ func EditConfig(c *gin.Context) {
return
}

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)
}
// use the new values
cfg.SyncNodeIds = json.SyncNodeIds
cfg.SyncOverwrite = json.SyncOverwrite

err = config.SyncToRemoteServer(cfg, json.NewFilepath)
g := query.ChatGPTLog
err = config.SyncToRemoteServer(cfg, absPath)
if err != nil {
api.ErrHandler(c, err)
return
Expand All @@ -123,7 +91,7 @@ func EditConfig(c *gin.Context) {
return
}

chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate()
chatgpt, err := g.Where(g.Name.Eq(absPath)).FirstOrCreate()
if err != nil {
api.ErrHandler(c, err)
return
Expand All @@ -134,10 +102,11 @@ func EditConfig(c *gin.Context) {
}

c.JSON(http.StatusOK, config.Config{
Name: name,
Name: filepath.Base(absPath),
Content: content,
ChatGPTMessages: chatgpt.Content,
FilePath: json.NewFilepath,
FilePath: absPath,
ModifiedAt: time.Now(),
Dir: filepath.Dir(relativePath),
})
}
9 changes: 7 additions & 2 deletions api/config/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ 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/uozi-tech/cosy/logger"
"net/http"
"os"
"strings"
)

func Rename(c *gin.Context) {
Expand Down Expand Up @@ -77,7 +79,10 @@ func Rename(c *gin.Context) {
_, _ = g.Where(g.Name.Like(origFullPath+"%")).Update(g.Name, g.Name.Replace(origFullPath, newFullPath))
}

_, err = q.Where(q.Filepath.Eq(origFullPath)).Update(q.Filepath, newFullPath)
_, err = q.Where(q.Filepath.Eq(origFullPath)).Updates(&model.Config{
Filepath: newFullPath,
Name: json.NewName,
})
if err != nil {
api.ErrHandler(c, err)
return
Expand All @@ -92,6 +97,6 @@ func Rename(c *gin.Context) {
}

c.JSON(http.StatusOK, gin.H{
"message": "ok",
"path": strings.TrimLeft(newFullPath, nginx.GetConfPath()),
})
}
4 changes: 2 additions & 2 deletions api/config/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ func InitRouter(r *gin.RouterGroup) {
r.GET("config_base_path", GetBasePath)

r.GET("configs", GetConfigs)
r.GET("configs/*name", GetConfig)
r.GET("configs/*path", GetConfig)
r.POST("configs", AddConfig)
r.POST("configs/*name", EditConfig)
r.POST("configs/*path", EditConfig)

o := r.Group("", middleware.RequireSecureSession())
{
Expand Down
31 changes: 31 additions & 0 deletions api/notification/live.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package notification

import (
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"io"
)

func Live(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")

evtChan := make(chan *model.Notification)

notification.SetClient(c, evtChan)

notify := c.Writer.CloseNotify()
go func() {
<-notify
notification.RemoveClient(c)
}()

for n := range evtChan {
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", n)
return false
})
}
}
2 changes: 2 additions & 0 deletions api/notification/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ func InitRouter(r *gin.RouterGroup) {
r.GET("notifications/:id", Get)
r.DELETE("notifications/:id", Destroy)
r.DELETE("notifications", DestroyAll)

r.GET("notifications/live", Live)
}
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"pinia-plugin-persistedstate": "^4.1.2",
"reconnecting-websocket": "^4.4.0",
"sortablejs": "^1.15.3",
"sse.js": "^2.5.0",
"universal-cookie": "^7.2.2",
"unocss": "^0.63.6",
"vite-plugin-build-id": "0.5.0",
Expand Down
8 changes: 8 additions & 0 deletions app/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Config {
modified_at: string
sync_node_ids?: number[]
sync_overwrite?: false
dir: string
}

class ConfigCurd extends Curd<Config> {
Expand All @@ -25,7 +26,7 @@ class ConfigCurd extends Curd<Config> {
return http.post('/config_mkdir', { base_path: basePath, folder_name: name })
}

rename(basePath: string, origName: string, newName: string, syncNodeIds: number[]) {
rename(basePath: string, origName: string, newName: string, syncNodeIds?: number[]) {
return http.post('/config_rename', {
base_path: basePath,
orig_name: origName,
Expand Down
Loading
Loading