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