main.go
3.24 KB
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
package middleware
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
web "git.to-net.rs/marko.tikvic/webutility"
)
func Headers(h http.HandlerFunc) http.HandlerFunc {
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(h)))
}
func AuthUser(roles string, h http.HandlerFunc) http.HandlerFunc {
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(Auth(roles, h))))
}
func AuthUserAndLog(roles string, h http.HandlerFunc) http.HandlerFunc {
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(Auth(roles, h)))))
}
func LogTraffic(h http.HandlerFunc) http.HandlerFunc {
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(h))))
}
func TrafficLogsHandler(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
logfile := req.FormValue("logfile")
if logfile == "" {
files, err := ioutil.ReadDir(httpLogger.GetOutDir() + "/")
if err != nil {
web.InternalServerError(w, req, err.Error())
return
}
errorLogs := make([]os.FileInfo, 0)
httpLogs := make([]os.FileInfo, 0)
var errorLogsCount, httpLogsCount int
for _, f := range files {
if strings.HasPrefix(f.Name(), "err") {
errorLogs = append(errorLogs, f)
errorLogsCount++
} else if strings.HasPrefix(f.Name(), "http") {
httpLogs = append(httpLogs, f)
httpLogsCount++
}
}
web.SetContentType(w, "text/html; charset=utf-8")
web.SetResponseStatus(w, http.StatusOK)
web.WriteResponse(w, []byte(`
<body style='background-color: black; color: white'>
<table>
<tr>
<th>Error logs</th><th></th> <th style="width: 25px"></th>
<th>Traffic logs</th><th></th>
</tr>
`))
var (
div, name string
size int64
)
max := errorLogsCount
if httpLogsCount > errorLogsCount {
max = httpLogsCount
}
for i := 0; i < max; i++ {
div = "<tr>"
if i < errorLogsCount {
name = errorLogs[i].Name()
size = errorLogs[i].Size()
div += fmt.Sprintf(`
<td>
<a style="color: white"
href="/api/v1/logs?logfile=%s"
target="_blank">%s
</a>
</td>
<td style="color: white; text-align:right">%dB</td>`,
name, name, size,
)
} else {
div += fmt.Sprintf(`<td></td><td></td>`)
}
div += "<td></td>"
if i < httpLogsCount {
name := httpLogs[i].Name()
size := httpLogs[i].Size()
div += fmt.Sprintf(`
<td>
<a style="color: white"
href="/api/v1/logs?logfile=%s"
target="_blank">%s
</a></td>
<td style="color: white; text-align:right">%dB</td>`,
name, name, size,
)
} else {
div += fmt.Sprintf(`<td></td><td></td>`)
}
div += "</tr>"
web.WriteResponse(w, []byte(div))
}
web.WriteResponse(w, []byte("</table></body>"))
} else {
content, err := web.ReadFileContent(httpLogger.GetOutDir() + "/" + logfile)
if err != nil {
web.InternalServerError(w, req, err.Error())
return
}
web.SetResponseStatus(w, http.StatusOK)
web.WriteResponse(w, []byte("<body style='background-color: black; color: white'>"))
web.WriteResponse(w, []byte("<pre>"))
web.WriteResponse(w, content)
web.WriteResponse(w, []byte("</pre></body>"))
}
}
}