-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
121 lines (97 loc) · 2.98 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/h2non/bimg"
"github.com/throttled/throttled"
"github.com/throttled/throttled/store/memstore"
)
func Middleware(fn func(http.ResponseWriter, *http.Request), o ServerOptions) http.Handler {
next := http.Handler(http.HandlerFunc(fn))
if o.Concurrency > 0 {
next = throttle(next, o)
}
if o.HTTPCacheTTL >= 0 {
next = setCacheHeaders(next, o.HTTPCacheTTL)
}
return validate(defaultHeaders(next), o)
}
func ImageMiddleware(o ServerOptions) func(Operation) http.Handler {
return func(fn Operation) http.Handler {
handler := validateImage(Middleware(imageController(o, Operation(fn)), o), o)
return handler
}
}
func throttleError(err error) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "throttle error: "+err.Error(), http.StatusInternalServerError)
})
}
func throttle(next http.Handler, o ServerOptions) http.Handler {
store, err := memstore.New(65536)
if err != nil {
return throttleError(err)
}
quota := throttled.RateQuota{throttled.PerSec(o.Concurrency), o.Burst}
rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
if err != nil {
return throttleError(err)
}
httpRateLimiter := throttled.HTTPRateLimiter{
RateLimiter: rateLimiter,
VaryBy: &throttled.VaryBy{Method: true},
}
return httpRateLimiter.RateLimit(next)
}
func validate(next http.Handler, o ServerOptions) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
ErrorReply(r, w, ErrMethodNotAllowed, o)
return
}
next.ServeHTTP(w, r)
})
}
func validateImage(next http.Handler, o ServerOptions) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if r.Method == "GET" && isPublicPath(path) {
next.ServeHTTP(w, r)
return
}
if r.Method == "GET" && o.Origin == "" && o.Cache == "" {
ErrorReply(r, w, ErrMethodNotAllowed, o)
return
}
next.ServeHTTP(w, r)
})
}
func defaultHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", fmt.Sprintf("tto-resize %s (bimg %s)", Version, bimg.Version))
next.ServeHTTP(w, r)
})
}
func setCacheHeaders(next http.Handler, ttl int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer next.ServeHTTP(w, r)
if r.Method != "GET" || isPublicPath(r.URL.Path) {
return
}
ttlDiff := time.Duration(ttl) * time.Second
expires := time.Now().Add(ttlDiff)
w.Header().Add("Expires", strings.Replace(expires.Format(time.RFC1123), "UTC", "GMT", -1))
w.Header().Add("Cache-Control", getCacheControl(ttl))
})
}
func getCacheControl(ttl int) string {
if ttl == 0 {
return "private, no-cache, no-store, must-revalidate"
}
return fmt.Sprintf("public, s-maxage=%d, max-age=%d, no-transform", ttl, ttl)
}
func isPublicPath(path string) bool {
return path == "/health" || path == "/"
}