Blame view
middleware.go
1.15 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 |
) |
1b7dfab73 Payload changed t... |
9 |
var reqLogger *gologger.Logger |
0a60d9a1c switched from han... |
10 11 |
func WithSetHeaders(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
12 13 14 15 |
SetDefaultHeaders(w) if req.Method == http.MethodOptions { return } |
0a60d9a1c switched from han... |
16 17 |
h(w, req) } |
ad8e9dd2a added middleware ... |
18 |
} |
0a60d9a1c switched from han... |
19 20 |
func WithParseForm(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
21 22 23 24 25 |
err := req.ParseForm() if err != nil { BadRequest(w, req, err.Error()) return } |
0a60d9a1c switched from han... |
26 27 |
h(w, req) } |
ad8e9dd2a added middleware ... |
28 |
} |
1b7dfab73 Payload changed t... |
29 30 31 32 33 |
func EnableLogging(log string) error { var err error reqLogger, err = gologger.New(log, gologger.MaxLogSize5MB) return err } |
0a60d9a1c switched from han... |
34 35 |
func WithLog(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
36 37 |
reqLogger.LogRequest(req, "") t1 := time.Now() |
0a60d9a1c switched from han... |
38 |
h(w, req) |
ad8e9dd2a added middleware ... |
39 40 |
t2 := time.Now() reqLogger.LogResponse(w, t2.Sub(t1)) |
0a60d9a1c switched from han... |
41 |
} |
ad8e9dd2a added middleware ... |
42 |
} |
1b7dfab73 Payload changed t... |
43 |
func WithAuth(authorizedRoles []string, h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
44 |
return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
45 46 47 48 |
if _, ok := AuthCheck(req, authorizedRoles); !ok { Unauthorized(w, req, "") return } |
0a60d9a1c switched from han... |
49 50 |
h(w, req) } |
ad8e9dd2a added middleware ... |
51 |
} |