Commit 0dd8dda34000b6b83b236a244b5cc968ea4dd0f6
1 parent
1469f25bfc
Exists in
master
traffic logs handler
Showing
1 changed file
with
59 additions
and
0 deletions
Show diff stats
middleware/main.go
1 | 1 | package middleware |
2 | 2 | |
3 | 3 | import ( |
4 | + "fmt" | |
5 | + "io/ioutil" | |
4 | 6 | "net/http" |
7 | + | |
8 | + web "git.to-net.rs/marko.tikvic/webutility" | |
5 | 9 | ) |
6 | 10 | |
7 | 11 | func Headers(h http.HandlerFunc) http.HandlerFunc { |
... | ... | @@ -19,3 +23,58 @@ func AuthUserAndLog(roles string, h http.HandlerFunc) http.HandlerFunc { |
19 | 23 | func LogTraffic(h http.HandlerFunc) http.HandlerFunc { |
20 | 24 | return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(h)))) |
21 | 25 | } |
26 | + | |
27 | +func TrafficLogsHandler(w http.ResponseWriter, req *http.Request) { | |
28 | + switch req.Method { | |
29 | + case "GET": | |
30 | + files, err := ioutil.ReadDir(httpLogger.GetOutDir() + "/") | |
31 | + if err != nil { | |
32 | + web.InternalServerError(w, req, err.Error()) | |
33 | + return | |
34 | + } | |
35 | + | |
36 | + web.SetContentType(w, "text/html; charset=utf-8") | |
37 | + web.SetResponseStatus(w, http.StatusOK) | |
38 | + | |
39 | + web.WriteResponse(w, []byte("<body style='background-color: black; color: white'>")) | |
40 | + inputForm := ` | |
41 | + <div> | |
42 | + <form action="/api/v1/logs" method="POST" target="_blank"> | |
43 | + Username:<br> | |
44 | + <input type="text" name="username"><br> | |
45 | + Password:<br> | |
46 | + <input type="password" name="password"><br> | |
47 | + Log:<br> | |
48 | + <input type="text" name="logfile"><br> | |
49 | + <input type="submit" value="View"> | |
50 | + </form> | |
51 | + </div>` | |
52 | + web.WriteResponse(w, []byte(inputForm)) | |
53 | + | |
54 | + web.WriteResponse(w, []byte("<table>")) | |
55 | + web.WriteResponse(w, []byte("<tr><th>Name</th><th>Size</th></tr>")) | |
56 | + for i := range files { | |
57 | + name := files[i].Name() | |
58 | + size := files[i].Size() | |
59 | + div := fmt.Sprintf(`<tr><td>%s</td><td style="text-align:right">%dB</td></tr>`, name, size) | |
60 | + web.WriteResponse(w, []byte(div)) | |
61 | + } | |
62 | + web.WriteResponse(w, []byte("</table></body>")) | |
63 | + | |
64 | + case "POST": | |
65 | + web.SetContentType(w, "text/html; charset=utf-8") | |
66 | + | |
67 | + logfile := req.FormValue("logfile") | |
68 | + content, err := web.ReadFileContent(httpLogger.GetOutDir() + "/" + logfile) | |
69 | + if err != nil { | |
70 | + web.InternalServerError(w, req, err.Error()) | |
71 | + return | |
72 | + } | |
73 | + web.SetResponseStatus(w, http.StatusOK) | |
74 | + web.WriteResponse(w, []byte("<body style='background-color: black; color: white'>")) | |
75 | + web.WriteResponse(w, []byte("<pre>")) | |
76 | + web.WriteResponse(w, content) | |
77 | + web.WriteResponse(w, []byte("</pre></body>")) | |
78 | + return | |
79 | + } | |
80 | +} | ... | ... |