diff --git a/middleware/main.go b/middleware/main.go index 720e570..be51d77 100644 --- a/middleware/main.go +++ b/middleware/main.go @@ -1,7 +1,11 @@ package middleware import ( + "fmt" + "io/ioutil" "net/http" + + web "git.to-net.rs/marko.tikvic/webutility" ) func Headers(h http.HandlerFunc) http.HandlerFunc { @@ -19,3 +23,58 @@ func AuthUserAndLog(roles string, h http.HandlerFunc) http.HandlerFunc { 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": + files, err := ioutil.ReadDir(httpLogger.GetOutDir() + "/") + if err != nil { + web.InternalServerError(w, req, err.Error()) + return + } + + web.SetContentType(w, "text/html; charset=utf-8") + web.SetResponseStatus(w, http.StatusOK) + + web.WriteResponse(w, []byte("")) + inputForm := ` +
+
+ Username:
+
+ Password:
+
+ Log:
+
+ +
+
` + web.WriteResponse(w, []byte(inputForm)) + + web.WriteResponse(w, []byte("")) + web.WriteResponse(w, []byte("")) + for i := range files { + name := files[i].Name() + size := files[i].Size() + div := fmt.Sprintf(``, name, size) + web.WriteResponse(w, []byte(div)) + } + web.WriteResponse(w, []byte("
NameSize
%s%dB
")) + + case "POST": + web.SetContentType(w, "text/html; charset=utf-8") + + logfile := req.FormValue("logfile") + 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("")) + web.WriteResponse(w, []byte("
"))
+		web.WriteResponse(w, content)
+		web.WriteResponse(w, []byte("
")) + return + } +}