Blame view
middleware.go
972 Bytes
ad8e9dd2a added middleware ... |
1 2 3 4 5 6 |
package webutility import ( "net/http" "time" ) |
0a60d9a1c switched from han... |
7 8 |
func WithSetHeaders(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
9 10 11 12 |
SetDefaultHeaders(w) if req.Method == http.MethodOptions { return } |
0a60d9a1c switched from han... |
13 14 |
h(w, req) } |
ad8e9dd2a added middleware ... |
15 |
} |
0a60d9a1c switched from han... |
16 17 |
func WithParseForm(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
18 19 20 21 22 |
err := req.ParseForm() if err != nil { BadRequest(w, req, err.Error()) return } |
0a60d9a1c switched from han... |
23 24 |
h(w, req) } |
ad8e9dd2a added middleware ... |
25 |
} |
0a60d9a1c switched from han... |
26 27 |
func WithLog(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
28 29 |
reqLogger.LogRequest(req, "") t1 := time.Now() |
0a60d9a1c switched from han... |
30 |
h(w, req) |
ad8e9dd2a added middleware ... |
31 32 |
t2 := time.Now() reqLogger.LogResponse(w, t2.Sub(t1)) |
0a60d9a1c switched from han... |
33 |
} |
ad8e9dd2a added middleware ... |
34 |
} |
0a60d9a1c switched from han... |
35 36 |
func WithAuth(h http.HandlerFunc, authorizedRoles []string) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
37 38 39 40 |
if _, ok := AuthCheck(req, authorizedRoles); !ok { Unauthorized(w, req, "") return } |
0a60d9a1c switched from han... |
41 42 |
h(w, req) } |
ad8e9dd2a added middleware ... |
43 |
} |