-
-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathwebMiddleware.go
More file actions
244 lines (205 loc) · 6.31 KB
/
Copy pathwebMiddleware.go
File metadata and controls
244 lines (205 loc) · 6.31 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"bytes"
"compress/gzip"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"
"github.com/ulule/limiter"
"github.com/ulule/limiter/drivers/store/memory"
)
func main() {
// Define route handlers
homeHandler := http.HandlerFunc(home)
profileHandler := http.HandlerFunc(profile)
// Apply the combined middleware to your route handlers
http.Handle("/", applyMiddleware(homeHandler))
http.Handle("/profile", applyMiddleware(profileHandler))
http.ListenAndServe(":8080", nil)
}
// home and profile are our route handlers
func home(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the home page!")
}
func profile(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the profile page!")
}
// Combine multiple middlewares into a single applyMiddleware function
func applyMiddleware(handler http.Handler) http.Handler {
return timingMiddleware(
loggingMiddleware(
corsMiddleware(
gzipMiddleware(handler),
),
),
)
}
// various middleware implementations:
// timingMiddleware logs the time taken to process a request
func timingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
elapsed := time.Since(start)
log.Printf("Elapsed time: %v\n", elapsed)
})
}
// loggingMiddleware logs the request method and URL
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Request received: %s %s\n", r.Method, r.URL)
next.ServeHTTP(w, r)
})
}
// gzipMiddleware compresses the response body
func gzipMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", "gzip")
gw := gzip.NewWriter(w)
defer gw.Close()
grw := gzipResponseWriter{ResponseWriter: w, Writer: gw}
next.ServeHTTP(grw, r)
})
}
type gzipResponseWriter struct {
http.ResponseWriter
io.Writer
}
func (grw gzipResponseWriter) Write(data []byte) (int, error) {
return grw.Writer.Write(data)
}
// corsMiddleware adds the Access-Control-Allow-Origin header
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
next.ServeHTTP(w, r)
})
}
// rateLimitMiddleware limits the number of requests per minute
func rateLimitMiddleware(next http.Handler) http.Handler {
rate := limiter.Rate{
Period: 1 * time.Minute,
Limit: 10,
}
store := memory.NewStore()
instance := limiter.New(store, rate)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
limiterCtx, err := instance.Get(r.Context(), r.RemoteAddr)
if err != nil {
http.Error(w, "Too many requests", http.StatusTooManyRequests)
return
}
w.Header().Set("X-RateLimit-Limit", fmt.Sprintf("%d", limiterCtx.Limit))
w.Header().Set("X-RateLimit-Remaining", fmt.Sprintf("%d", limiterCtx.Remaining))
w.Header().Set("X-RateLimit-Reset", fmt.Sprintf("%d", limiterCtx.Reset))
if limiterCtx.Reached {
http.Error(w, "Too many requests", http.StatusTooManyRequests)
return
}
next.ServeHTTP(w, r)
})
}
// authenticationMiddleware checks for a valid Authorization header
func authenticationMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token != "Bearer my-secret-token" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
// contentTypeJsonMiddleware sets the Content-Type header to application/json
func contentTypeJsonMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
next.ServeHTTP(w, r)
})
}
// panicRecoveryMiddleware recovers from panics and returns a 500 error
func panicRecoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered from panic: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}
// cacheItem stores a response and its timestamp
type cacheItem struct {
response []byte
timestamp time.Time
}
// cacheStorage is a simple in-memory cache
type cacheStorage struct {
data map[string]*cacheItem
mutex sync.RWMutex
}
// newCacheStorage creates a new cacheStorage
func newCacheStorage() *cacheStorage {
return &cacheStorage{data: make(map[string]*cacheItem)}
}
func (cs *cacheStorage) get(key string) (value []byte, found bool) {
cs.mutex.RLock()
defer cs.mutex.RUnlock()
item, exists := cs.data[key]
if exists && !isExpired(item.timestamp) {
return item.response, true
}
return nil, false
}
func (cs *cacheStorage) set(key string, value []byte) {
cs.mutex.Lock()
defer cs.mutex.Unlock()
cs.data[key] = &cacheItem{response: value, timestamp: time.Now()}
}
func isExpired(timestamp time.Time) bool {
return time.Since(timestamp) > 10*time.Minute
}
var cache = newCacheStorage()
// cacheMiddleware is a middleware that caches responses
func cacheMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cacheKey := generateCacheKey(r)
if response, found := cache.get(cacheKey); found {
w.Write(response)
return
}
recorder := newResponseRecorder()
next.ServeHTTP(recorder, r)
response := recorder.Bytes()
cache.set(cacheKey, response)
w.Write(response)
}
}
// generateCacheKey generates a cache key from a request
func generateCacheKey(r *http.Request) string {
hasher := sha1.New()
io.WriteString(hasher, r.URL.String())
io.WriteString(hasher, r.Method)
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
type responseRecorder struct {
http.ResponseWriter
buffer bytes.Buffer
}
// newResponseRecorder creates a new responseRecorder
func newResponseRecorder() *responseRecorder {
return &responseRecorder{}
}
func (rr *responseRecorder) Write(data []byte) (int, error) {
rr.buffer.Write(data)
return rr.ResponseWriter.Write(data)
}
func (rr *responseRecorder) Bytes() []byte {
return rr.buffer.Bytes()
}