Blame view
middleware/middleware.go
1.76 KB
b3f1275cd refactored middle... |
1 |
package middleware |
ad8e9dd2a added middleware ... |
2 3 4 |
import ( "net/http" |
954ce8ddd moved mw logging ... |
5 6 7 |
"time" "git.to-net.rs/marko.tikvic/gologger" |
b3f1275cd refactored middle... |
8 9 |
web "git.to-net.rs/marko.tikvic/webutility" |
ad8e9dd2a added middleware ... |
10 |
) |
954ce8ddd moved mw logging ... |
11 |
var httpLogger *gologger.Logger |
b3f1275cd refactored middle... |
12 13 |
// IgnoreOptionsRequests ... func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
14 |
return func(w http.ResponseWriter, req *http.Request) { |
b3f1275cd refactored middle... |
15 |
web.SetDefaultHeaders(w) |
ad8e9dd2a added middleware ... |
16 17 18 |
if req.Method == http.MethodOptions { return } |
0a60d9a1c switched from han... |
19 20 |
h(w, req) } |
ad8e9dd2a added middleware ... |
21 |
} |
707782344 lint; vet |
22 |
// ParseForm ... |
3fffcb954 removed old http API |
23 |
func ParseForm(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
24 |
return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
25 26 |
err := req.ParseForm() if err != nil { |
b3f1275cd refactored middle... |
27 |
web.BadRequest(w, req, err.Error()) |
ad8e9dd2a added middleware ... |
28 29 |
return } |
0a60d9a1c switched from han... |
30 31 |
h(w, req) } |
ad8e9dd2a added middleware ... |
32 |
} |
707782344 lint; vet |
33 |
// ParseMultipartForm ... |
3f8e3c437 minor changes |
34 35 36 37 |
func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { err := req.ParseMultipartForm(32 << 20) if err != nil { |
b3f1275cd refactored middle... |
38 |
web.BadRequest(w, req, err.Error()) |
3f8e3c437 minor changes |
39 40 41 42 43 |
return } h(w, req) } } |
b3f1275cd refactored middle... |
44 45 46 |
// SetLogger ... func SetLogger(logger *gologger.Logger) { httpLogger = logger |
954ce8ddd moved mw logging ... |
47 |
} |
b3f1275cd refactored middle... |
48 49 |
// LogRequestAndResponse ... func LogRequestAndResponse(h http.HandlerFunc) http.HandlerFunc { |
954ce8ddd moved mw logging ... |
50 |
return func(w http.ResponseWriter, req *http.Request) { |
b3f1275cd refactored middle... |
51 52 |
if httpLogger != nil { t1 := time.Now() |
954ce8ddd moved mw logging ... |
53 |
|
b3f1275cd refactored middle... |
54 55 |
claims, _ := web.GetTokenClaims(req) in := httpLogger.LogHTTPRequest(req, claims.Username) |
954ce8ddd moved mw logging ... |
56 |
|
b3f1275cd refactored middle... |
57 |
rec := web.NewStatusRecorder(w) |
954ce8ddd moved mw logging ... |
58 |
|
b3f1275cd refactored middle... |
59 |
h(rec, req) |
954ce8ddd moved mw logging ... |
60 |
|
b3f1275cd refactored middle... |
61 62 |
t2 := time.Now() out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size()) |
954ce8ddd moved mw logging ... |
63 |
|
b3f1275cd refactored middle... |
64 65 66 67 |
httpLogger.CombineHTTPLogs(in, out) } else { h(w, req) } |
954ce8ddd moved mw logging ... |
68 69 |
} } |
707782344 lint; vet |
70 |
// Auth ... |
3fffcb954 removed old http API |
71 |
func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
72 |
return func(w http.ResponseWriter, req *http.Request) { |
b3f1275cd refactored middle... |
73 74 |
if _, err := web.AuthCheck(req, roles); err != nil { web.Unauthorized(w, req, err.Error()) |
ad8e9dd2a added middleware ... |
75 76 |
return } |
0a60d9a1c switched from han... |
77 78 |
h(w, req) } |
ad8e9dd2a added middleware ... |
79 |
} |