Commit 0a60d9a1c07d143118e3d494090bfbbed1342b48

Authored by Marko Tikvić
1 parent ad8e9dd2af
Exists in master and in 1 other branch v2

switched from handler to handlerfuncs

Showing 1 changed file with 16 additions and 16 deletions   Show diff stats
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 7
8 func WithSetHeaders(h http.Handler) http.Handler { 8 func WithSetHeaders(h http.HandlerFunc) http.HandlerFunc {
9 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 9 return func(w http.ResponseWriter, req *http.Request) {
10 SetDefaultHeaders(w) 10 SetDefaultHeaders(w)
11 if req.Method == http.MethodOptions { 11 if req.Method == http.MethodOptions {
12 return 12 return
13 } 13 }
14 h.ServeHTTP(w, req) 14 h(w, req)
15 }) 15 }
16 } 16 }
17 17
18 func WithParseForm(h http.Handler) http.Handler { 18 func WithParseForm(h http.HandlerFunc) http.HandlerFunc {
19 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 19 return func(w http.ResponseWriter, req *http.Request) {
20 err := req.ParseForm() 20 err := req.ParseForm()
21 if err != nil { 21 if err != nil {
22 BadRequest(w, req, err.Error()) 22 BadRequest(w, req, err.Error())
23 return 23 return
24 } 24 }
25 h.ServeHTTP(w, req) 25 h(w, req)
26 }) 26 }
27 } 27 }
28 28
29 func WithLog(h http.Handler) http.Handler { 29 func WithLog(h http.HandlerFunc) http.HandlerFunc {
30 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 30 return func(w http.ResponseWriter, req *http.Request) {
31 reqLogger.LogRequest(req, "") 31 reqLogger.LogRequest(req, "")
32 t1 := time.Now() 32 t1 := time.Now()
33 h.ServeHTTP(w, req) 33 h(w, req)
34 t2 := time.Now() 34 t2 := time.Now()
35 reqLogger.LogResponse(w, t2.Sub(t1)) 35 reqLogger.LogResponse(w, t2.Sub(t1))
36 }) 36 }
37 } 37 }
38 38
39 func WithAuth(h http.Handler, authorizedRoles []string) http.Handler { 39 func WithAuth(h http.HandlerFunc, authorizedRoles []string) http.HandlerFunc {
40 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 40 return func(w http.ResponseWriter, req *http.Request) {
41 if _, ok := AuthCheck(req, authorizedRoles); !ok { 41 if _, ok := AuthCheck(req, authorizedRoles); !ok {
42 Unauthorized(w, req, "") 42 Unauthorized(w, req, "")
43 return 43 return
44 } 44 }
45 h.ServeHTTP(w, req) 45 h(w, req)
46 }) 46 }
47 } 47 }
48 48