Blame view
middleware/main.go
3.24 KB
776b4c95b refactored middle... |
1 2 3 |
package middleware import ( |
0dd8dda34 traffic logs handler |
4 5 |
"fmt" "io/ioutil" |
776b4c95b refactored middle... |
6 |
"net/http" |
b80ee4b2b new stuff |
7 8 |
"os" "strings" |
0dd8dda34 traffic logs handler |
9 10 |
web "git.to-net.rs/marko.tikvic/webutility" |
776b4c95b refactored middle... |
11 12 13 |
) func Headers(h http.HandlerFunc) http.HandlerFunc { |
65d214f47 improved middlewa... |
14 |
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(h))) |
776b4c95b refactored middle... |
15 |
} |
65d214f47 improved middlewa... |
16 17 |
func AuthUser(roles string, h http.HandlerFunc) http.HandlerFunc { return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(Auth(roles, h)))) |
776b4c95b refactored middle... |
18 |
} |
69178b824 changed middleware |
19 |
func AuthUserAndLog(roles string, h http.HandlerFunc) http.HandlerFunc { |
65d214f47 improved middlewa... |
20 |
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(Auth(roles, h))))) |
776b4c95b refactored middle... |
21 |
} |
b3f1275cd refactored middle... |
22 |
func LogTraffic(h http.HandlerFunc) http.HandlerFunc { |
65d214f47 improved middlewa... |
23 |
return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(h)))) |
776b4c95b refactored middle... |
24 |
} |
0dd8dda34 traffic logs handler |
25 26 27 28 |
func TrafficLogsHandler(w http.ResponseWriter, req *http.Request) { switch req.Method { case "GET": |
b80ee4b2b new stuff |
29 30 31 32 33 34 35 |
logfile := req.FormValue("logfile") if logfile == "" { files, err := ioutil.ReadDir(httpLogger.GetOutDir() + "/") if err != nil { web.InternalServerError(w, req, err.Error()) return } |
0dd8dda34 traffic logs handler |
36 |
|
b80ee4b2b new stuff |
37 38 |
errorLogs := make([]os.FileInfo, 0) httpLogs := make([]os.FileInfo, 0) |
0dd8dda34 traffic logs handler |
39 |
|
b80ee4b2b new stuff |
40 41 42 43 44 45 46 47 48 49 |
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++ } } |
0dd8dda34 traffic logs handler |
50 |
|
b80ee4b2b new stuff |
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 |
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>")) |
0dd8dda34 traffic logs handler |
126 |
} |
0dd8dda34 traffic logs handler |
127 128 |
} } |