-
Notifications
You must be signed in to change notification settings - Fork 38
/
adminSystemController.go
307 lines (243 loc) · 6.06 KB
/
adminSystemController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* @Description:系统管理
* @Author: gphper
* @Date: 2021-06-01 20:15:04
*/
package setting
import (
"bufio"
"context"
"io/fs"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gphper/ginadmin/configs"
"github.com/gphper/ginadmin/internal/controllers/admin"
"github.com/gphper/ginadmin/pkg/loggers"
"github.com/gphper/ginadmin/pkg/redisx"
"github.com/gphper/ginadmin/pkg/utils/filesystem"
gstrings "github.com/gphper/ginadmin/pkg/utils/strings"
"github.com/gin-gonic/gin"
)
type adminSystemController struct {
admin.BaseController
}
func NewAdminSystemController() adminSystemController {
return adminSystemController{}
}
func (con adminSystemController) Routes(rg *gin.RouterGroup) {
rg.GET("/index", con.index)
rg.GET("/getdir", con.getDir)
rg.GET("/view", con.view)
rg.GET("/index_redis", con.indexRedis)
rg.GET("/getdir_redis", con.getDirRedis)
rg.GET("/view_redis", con.viewRedis)
}
/**
日志目录页面
*/
func (con adminSystemController) index(c *gin.Context) {
var (
path string
err error
log_path string
)
path = gstrings.JoinStr(configs.RootPath, string(filepath.Separator), "logs")
files, err := ioutil.ReadDir(path)
log_path = gstrings.JoinStr(string(filepath.Separator), "logs")
ctx, _ := c.Get("ctx")
if err != nil {
loggers.LogError(ctx.(context.Context), "admin", "读取目录失败", map[string]string{"error": err.Error()})
con.ErrorHtml(c, err)
return
}
con.Html(c, http.StatusOK, "setting/systemlog.html", gin.H{
"log_path": log_path,
"files": files,
"line": string(filepath.Separator),
})
}
/**
获取目录
*/
func (con adminSystemController) getDir(c *gin.Context) {
type FileNode struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
}
var (
path string
err error
fileSlice []FileNode
files []fs.FileInfo
)
fileSlice = make([]FileNode, 0)
path, err = filesystem.FilterPath(configs.RootPath+"logs", c.Query("path"))
if err != nil {
con.Error(c, err.Error())
return
}
files, err = ioutil.ReadDir(path)
if err != nil {
con.Error(c, "获取目录失败")
return
}
for _, v := range files {
var fileType string
if v.IsDir() {
fileType = "dir"
} else {
fileType = "file"
}
fileSlice = append(fileSlice, FileNode{
Name: v.Name(),
Path: gstrings.JoinStr(c.Query("path"), string(filepath.Separator), v.Name()),
Type: fileType,
})
}
c.JSON(http.StatusOK, gin.H{
"data": fileSlice,
})
}
/**
获取日志详情
*/
func (con adminSystemController) view(c *gin.Context) {
var (
err error
startLine int
endLine int
scanner *bufio.Scanner
line int
)
startLine, err = strconv.Atoi(c.DefaultQuery("start_line", "1"))
if err != nil {
con.ErrorHtml(c, err)
return
}
endLine, err = strconv.Atoi(c.DefaultQuery("end_line", "20"))
if err != nil {
con.ErrorHtml(c, err)
return
}
var filecontents []string
filePath, err := filesystem.FilterPath(configs.RootPath+"logs", c.Query("path"))
if err != nil {
con.ErrorHtml(c, err)
return
}
fi, err := os.Open(filePath)
if err != nil {
con.ErrorHtml(c, err)
return
}
defer fi.Close()
scanner = bufio.NewScanner(fi)
for scanner.Scan() {
line++
if line >= startLine && line <= endLine {
// 在要求行数内取得数据
filecontents = append(filecontents, scanner.Text())
} else {
continue
}
}
con.Html(c, http.StatusOK, "setting/systemlog_view.html", gin.H{
"file_path": c.Query("path"),
"filecontents": filecontents,
"start_line": startLine,
"end_line": endLine,
"line": line,
})
}
/**
日志目录页面
*/
func (con adminSystemController) indexRedis(c *gin.Context) {
path := "logs"
dateSlice, err := redisx.GetRedisClient().Keys("logs:*").Result()
ctx, _ := c.Get("ctx")
if err != nil {
loggers.LogError(ctx.(context.Context), "admin", "读取目录失败", map[string]string{"error": err.Error()})
con.ErrorHtml(c, err)
return
}
dates := make(map[string]struct{})
for _, v := range dateSlice {
temp := strings.Split(v, ":")
if _, ok := dates[temp[1]]; !ok {
dates[temp[1]] = struct{}{}
}
}
con.Html(c, http.StatusOK, "setting/systemlog_redis.html", gin.H{
"log_path": path,
"files": dates,
})
}
/**
获取目录
*/
func (con adminSystemController) getDirRedis(c *gin.Context) {
path := c.Query("path")
type FileNode struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
}
pathSlice := strings.Split(path, "_")
pattern := pathSlice[0] + ":*"
dateSlice, err := redisx.GetRedisClient().Keys(pattern).Result()
ctx, _ := c.Get("ctx")
if err != nil {
loggers.LogError(ctx.(context.Context), "admin", "读取目录失败", map[string]string{"error": err.Error()})
con.ErrorHtml(c, err)
return
}
fileSlice := make([]FileNode, 0)
tempMap := make(map[string]struct{})
for _, v := range dateSlice {
temp := strings.Split(v, ":")
index, _ := strconv.Atoi(pathSlice[1])
var fileType string
if index+2 == len(temp) {
fileType = "file"
} else {
fileType = "dir"
}
if _, ok := tempMap[temp[index+1]]; ok {
continue
} else {
tempMap[temp[index+1]] = struct{}{}
}
fileSlice = append(fileSlice, FileNode{
Name: temp[index+1],
Path: pathSlice[0] + ":" + temp[index+1] + "_" + strconv.Itoa(index+1),
Type: fileType,
})
}
c.JSON(http.StatusOK, gin.H{
"data": fileSlice,
})
}
/**
获取日志详情
*/
func (con adminSystemController) viewRedis(c *gin.Context) {
startLine, _ := strconv.Atoi(c.DefaultQuery("start_line", "1"))
endLine, _ := strconv.Atoi(c.DefaultQuery("end_line", "20"))
filePath := c.Query("path")
pathSlice := strings.Split(filePath, "_")
filecontents, _ := redisx.GetRedisClient().LRange(pathSlice[0], int64(startLine-1), int64(endLine-1)).Result()
line, _ := redisx.GetRedisClient().LLen(pathSlice[0]).Result()
con.Html(c, http.StatusOK, "setting/systemlog_viewredis.html", gin.H{
"file_path": filePath,
"filecontents": filecontents,
"start_line": startLine,
"end_line": endLine,
"line": line,
})
}