Blame view

middleware/middleware.go 2.08 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
  }
1469f25bf   Marko Tikvić   added utility for...
57
58
59
60
61
62
63
64
65
66
  func StartLogging(filename, dir string) (err error) {
  	if httpLogger, err = gologger.New(filename, dir, gologger.MaxLogSize1MB); err != nil {
  		return err
  	}
  	return nil
  }
  
  func CloseLogger() {
  	httpLogger.Close()
  }
65d214f47   Marko Tikvić   improved middlewa...
67
68
  // LogHTTP ...
  func LogHTTP(h http.HandlerFunc) http.HandlerFunc {
954ce8ddd   Marko Tikvić   moved mw logging ...
69
  	return func(w http.ResponseWriter, req *http.Request) {
65d214f47   Marko Tikvić   improved middlewa...
70
71
72
73
  		if httpLogger == nil {
  			h(w, req)
  			return
  		}
954ce8ddd   Marko Tikvić   moved mw logging ...
74

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

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

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

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

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

0a60d9a1c   Marko Tikvić   switched from han...
98
99
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
100
  }