Blame view

middleware/middleware.go 1.76 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
b3f1275cd   Marko Tikvić   refactored middle...
12
13
  // IgnoreOptionsRequests ...
  func IgnoreOptionsRequests(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
14
  	return func(w http.ResponseWriter, req *http.Request) {
b3f1275cd   Marko Tikvić   refactored middle...
15
  		web.SetDefaultHeaders(w)
ad8e9dd2a   Marko Tikvić   added middleware ...
16
17
18
  		if req.Method == http.MethodOptions {
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
19
20
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
21
  }
707782344   Marko Tikvić   lint; vet
22
  // ParseForm ...
3fffcb954   Marko Tikvić   removed old http API
23
  func ParseForm(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
24
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
25
26
  		err := req.ParseForm()
  		if err != nil {
b3f1275cd   Marko Tikvić   refactored middle...
27
  			web.BadRequest(w, req, err.Error())
ad8e9dd2a   Marko Tikvić   added middleware ...
28
29
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
30
31
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
32
  }
707782344   Marko Tikvić   lint; vet
33
  // ParseMultipartForm ...
3f8e3c437   Marko Tikvić   minor changes
34
35
36
37
  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...
38
  			web.BadRequest(w, req, err.Error())
3f8e3c437   Marko Tikvić   minor changes
39
40
41
42
43
  			return
  		}
  		h(w, req)
  	}
  }
b3f1275cd   Marko Tikvić   refactored middle...
44
45
46
  // SetLogger ...
  func SetLogger(logger *gologger.Logger) {
  	httpLogger = logger
954ce8ddd   Marko Tikvić   moved mw logging ...
47
  }
b3f1275cd   Marko Tikvić   refactored middle...
48
49
  // LogRequestAndResponse ...
  func LogRequestAndResponse(h http.HandlerFunc) http.HandlerFunc {
954ce8ddd   Marko Tikvić   moved mw logging ...
50
  	return func(w http.ResponseWriter, req *http.Request) {
b3f1275cd   Marko Tikvić   refactored middle...
51
52
  		if httpLogger != nil {
  			t1 := time.Now()
954ce8ddd   Marko Tikvić   moved mw logging ...
53

b3f1275cd   Marko Tikvić   refactored middle...
54
55
  			claims, _ := web.GetTokenClaims(req)
  			in := httpLogger.LogHTTPRequest(req, claims.Username)
954ce8ddd   Marko Tikvić   moved mw logging ...
56

b3f1275cd   Marko Tikvić   refactored middle...
57
  			rec := web.NewStatusRecorder(w)
954ce8ddd   Marko Tikvić   moved mw logging ...
58

b3f1275cd   Marko Tikvić   refactored middle...
59
  			h(rec, req)
954ce8ddd   Marko Tikvić   moved mw logging ...
60

b3f1275cd   Marko Tikvić   refactored middle...
61
62
  			t2 := time.Now()
  			out := httpLogger.LogHTTPResponse(rec.Status(), t2.Sub(t1), rec.Size())
954ce8ddd   Marko Tikvić   moved mw logging ...
63

b3f1275cd   Marko Tikvić   refactored middle...
64
65
66
67
  			httpLogger.CombineHTTPLogs(in, out)
  		} else {
  			h(w, req)
  		}
954ce8ddd   Marko Tikvić   moved mw logging ...
68
69
  	}
  }
707782344   Marko Tikvić   lint; vet
70
  // Auth ...
3fffcb954   Marko Tikvić   removed old http API
71
  func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
72
  	return func(w http.ResponseWriter, req *http.Request) {
b3f1275cd   Marko Tikvić   refactored middle...
73
74
  		if _, err := web.AuthCheck(req, roles); err != nil {
  			web.Unauthorized(w, req, err.Error())
ad8e9dd2a   Marko Tikvić   added middleware ...
75
76
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
77
78
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
79
  }