Blame view

middleware/middleware.go 1.87 KB
b3f1275cd   Marko Tikvić   refactored middle...
1
  package middleware
ad8e9dd2a   Marko Tikvić   added middleware ...
2
3
4
  
  import (
  	"net/http"
954ce8ddd   Marko Tikvić   moved mw logging ...
5
6
7
  	"time"
  
  	"git.to-net.rs/marko.tikvic/gologger"
b3f1275cd   Marko Tikvić   refactored middle...
8
9
  
  	web "git.to-net.rs/marko.tikvic/webutility"
ad8e9dd2a   Marko Tikvić   added middleware ...
10
  )
954ce8ddd   Marko Tikvić   moved mw logging ...
11
  var httpLogger *gologger.Logger
65d214f47   Marko Tikvić   improved middlewa...
12
13
14
15
16
17
18
  func SetAccessControlHeaders(h http.HandlerFunc) http.HandlerFunc {
  	return func(w http.ResponseWriter, req *http.Request) {
  		web.SetAccessControlHeaders(w)
  
  		h(w, req)
  	}
  }
b3f1275cd   Marko Tikvić   refactored middle...
19
20
  // IgnoreOptionsRequests ...
  func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
21
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
22
23
24
  		if req.Method == http.MethodOptions {
  			return
  		}
65d214f47   Marko Tikvić   improved middlewa...
25

0a60d9a1c   Marko Tikvić   switched from han...
26
27
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
28
  }
707782344   Marko Tikvić   lint; vet
29
  // ParseForm ...
3fffcb954   Marko Tikvić   removed old http API
30
  func ParseForm(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
31
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
32
33
  		err := req.ParseForm()
  		if err != nil {
b3f1275cd   Marko Tikvić   refactored middle...
34
  			web.BadRequest(w, req, err.Error())
ad8e9dd2a   Marko Tikvić   added middleware ...
35
36
  			return
  		}
65d214f47   Marko Tikvić   improved middlewa...
37

0a60d9a1c   Marko Tikvić   switched from han...
38
39
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
40
  }
707782344   Marko Tikvić   lint; vet
41
  // ParseMultipartForm ...
3f8e3c437   Marko Tikvić   minor changes
42
43
44
45
  func ParseMultipartForm(h http.HandlerFunc) http.HandlerFunc {
  	return func(w http.ResponseWriter, req *http.Request) {
  		err := req.ParseMultipartForm(32 << 20)
  		if err != nil {
b3f1275cd   Marko Tikvić   refactored middle...
46
  			web.BadRequest(w, req, err.Error())
3f8e3c437   Marko Tikvić   minor changes
47
48
  			return
  		}
65d214f47   Marko Tikvić   improved middlewa...
49

3f8e3c437   Marko Tikvić   minor changes
50
51
52
  		h(w, req)
  	}
  }
b3f1275cd   Marko Tikvić   refactored middle...
53
54
55
  // SetLogger ...
  func SetLogger(logger *gologger.Logger) {
  	httpLogger = logger
954ce8ddd   Marko Tikvić   moved mw logging ...
56
  }
65d214f47   Marko Tikvić   improved middlewa...
57
58
  // LogHTTP ...
  func LogHTTP(h http.HandlerFunc) http.HandlerFunc {
954ce8ddd   Marko Tikvić   moved mw logging ...
59
  	return func(w http.ResponseWriter, req *http.Request) {
65d214f47   Marko Tikvić   improved middlewa...
60
61
62
63
  		if httpLogger == nil {
  			h(w, req)
  			return
  		}
954ce8ddd   Marko Tikvić   moved mw logging ...
64

65d214f47   Marko Tikvić   improved middlewa...
65
  		t1 := time.Now()
954ce8ddd   Marko Tikvić   moved mw logging ...
66

65d214f47   Marko Tikvić   improved middlewa...
67
68
  		claims, _ := web.GetTokenClaims(req)
  		in := httpLogger.LogHTTPRequest(req, claims.Username)
954ce8ddd   Marko Tikvić   moved mw logging ...
69

65d214f47   Marko Tikvić   improved middlewa...
70
  		rec := web.NewStatusRecorder(w)
954ce8ddd   Marko Tikvić   moved mw logging ...
71

65d214f47   Marko Tikvić   improved middlewa...
72
  		h(rec, req)
954ce8ddd   Marko Tikvić   moved mw logging ...
73

65d214f47   Marko Tikvić   improved middlewa...
74
75
76
77
  		t2 := time.Now()
  		out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size())
  
  		httpLogger.CombineHTTPLogs(in, out)
954ce8ddd   Marko Tikvić   moved mw logging ...
78
79
  	}
  }
707782344   Marko Tikvić   lint; vet
80
  // Auth ...
3fffcb954   Marko Tikvić   removed old http API
81
  func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
82
  	return func(w http.ResponseWriter, req *http.Request) {
b3f1275cd   Marko Tikvić   refactored middle...
83
84
  		if _, err := web.AuthCheck(req, roles); err != nil {
  			web.Unauthorized(w, req, err.Error())
ad8e9dd2a   Marko Tikvić   added middleware ...
85
86
  			return
  		}
65d214f47   Marko Tikvić   improved middlewa...
87

0a60d9a1c   Marko Tikvić   switched from han...
88
89
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
90
  }