Commit 954ce8ddd99bf7e610f11e47db677d363fc13860

Authored by Marko Tikvić
1 parent 1257b237ad
Exists in master

moved mw logging to middleware.go

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