-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.go
51 lines (41 loc) · 1.12 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"net/http"
"strings"
"github.com/sirupsen/logrus"
)
func (s *server) chooseFormat(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
accept := r.Header.Get("Accept")
for _, af := range s.acceptedFormats {
if strings.Contains(accept, af.accept) {
s.responder = &af
break
}
}
if s.responder == nil {
s.error(w, http.StatusNotAcceptable, "unsupported Accept header provided")
return
}
handler(w, r)
}
}
func (s *server) recordRequest(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
s.logger.WithFields(logrus.Fields{
"accept": r.Header.Get("Accept"),
"auth": r.Header.Get("Authorization") != "",
"path": r.URL.EscapedPath(),
}).Debug("Request made")
handler(w, r)
}
}
func (s *server) requireAuth(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") == "" {
s.error(w, http.StatusUnauthorized, "you need to provide Authorization header")
return
}
handler(w, r)
}
}