Blame view
middleware/middleware.go
2.07 KB
b3f1275cd refactored middle... |
1 |
package middleware |
ad8e9dd2a added middleware ... |
2 3 4 |
import ( "net/http" |
954ce8ddd moved mw logging ... |
5 |
"time" |
b3f1275cd refactored middle... |
6 |
web "git.to-net.rs/marko.tikvic/webutility" |
ed367ddd1 moved gologger |
7 |
"git.to-net.rs/marko.tikvic/webutility/logger" |
ad8e9dd2a added middleware ... |
8 |
) |
ed367ddd1 moved gologger |
9 |
var httpLogger *logger.Logger |
954ce8ddd moved mw logging ... |
10 |
|
65d214f47 improved middlewa... |
11 12 13 14 15 16 17 |
func SetAccessControlHeaders(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { web.SetAccessControlHeaders(w) h(w, req) } } |
b3f1275cd refactored middle... |
18 19 |
// IgnoreOptionsRequests ... func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
20 |
return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
21 22 23 |
if req.Method == http.MethodOptions { return } |
65d214f47 improved middlewa... |
24 |
|
0a60d9a1c switched from han... |
25 26 |
h(w, req) } |
ad8e9dd2a added middleware ... |
27 |
} |
707782344 lint; vet |
28 |
// ParseForm ... |
3fffcb954 removed old http API |
29 |
func ParseForm(h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
30 |
return func(w http.ResponseWriter, req *http.Request) { |
ad8e9dd2a added middleware ... |
31 32 |
err := req.ParseForm() if err != nil { |
b3f1275cd refactored middle... |
33 |
web.BadRequest(w, req, err.Error()) |
ad8e9dd2a added middleware ... |
34 35 |
return } |
65d214f47 improved middlewa... |
36 |
|
0a60d9a1c switched from han... |
37 38 |
h(w, req) } |
ad8e9dd2a added middleware ... |
39 |
} |
707782344 lint; vet |
40 |
// ParseMultipartForm ... |
3f8e3c437 minor changes |
41 42 43 44 |
func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { err := req.ParseMultipartForm(32 << 20) if err != nil { |
b3f1275cd refactored middle... |
45 |
web.BadRequest(w, req, err.Error()) |
3f8e3c437 minor changes |
46 47 |
return } |
65d214f47 improved middlewa... |
48 |
|
3f8e3c437 minor changes |
49 50 51 |
h(w, req) } } |
b3f1275cd refactored middle... |
52 |
// SetLogger ... |
ed367ddd1 moved gologger |
53 |
func SetLogger(logger *logger.Logger) { |
b3f1275cd refactored middle... |
54 |
httpLogger = logger |
954ce8ddd moved mw logging ... |
55 |
} |
1469f25bf added utility for... |
56 |
func StartLogging(filename, dir string) (err error) { |
ed367ddd1 moved gologger |
57 |
if httpLogger, err = logger.New(filename, dir, logger.MaxLogSize1MB); err != nil { |
1469f25bf added utility for... |
58 59 60 61 62 63 64 65 |
return err } return nil } func CloseLogger() { httpLogger.Close() } |
65d214f47 improved middlewa... |
66 |
// LogHTTP ... |
e2880d3fb integraded golloger |
67 |
func LogHTTP(hfunc http.HandlerFunc) http.HandlerFunc { |
954ce8ddd moved mw logging ... |
68 |
return func(w http.ResponseWriter, req *http.Request) { |
65d214f47 improved middlewa... |
69 |
if httpLogger == nil { |
e2880d3fb integraded golloger |
70 |
hfunc(w, req) |
65d214f47 improved middlewa... |
71 72 |
return } |
954ce8ddd moved mw logging ... |
73 |
|
65d214f47 improved middlewa... |
74 |
t1 := time.Now() |
954ce8ddd moved mw logging ... |
75 |
|
65d214f47 improved middlewa... |
76 77 |
claims, _ := web.GetTokenClaims(req) in := httpLogger.LogHTTPRequest(req, claims.Username) |
954ce8ddd moved mw logging ... |
78 |
|
65d214f47 improved middlewa... |
79 |
rec := web.NewStatusRecorder(w) |
954ce8ddd moved mw logging ... |
80 |
|
e2880d3fb integraded golloger |
81 |
hfunc(rec, req) |
954ce8ddd moved mw logging ... |
82 |
|
e2880d3fb integraded golloger |
83 |
out := httpLogger.LogHTTPResponse(rec.Data(), rec.Status(), t1) |
65d214f47 improved middlewa... |
84 85 |
httpLogger.CombineHTTPLogs(in, out) |
954ce8ddd moved mw logging ... |
86 87 |
} } |
707782344 lint; vet |
88 |
// Auth ... |
3fffcb954 removed old http API |
89 |
func Auth(roles string, h http.HandlerFunc) http.HandlerFunc { |
0a60d9a1c switched from han... |
90 |
return func(w http.ResponseWriter, req *http.Request) { |
b3f1275cd refactored middle... |
91 92 |
if _, err := web.AuthCheck(req, roles); err != nil { web.Unauthorized(w, req, err.Error()) |
ad8e9dd2a added middleware ... |
93 94 |
return } |
65d214f47 improved middlewa... |
95 |
|
0a60d9a1c switched from han... |
96 97 |
h(w, req) } |
ad8e9dd2a added middleware ... |
98 |
} |