Blame view

middleware.go 958 Bytes
ad8e9dd2a   Marko Tikvić   added middleware ...
1
2
3
4
  package webutility
  
  import (
  	"net/http"
ad8e9dd2a   Marko Tikvić   added middleware ...
5
  )
3fffcb954   Marko Tikvić   removed old http API
6
  func SetHeaders(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
7
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
8
9
10
11
  		SetDefaultHeaders(w)
  		if req.Method == http.MethodOptions {
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
12
13
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
14
  }
3fffcb954   Marko Tikvić   removed old http API
15
  func ParseForm(h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
16
  	return func(w http.ResponseWriter, req *http.Request) {
ad8e9dd2a   Marko Tikvić   added middleware ...
17
18
19
20
21
  		err := req.ParseForm()
  		if err != nil {
  			BadRequest(w, req, err.Error())
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
22
23
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
24
  }
3f8e3c437   Marko Tikvić   minor changes
25
26
27
28
29
30
31
32
33
34
  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)
  	}
  }
3fffcb954   Marko Tikvić   removed old http API
35
  func Auth(roles string, h http.HandlerFunc) http.HandlerFunc {
0a60d9a1c   Marko Tikvić   switched from han...
36
  	return func(w http.ResponseWriter, req *http.Request) {
3f8e3c437   Marko Tikvić   minor changes
37
38
  		if _, err := AuthCheck(req, roles); err != nil {
  			Unauthorized(w, req, err.Error())
ad8e9dd2a   Marko Tikvić   added middleware ...
39
40
  			return
  		}
0a60d9a1c   Marko Tikvić   switched from han...
41
42
  		h(w, req)
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
43
  }