/
middleware.go
56 lines (46 loc) · 1.45 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
52
53
54
55
56
package middleware
import (
"errors"
"expvar"
"net/http"
"strconv"
"github.com/felixge/httpsnoop"
"github.com/seanflannery10/ossa/auth"
"github.com/seanflannery10/ossa/httperrors"
)
func RequireAuthenticatedUser(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authenticatedUser := auth.GetUser(r)
if authenticatedUser == "" {
httperrors.AuthenticationRequired(w, r)
return
}
next.ServeHTTP(w, r)
})
}
func Metrics(next http.Handler) http.Handler {
totalRequestsReceived := expvar.NewInt("total_requests_received")
totalResponsesSent := expvar.NewInt("total_responses_sent")
totalProcessingTimeMicroseconds := expvar.NewInt("total_processing_time_μs")
totalResponsesSentByStatus := expvar.NewMap("total_responses_sent_by_status")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
metrics := httpsnoop.CaptureMetrics(next, w, r)
totalRequestsReceived.Add(1)
totalResponsesSent.Add(1)
totalProcessingTimeMicroseconds.Add(metrics.Duration.Microseconds())
totalResponsesSentByStatus.Add(strconv.Itoa(metrics.Code), 1)
})
}
func RecoverPanic(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
err, _ := rvr.(error)
if !errors.Is(err, http.ErrAbortHandler) {
httperrors.ServerError(w, r, err)
}
}
}()
next.ServeHTTP(w, r)
})
}