-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
62 lines (56 loc) · 1.87 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
57
58
59
60
61
62
package main
import (
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
func secureHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("X-Frame-Options", "deny")
next.ServeHTTP(w, r)
})
}
func (app *application) logRequest(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI() != "/metrics" {
app.infoLog.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL.RequestURI())
}
next.ServeHTTP(w, r)
})
}
func (app *application) recoverPanic(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.Header().Set("Connection", "close")
app.serverError(w, fmt.Errorf("%s", err))
}
}()
next.ServeHTTP(w, r)
})
}
func (app *application) metrics(next http.Handler) http.Handler {
totalRequestsReceived := promauto.NewCounter(prometheus.CounterOpts{
Name: "app_requests_received_total",
Help: "The total number of processed requests",
})
totalResponsesSent := promauto.NewCounter(prometheus.CounterOpts{
Name: "app_responses_sent_total",
Help: "The total number of processed responses",
})
totalProcessingTimeMicroseconds := promauto.NewCounter(prometheus.CounterOpts{
Name: "app_processing_time_microseconds_total",
Help: "The total amount of processing time in microseconds",
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
totalRequestsReceived.Inc()
next.ServeHTTP(w, r)
totalResponsesSent.Inc()
duration := time.Since(start).Microseconds()
totalProcessingTimeMicroseconds.Add(float64(duration))
})
}