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