Blame view

middleware.go 1.49 KB
ad8e9dd2a   Marko Tikvić   added middleware ...
1
2
3
4
5
  package webutility
  
  import (
  	"net/http"
  	"time"
1b7dfab73   Marko Tikvić   Payload changed t...
6
7
  
  	"git.to-net.rs/marko.tikvic/gologger"
ad8e9dd2a   Marko Tikvić   added middleware ...
8
  )
3fffcb954   Marko Tikvić   removed old http API
9
  func SetHeaders(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
10
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
11
12
13
14
  		SetDefaultHeaders(w)
  		if req.Method == http.MethodOptions {
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
15
16
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
17
  }
3fffcb954   Marko Tikvić   removed old http API
18
  func ParseForm(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
19
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
20
21
22
23
24
  		err := req.ParseForm()
  		if err != nil {
  			BadRequest(w, req, err.Error())
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
25
26
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
27
  }
3f8e3c437   Marko Tikvić   minor changes
28
29
30
31
32
33
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 {
  			BadRequest(w, req, err.Error())
  			return
  		}
  		h(w, req)
  	}
  }
34436d11e   Marko Tikvić   in-out http logs ...
38
  var trafficLogger *gologger.Logger
3fffcb954   Marko Tikvić   removed old http API
39

1b7dfab73   Marko Tikvić   Payload changed t...
40
41
  func EnableLogging(log string) error {
  	var err error
34436d11e   Marko Tikvić   in-out http logs ...
42
  	trafficLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
1b7dfab73   Marko Tikvić   Payload changed t...
43
44
  	return err
  }
3fffcb954   Marko Tikvić   removed old http API
45
  func Log(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
46
  	return func(w http.ResponseWriter, req *http.Request) {
3f8e3c437   Marko Tikvić   minor changes
47
  		t1 := time.Now()
34436d11e   Marko Tikvić   in-out http logs ...
48
  		in := trafficLogger.RequestLog(req, "")
3f8e3c437   Marko Tikvić   minor changes
49
50
  		wRec := WrapWithStatusRecorder(w)
  		h(wRec, req)
ad8e9dd2a   Marko Tikvić   added middleware ...
51
  		t2 := time.Now()
34436d11e   Marko Tikvić   in-out http logs ...
52

3f8e3c437   Marko Tikvić   minor changes
53
  		out := trafficLogger.ResponseLog(wRec.Status(), t2.Sub(t1), 0)
34436d11e   Marko Tikvić   in-out http logs ...
54
  		trafficLogger.LogHTTPTraffic(in, out)
0a60d9a1c   Marko Tikvić   switched from han...
55
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
56
  }
3fffcb954   Marko Tikvić   removed old http API
57
  func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
58
  	return func(w http.ResponseWriter, req *http.Request) {
3f8e3c437   Marko Tikvić   minor changes
59
60
  		if _, err := AuthCheck(req, roles); err != nil {
  			Unauthorized(w, req, err.Error())
ad8e9dd2a   Marko Tikvić   added middleware ...
61
62
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
63
64
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
65
  }