Commit 932f8146133ef59e7d64632c4c865b84a4cb65a8
1 parent
3fffcb954a
Exists in
master
and in
1 other branch
changes to gologger
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
middleware.go
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 | "git.to-net.rs/marko.tikvic/gologger" | 7 | "git.to-net.rs/marko.tikvic/gologger" |
8 | ) | 8 | ) |
9 | 9 | ||
10 | func SetHeaders(h http.HandlerFunc) http.HandlerFunc { | 10 | func SetHeaders(h http.HandlerFunc) http.HandlerFunc { |
11 | return func(w http.ResponseWriter, req *http.Request) { | 11 | return func(w http.ResponseWriter, req *http.Request) { |
12 | SetDefaultHeaders(w) | 12 | SetDefaultHeaders(w) |
13 | if req.Method == http.MethodOptions { | 13 | if req.Method == http.MethodOptions { |
14 | return | 14 | return |
15 | } | 15 | } |
16 | h(w, req) | 16 | h(w, req) |
17 | } | 17 | } |
18 | } | 18 | } |
19 | 19 | ||
20 | func ParseForm(h http.HandlerFunc) http.HandlerFunc { | 20 | func ParseForm(h http.HandlerFunc) http.HandlerFunc { |
21 | return func(w http.ResponseWriter, req *http.Request) { | 21 | return func(w http.ResponseWriter, req *http.Request) { |
22 | err := req.ParseForm() | 22 | err := req.ParseForm() |
23 | if err != nil { | 23 | if err != nil { |
24 | BadRequest(w, req, err.Error()) | 24 | BadRequest(w, req, err.Error()) |
25 | return | 25 | return |
26 | } | 26 | } |
27 | h(w, req) | 27 | h(w, req) |
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
31 | var reqLogger *gologger.Logger | 31 | var reqLogger *gologger.Logger |
32 | 32 | ||
33 | func EnableLogging(log string) error { | 33 | func EnableLogging(log string) error { |
34 | var err error | 34 | var err error |
35 | reqLogger, err = gologger.New(log, gologger.MaxLogSize5MB) | 35 | reqLogger, err = gologger.New(log, gologger.MaxLogSize5MB) |
36 | return err | 36 | return err |
37 | } | 37 | } |
38 | 38 | ||
39 | func Log(h http.HandlerFunc) http.HandlerFunc { | 39 | func Log(h http.HandlerFunc) http.HandlerFunc { |
40 | return func(w http.ResponseWriter, req *http.Request) { | 40 | return func(w http.ResponseWriter, req *http.Request) { |
41 | reqLogger.LogRequest(req, "") | 41 | reqLogger.LogRequest(req, "") |
42 | t1 := time.Now() | 42 | t1 := time.Now() |
43 | h(w, req) | 43 | h(w, req) |
44 | t2 := time.Now() | 44 | t2 := time.Now() |
45 | reqLogger.LogResponse(w, t2.Sub(t1)) | 45 | reqLogger.LogResponse(w, req, t2.Sub(t1)) |
46 | } | 46 | } |
47 | } | 47 | } |
48 | 48 | ||
49 | func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { | 49 | func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
50 | return func(w http.ResponseWriter, req *http.Request) { | 50 | return func(w http.ResponseWriter, req *http.Request) { |
51 | if _, ok := AuthCheck(req, roles); !ok { | 51 | if _, ok := AuthCheck(req, roles); !ok { |
52 | Unauthorized(w, req, "") | 52 | Unauthorized(w, req, "") |
53 | return | 53 | return |
54 | } | 54 | } |
55 | h(w, req) | 55 | h(w, req) |
56 | } | 56 | } |
57 | } | 57 | } |
58 | 58 |