Blame view

middleware/main.go 2.35 KB
776b4c95b   Marko Tikvić   refactored middle...
1
2
3
  package middleware
  
  import (
0dd8dda34   Marko Tikvić   traffic logs handler
4
5
  	"fmt"
  	"io/ioutil"
776b4c95b   Marko Tikvić   refactored middle...
6
  	"net/http"
0dd8dda34   Marko Tikvić   traffic logs handler
7
8
  
  	web "git.to-net.rs/marko.tikvic/webutility"
776b4c95b   Marko Tikvić   refactored middle...
9
10
11
  )
  
  func Headers(h http.HandlerFunc) http.HandlerFunc {
65d214f47   Marko Tikvić   improved middlewa...
12
  	return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(h)))
776b4c95b   Marko Tikvić   refactored middle...
13
  }
65d214f47   Marko Tikvić   improved middlewa...
14
15
  func AuthUser(roles string, h http.HandlerFunc) http.HandlerFunc {
  	return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(Auth(roles, h))))
776b4c95b   Marko Tikvić   refactored middle...
16
  }
69178b824   Marko Tikvić   changed middleware
17
  func AuthUserAndLog(roles string, h http.HandlerFunc) http.HandlerFunc {
65d214f47   Marko Tikvić   improved middlewa...
18
  	return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(Auth(roles, h)))))
776b4c95b   Marko Tikvić   refactored middle...
19
  }
b3f1275cd   Marko Tikvić   refactored middle...
20
  func LogTraffic(h http.HandlerFunc) http.HandlerFunc {
65d214f47   Marko Tikvić   improved middlewa...
21
  	return SetAccessControlHeaders(IgnoreOptionsRequests(ParseForm(LogHTTP(h))))
776b4c95b   Marko Tikvić   refactored middle...
22
  }
0dd8dda34   Marko Tikvić   traffic logs handler
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
  
  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("<body style='background-color: black; color: white'>"))
  		inputForm := `
  		<div>
  	  	  <form action="/api/v1/logs" method="POST" target="_blank">
  		    Username:<br>
  		    <input type="text" name="username"><br>
  		    Password:<br>
  		    <input type="password" name="password"><br>
  		    Log:<br>
  		    <input type="text" name="logfile"><br>
  		    <input type="submit" value="View">
  		  </form>
  		</div>`
  		web.WriteResponse(w, []byte(inputForm))
  
  		web.WriteResponse(w, []byte("<table>"))
  		web.WriteResponse(w, []byte("<tr><th>Name</th><th>Size</th></tr>"))
  		for i := range files {
  			name := files[i].Name()
  			size := files[i].Size()
  			div := fmt.Sprintf(`<tr><td>%s</td><td style="text-align:right">%dB</td></tr>`, name, size)
  			web.WriteResponse(w, []byte(div))
  		}
  		web.WriteResponse(w, []byte("</table></body>"))
  
  	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("<body style='background-color: black; color: white'>"))
  		web.WriteResponse(w, []byte("<pre>"))
  		web.WriteResponse(w, content)
  		web.WriteResponse(w, []byte("</pre></body>"))
  		return
  	}
  }