Blame view
middleware.go
1.22 KB
ad8e9dd2a added middleware ... |
1 2 3 4 5 |
package webutility import ( "net/http" "time" |
1b7dfab73 Payload changed t... |
6 7 |
"git.to-net.rs/marko.tikvic/gologger" |
ad8e9dd2a added middleware ... |
8 |
) |
3fffcb954 removed old http API |
9 |
func SetHeaders(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
10 |
return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
11 12 13 14 |
SetDefaultHeaders(w) if req.Method == http.MethodOptions { return } |
0a60d9a1c switched from han... |
15 16 |
h(w, req) } |
ad8e9dd2a added middleware ... |
17 |
} |
3fffcb954 removed old http API |
18 |
func ParseForm(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
19 |
return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
20 21 22 23 24 |
err := req.ParseForm() if err != nil { BadRequest(w, req, err.Error()) return } |
0a60d9a1c switched from han... |
25 26 |
h(w, req) } |
ad8e9dd2a added middleware ... |
27 |
} |
34436d11e in-out http logs ... |
28 |
var trafficLogger *gologger.Logger |
3fffcb954 removed old http API |
29 |
|
1b7dfab73 Payload changed t... |
30 31 |
func EnableLogging(log string) error { var err error |
34436d11e in-out http logs ... |
32 |
trafficLogger, err = gologger.New(log, gologger.MaxLogSize5MB) |
1b7dfab73 Payload changed t... |
33 34 |
return err } |
3fffcb954 removed old http API |
35 |
func Log(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
36 |
return func(w http.ResponseWriter, req *http.Request) { |
34436d11e in-out http logs ... |
37 38 39 |
in := trafficLogger.RequestLog(req, "") w2 := WrapWithStatusRecorder(w) |
ad8e9dd2a added middleware ... |
40 |
t1 := time.Now() |
34436d11e in-out http logs ... |
41 |
h(w2, req) |
ad8e9dd2a added middleware ... |
42 |
t2 := time.Now() |
34436d11e in-out http logs ... |
43 44 45 |
out := trafficLogger.ResponseLog(w2.Status(), t2.Sub(t1)) trafficLogger.LogHTTPTraffic(in, out) |
0a60d9a1c switched from han... |
46 |
} |
ad8e9dd2a added middleware ... |
47 |
} |
3fffcb954 removed old http API |
48 |
func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
49 |
return func(w http.ResponseWriter, req *http.Request) { |
3fffcb954 removed old http API |
50 |
if _, ok := AuthCheck(req, roles); !ok { |
ad8e9dd2a added middleware ... |
51 52 53 |
Unauthorized(w, req, "") return } |
0a60d9a1c switched from han... |
54 55 |
h(w, req) } |
ad8e9dd2a added middleware ... |
56 |
} |