Commit feb3471e7afc30f68bf49129d71ee190581f223b

Authored by Marko Tikvić
1 parent 368c7f87bf
Exists in master

refactored logging middleware

Showing 2 changed files with 34 additions and 28 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "net/http" 4 "net/http"
5 "time"
6
7 "git.to-net.rs/marko.tikvic/gologger"
8 ) 5 )
9 6
10 func SetHeaders(h http.HandlerFunc) http.HandlerFunc { 7 func SetHeaders(h http.HandlerFunc) http.HandlerFunc {
11 return func(w http.ResponseWriter, req *http.Request) { 8 return func(w http.ResponseWriter, req *http.Request) {
12 SetDefaultHeaders(w) 9 SetDefaultHeaders(w)
13 if req.Method == http.MethodOptions { 10 if req.Method == http.MethodOptions {
14 return 11 return
15 } 12 }
16 h(w, req) 13 h(w, req)
17 } 14 }
18 } 15 }
19 16
20 func ParseForm(h http.HandlerFunc) http.HandlerFunc { 17 func ParseForm(h http.HandlerFunc) http.HandlerFunc {
21 return func(w http.ResponseWriter, req *http.Request) { 18 return func(w http.ResponseWriter, req *http.Request) {
22 err := req.ParseForm() 19 err := req.ParseForm()
23 if err != nil { 20 if err != nil {
24 BadRequest(w, req, err.Error()) 21 BadRequest(w, req, err.Error())
25 return 22 return
26 } 23 }
27 h(w, req) 24 h(w, req)
28 } 25 }
29 } 26 }
30 27
31 func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { 28 func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc {
32 return func(w http.ResponseWriter, req *http.Request) { 29 return func(w http.ResponseWriter, req *http.Request) {
33 err := req.ParseMultipartForm(32 << 20) 30 err := req.ParseMultipartForm(32 << 20)
34 if err != nil { 31 if err != nil {
35 BadRequest(w, req, err.Error()) 32 BadRequest(w, req, err.Error())
36 return 33 return
37 } 34 }
38 h(w, req) 35 h(w, req)
39 } 36 }
40 } 37 }
41 38
42 var trafficLogger *gologger.Logger
43
44 func EnableLogging(log string) error {
45 var err error
46 trafficLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
47 return err
48 }
49
50 func Log(h http.HandlerFunc) http.HandlerFunc {
51 return func(w http.ResponseWriter, req *http.Request) {
52 t1 := time.Now()
53
54 in := trafficLogger.RequestLog(req, "")
55
56 wRec := WrapWithStatusRecorder(w)
57 h(wRec, req)
58
59 t2 := time.Now()
60
61 out := trafficLogger.ResponseLog(wRec.Status(), t2.Sub(t1), 0)
62
63 trafficLogger.LogHTTPTraffic(in, out)
64 }
65 }
66
67 func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { 39 func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
68 return func(w http.ResponseWriter, req *http.Request) { 40 return func(w http.ResponseWriter, req *http.Request) {
69 if _, err := AuthCheck(req, roles); err != nil { 41 if _, err := AuthCheck(req, roles); err != nil {
70 Unauthorized(w, req, err.Error()) 42 Unauthorized(w, req, err.Error())
71 return 43 return
72 } 44 }
73 h(w, req) 45 h(w, req)
74 } 46 }
75 } 47 }
76 48
middleware_log.go
File was created 1 package webutility
2
3 import (
4 "net/http"
5 "time"
6
7 "git.to-net.rs/marko.tikvic/gologger"
8 )
9
10 var trafficLogger *gologger.Logger
11
12 func EnableLogging(log string) (err error) {
13 trafficLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
14 return err
15 }
16
17 func Log(h http.HandlerFunc) http.HandlerFunc {
18 return func(w http.ResponseWriter, req *http.Request) {
19 t1 := time.Now()
20
21 claims, _ := GetTokenClaims(req)
22
23 in := trafficLogger.RequestLog(req, claims.Username)
24
25 wRec := WrapWithStatusRecorder(w)
26 h(wRec, req)
27
28 t2 := time.Now()
29
30 out := trafficLogger.ResponseLog(wRec.Status(), t2.Sub(t1), 0)
31
32 trafficLogger.LogHTTPTraffic(in, out)
33 }
34 }
35