-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
45 lines (36 loc) · 1019 Bytes
/
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
package main
import (
"fmt"
"net/http"
"go.uber.org/atomic"
)
var requests = atomic.NewInt64(0)
func (app *application) noCache(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if app.config.Cacheheaders == true {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
}
h(w, r)
}
}
func (app *application) noCacheHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if app.config.Cacheheaders == true {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
}
h.ServeHTTP(w, r)
})
}
func (app *application) maxRequests(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if app.config.MaxRequests > 0 {
if i := requests.Inc(); i >= app.config.MaxRequests {
select {
case app.stoppingCh <- fmt.Sprintf("Kubico request limit reached of %d requests", app.config.MaxRequests):
default:
}
}
}
h(w, r)
}
}