Commit 954ce8ddd99bf7e610f11e47db677d363fc13860
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
middleware.go
... | ... | @@ -2,8 +2,13 @@ package webutility |
2 | 2 | |
3 | 3 | import ( |
4 | 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 | 12 | func SetHeaders(h http.HandlerFunc) http.HandlerFunc { |
8 | 13 | return func(w http.ResponseWriter, req *http.Request) { |
9 | 14 | SetDefaultHeaders(w) |
... | ... | @@ -36,6 +41,29 @@ func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { |
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 | 67 | func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
40 | 68 | return func(w http.ResponseWriter, req *http.Request) { |
41 | 69 | if _, err := AuthCheck(req, roles); err != nil { | ... | ... |
middleware_log.go
... | ... | @@ -1,33 +0,0 @@ |
1 | -package webutility | |
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 | -} |