Commit 1469f25bfc7cccaef5cecb97d323de874cc5df44
1 parent
2726b56833
Exists in
master
added utility for easier logging
Showing
1 changed file
with
11 additions
and
0 deletions
Show diff stats
middleware/middleware.go
1 | package middleware | 1 | package middleware |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "net/http" | 4 | "net/http" |
5 | "time" | 5 | "time" |
6 | 6 | ||
7 | "git.to-net.rs/marko.tikvic/gologger" | 7 | "git.to-net.rs/marko.tikvic/gologger" |
8 | 8 | ||
9 | web "git.to-net.rs/marko.tikvic/webutility" | 9 | web "git.to-net.rs/marko.tikvic/webutility" |
10 | ) | 10 | ) |
11 | 11 | ||
12 | var httpLogger *gologger.Logger | 12 | var httpLogger *gologger.Logger |
13 | 13 | ||
14 | func SetAccessControlHeaders(h http.HandlerFunc) http.HandlerFunc { | 14 | func SetAccessControlHeaders(h http.HandlerFunc) http.HandlerFunc { |
15 | return func(w http.ResponseWriter, req *http.Request) { | 15 | return func(w http.ResponseWriter, req *http.Request) { |
16 | web.SetAccessControlHeaders(w) | 16 | web.SetAccessControlHeaders(w) |
17 | 17 | ||
18 | h(w, req) | 18 | h(w, req) |
19 | } | 19 | } |
20 | } | 20 | } |
21 | 21 | ||
22 | // IgnoreOptionsRequests ... | 22 | // IgnoreOptionsRequests ... |
23 | func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc { | 23 | func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc { |
24 | return func(w http.ResponseWriter, req *http.Request) { | 24 | return func(w http.ResponseWriter, req *http.Request) { |
25 | if req.Method == http.MethodOptions { | 25 | if req.Method == http.MethodOptions { |
26 | return | 26 | return |
27 | } | 27 | } |
28 | 28 | ||
29 | h(w, req) | 29 | h(w, req) |
30 | } | 30 | } |
31 | } | 31 | } |
32 | 32 | ||
33 | // ParseForm ... | 33 | // ParseForm ... |
34 | func ParseForm(h http.HandlerFunc) http.HandlerFunc { | 34 | func ParseForm(h http.HandlerFunc) http.HandlerFunc { |
35 | return func(w http.ResponseWriter, req *http.Request) { | 35 | return func(w http.ResponseWriter, req *http.Request) { |
36 | err := req.ParseForm() | 36 | err := req.ParseForm() |
37 | if err != nil { | 37 | if err != nil { |
38 | web.BadRequest(w, req, err.Error()) | 38 | web.BadRequest(w, req, err.Error()) |
39 | return | 39 | return |
40 | } | 40 | } |
41 | 41 | ||
42 | h(w, req) | 42 | h(w, req) |
43 | } | 43 | } |
44 | } | 44 | } |
45 | 45 | ||
46 | // ParseMultipartForm ... | 46 | // ParseMultipartForm ... |
47 | func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { | 47 | func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { |
48 | return func(w http.ResponseWriter, req *http.Request) { | 48 | return func(w http.ResponseWriter, req *http.Request) { |
49 | err := req.ParseMultipartForm(32 << 20) | 49 | err := req.ParseMultipartForm(32 << 20) |
50 | if err != nil { | 50 | if err != nil { |
51 | web.BadRequest(w, req, err.Error()) | 51 | web.BadRequest(w, req, err.Error()) |
52 | return | 52 | return |
53 | } | 53 | } |
54 | 54 | ||
55 | h(w, req) | 55 | h(w, req) |
56 | } | 56 | } |
57 | } | 57 | } |
58 | 58 | ||
59 | // SetLogger ... | 59 | // SetLogger ... |
60 | func SetLogger(logger *gologger.Logger) { | 60 | func SetLogger(logger *gologger.Logger) { |
61 | httpLogger = logger | 61 | httpLogger = logger |
62 | } | 62 | } |
63 | 63 | ||
64 | func StartLogging(filename, dir string) (err error) { | ||
65 | if httpLogger, err = gologger.New(filename, dir, gologger.MaxLogSize1MB); err != nil { | ||
66 | return err | ||
67 | } | ||
68 | return nil | ||
69 | } | ||
70 | |||
71 | func CloseLogger() { | ||
72 | httpLogger.Close() | ||
73 | } | ||
74 | |||
64 | // LogHTTP ... | 75 | // LogHTTP ... |
65 | func LogHTTP(h http.HandlerFunc) http.HandlerFunc { | 76 | func LogHTTP(h http.HandlerFunc) http.HandlerFunc { |
66 | return func(w http.ResponseWriter, req *http.Request) { | 77 | return func(w http.ResponseWriter, req *http.Request) { |
67 | if httpLogger == nil { | 78 | if httpLogger == nil { |
68 | h(w, req) | 79 | h(w, req) |
69 | return | 80 | return |
70 | } | 81 | } |
71 | 82 | ||
72 | t1 := time.Now() | 83 | t1 := time.Now() |
73 | 84 | ||
74 | claims, _ := web.GetTokenClaims(req) | 85 | claims, _ := web.GetTokenClaims(req) |
75 | in := httpLogger.LogHTTPRequest(req, claims.Username) | 86 | in := httpLogger.LogHTTPRequest(req, claims.Username) |
76 | 87 | ||
77 | rec := web.NewStatusRecorder(w) | 88 | rec := web.NewStatusRecorder(w) |
78 | 89 | ||
79 | h(rec, req) | 90 | h(rec, req) |
80 | 91 | ||
81 | t2 := time.Now() | 92 | t2 := time.Now() |
82 | out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) | 93 | out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) |
83 | 94 | ||
84 | httpLogger.CombineHTTPLogs(in, out) | 95 | httpLogger.CombineHTTPLogs(in, out) |
85 | } | 96 | } |
86 | } | 97 | } |
87 | 98 | ||
88 | // Auth ... | 99 | // Auth ... |
89 | func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { | 100 | func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
90 | return func(w http.ResponseWriter, req *http.Request) { | 101 | return func(w http.ResponseWriter, req *http.Request) { |
91 | if _, err := web.AuthCheck(req, roles); err != nil { | 102 | if _, err := web.AuthCheck(req, roles); err != nil { |
92 | web.Unauthorized(w, req, err.Error()) | 103 | web.Unauthorized(w, req, err.Error()) |
93 | return | 104 | return |
94 | } | 105 | } |
95 | 106 | ||
96 | h(w, req) | 107 | h(w, req) |
97 | } | 108 | } |
98 | } | 109 | } |
99 | 110 |