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