|
1 | 1 | package config |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/0xJacky/Nginx-UI/api" |
5 | | - "github.com/0xJacky/Nginx-UI/internal/config" |
6 | | - "github.com/0xJacky/Nginx-UI/internal/helper" |
7 | | - "github.com/0xJacky/Nginx-UI/internal/nginx" |
8 | | - "github.com/0xJacky/Nginx-UI/model" |
9 | | - "github.com/0xJacky/Nginx-UI/query" |
10 | | - "github.com/gin-gonic/gin" |
11 | | - "github.com/sashabaranov/go-openai" |
12 | | - "net/http" |
13 | | - "os" |
14 | | - "time" |
| 4 | + "github.com/0xJacky/Nginx-UI/api" |
| 5 | + "github.com/0xJacky/Nginx-UI/internal/config" |
| 6 | + "github.com/0xJacky/Nginx-UI/internal/helper" |
| 7 | + "github.com/0xJacky/Nginx-UI/internal/nginx" |
| 8 | + "github.com/0xJacky/Nginx-UI/model" |
| 9 | + "github.com/0xJacky/Nginx-UI/query" |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/sashabaranov/go-openai" |
| 12 | + "net/http" |
| 13 | + "os" |
| 14 | + "time" |
15 | 15 | ) |
16 | 16 |
|
17 | 17 | type EditConfigJson struct { |
18 | | - Content string `json:"content" binding:"required"` |
| 18 | + Content string `json:"content" binding:"required"` |
19 | 19 | } |
20 | 20 |
|
21 | 21 | func EditConfig(c *gin.Context) { |
22 | | - name := c.Param("name") |
23 | | - var json struct { |
24 | | - Name string `json:"name" binding:"required"` |
25 | | - Filepath string `json:"filepath" binding:"required"` |
26 | | - NewFilepath string `json:"new_filepath" binding:"required"` |
27 | | - Content string `json:"content"` |
28 | | - Overwrite bool `json:"overwrite"` |
29 | | - SyncNodeIds []int `json:"sync_node_ids"` |
30 | | - } |
31 | | - if !api.BindAndValid(c, &json) { |
32 | | - return |
33 | | - } |
34 | | - |
35 | | - path := json.Filepath |
36 | | - if !helper.IsUnderDirectory(path, nginx.GetConfPath()) { |
37 | | - c.JSON(http.StatusForbidden, gin.H{ |
38 | | - "message": "filepath is not under the nginx conf path", |
39 | | - }) |
40 | | - return |
41 | | - } |
42 | | - |
43 | | - if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) { |
44 | | - c.JSON(http.StatusForbidden, gin.H{ |
45 | | - "message": "new filepath is not under the nginx conf path", |
46 | | - }) |
47 | | - return |
48 | | - } |
49 | | - |
50 | | - if !helper.FileExists(path) { |
51 | | - c.JSON(http.StatusNotFound, gin.H{ |
52 | | - "message": "file not found", |
53 | | - }) |
54 | | - return |
55 | | - } |
56 | | - |
57 | | - content := json.Content |
58 | | - origContent, err := os.ReadFile(path) |
59 | | - if err != nil { |
60 | | - api.ErrHandler(c, err) |
61 | | - return |
62 | | - } |
63 | | - |
64 | | - if content != "" && content != string(origContent) { |
65 | | - err = os.WriteFile(path, []byte(content), 0644) |
66 | | - if err != nil { |
67 | | - api.ErrHandler(c, err) |
68 | | - return |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - q := query.Config |
73 | | - cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate() |
74 | | - if err != nil { |
75 | | - api.ErrHandler(c, err) |
76 | | - return |
77 | | - } |
78 | | - |
79 | | - _, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{ |
80 | | - Name: json.Name, |
81 | | - Filepath: json.NewFilepath, |
82 | | - SyncNodeIds: json.SyncNodeIds, |
83 | | - SyncOverwrite: json.Overwrite, |
84 | | - }) |
85 | | - |
86 | | - if err != nil { |
87 | | - api.ErrHandler(c, err) |
88 | | - return |
89 | | - } |
90 | | - g := query.ChatGPTLog |
91 | | - // handle rename |
92 | | - if path != json.NewFilepath { |
93 | | - if helper.FileExists(json.NewFilepath) { |
94 | | - c.JSON(http.StatusNotAcceptable, gin.H{ |
95 | | - "message": "File exists", |
96 | | - }) |
97 | | - return |
98 | | - } |
99 | | - err := os.Rename(json.Filepath, json.NewFilepath) |
100 | | - if err != nil { |
101 | | - api.ErrHandler(c, err) |
102 | | - return |
103 | | - } |
104 | | - |
105 | | - // update ChatGPT record |
106 | | - _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete() |
107 | | - _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath) |
108 | | - } |
109 | | - |
110 | | - err = config.SyncToRemoteServer(cfg, json.NewFilepath) |
111 | | - if err != nil { |
112 | | - api.ErrHandler(c, err) |
113 | | - return |
114 | | - } |
115 | | - |
116 | | - output := nginx.Reload() |
117 | | - if nginx.GetLogLevel(output) >= nginx.Warn { |
118 | | - c.JSON(http.StatusInternalServerError, gin.H{ |
119 | | - "message": output, |
120 | | - }) |
121 | | - return |
122 | | - } |
123 | | - |
124 | | - chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate() |
125 | | - if err != nil { |
126 | | - api.ErrHandler(c, err) |
127 | | - return |
128 | | - } |
129 | | - |
130 | | - if chatgpt.Content == nil { |
131 | | - chatgpt.Content = make([]openai.ChatCompletionMessage, 0) |
132 | | - } |
133 | | - |
134 | | - c.JSON(http.StatusOK, config.Config{ |
135 | | - Name: name, |
136 | | - Content: content, |
137 | | - ChatGPTMessages: chatgpt.Content, |
138 | | - FilePath: json.NewFilepath, |
139 | | - ModifiedAt: time.Now(), |
140 | | - }) |
| 22 | + name := c.Param("name") |
| 23 | + var json struct { |
| 24 | + Name string `json:"name" binding:"required"` |
| 25 | + Filepath string `json:"filepath" binding:"required"` |
| 26 | + NewFilepath string `json:"new_filepath" binding:"required"` |
| 27 | + Content string `json:"content"` |
| 28 | + SyncOverwrite bool `json:"sync_overwrite"` |
| 29 | + SyncNodeIds []int `json:"sync_node_ids"` |
| 30 | + } |
| 31 | + if !api.BindAndValid(c, &json) { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + path := json.Filepath |
| 36 | + if !helper.IsUnderDirectory(path, nginx.GetConfPath()) { |
| 37 | + c.JSON(http.StatusForbidden, gin.H{ |
| 38 | + "message": "filepath is not under the nginx conf path", |
| 39 | + }) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + if !helper.IsUnderDirectory(json.NewFilepath, nginx.GetConfPath()) { |
| 44 | + c.JSON(http.StatusForbidden, gin.H{ |
| 45 | + "message": "new filepath is not under the nginx conf path", |
| 46 | + }) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + if !helper.FileExists(path) { |
| 51 | + c.JSON(http.StatusNotFound, gin.H{ |
| 52 | + "message": "file not found", |
| 53 | + }) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + content := json.Content |
| 58 | + origContent, err := os.ReadFile(path) |
| 59 | + if err != nil { |
| 60 | + api.ErrHandler(c, err) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + if content != "" && content != string(origContent) { |
| 65 | + err = os.WriteFile(path, []byte(content), 0644) |
| 66 | + if err != nil { |
| 67 | + api.ErrHandler(c, err) |
| 68 | + return |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + q := query.Config |
| 73 | + cfg, err := q.Where(q.Filepath.Eq(json.Filepath)).FirstOrCreate() |
| 74 | + if err != nil { |
| 75 | + api.ErrHandler(c, err) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + _, err = q.Where(q.Filepath.Eq(json.Filepath)).Updates(&model.Config{ |
| 80 | + Name: json.Name, |
| 81 | + Filepath: json.NewFilepath, |
| 82 | + SyncNodeIds: json.SyncNodeIds, |
| 83 | + SyncOverwrite: json.SyncOverwrite, |
| 84 | + }) |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + api.ErrHandler(c, err) |
| 88 | + return |
| 89 | + } |
| 90 | + g := query.ChatGPTLog |
| 91 | + // handle rename |
| 92 | + if path != json.NewFilepath { |
| 93 | + if helper.FileExists(json.NewFilepath) { |
| 94 | + c.JSON(http.StatusNotAcceptable, gin.H{ |
| 95 | + "message": "File exists", |
| 96 | + }) |
| 97 | + return |
| 98 | + } |
| 99 | + err := os.Rename(json.Filepath, json.NewFilepath) |
| 100 | + if err != nil { |
| 101 | + api.ErrHandler(c, err) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + // update ChatGPT record |
| 106 | + _, _ = g.Where(g.Name.Eq(json.NewFilepath)).Delete() |
| 107 | + _, _ = g.Where(g.Name.Eq(path)).Update(g.Name, json.NewFilepath) |
| 108 | + } |
| 109 | + |
| 110 | + err = config.SyncToRemoteServer(cfg, json.NewFilepath) |
| 111 | + if err != nil { |
| 112 | + api.ErrHandler(c, err) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + output := nginx.Reload() |
| 117 | + if nginx.GetLogLevel(output) >= nginx.Warn { |
| 118 | + c.JSON(http.StatusInternalServerError, gin.H{ |
| 119 | + "message": output, |
| 120 | + }) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + chatgpt, err := g.Where(g.Name.Eq(json.NewFilepath)).FirstOrCreate() |
| 125 | + if err != nil { |
| 126 | + api.ErrHandler(c, err) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + if chatgpt.Content == nil { |
| 131 | + chatgpt.Content = make([]openai.ChatCompletionMessage, 0) |
| 132 | + } |
| 133 | + |
| 134 | + c.JSON(http.StatusOK, config.Config{ |
| 135 | + Name: name, |
| 136 | + Content: content, |
| 137 | + ChatGPTMessages: chatgpt.Content, |
| 138 | + FilePath: json.NewFilepath, |
| 139 | + ModifiedAt: time.Now(), |
| 140 | + }) |
141 | 141 | } |
0 commit comments