-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.go
55 lines (49 loc) · 1.77 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
package main
import (
"math/rand"
"net/http"
)
// Middleware wraps an http.Handler with additional functionality
type Middleware func(http.Handler) http.Handler
// handleWithMiddleware returns a request handler wrapped with the
// provided middleware layers
func handleWithMiddleware(h http.Handler, adapters ...Middleware) http.Handler {
// To make the middleware run in the order in which they are specified,
// we reverse through them in the Middleware function, rather than just
// ranging over them
for i := len(adapters) - 1; i >= 0; i-- {
h = adapters[i](h)
}
return h
}
func makeRequestID() string {
rid := make([]rune, 16)
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
for i := range rid {
rid[i] = letters[rand.Intn(len(letters))]
}
return string(rid)
}
// setRequestID is a middleware the generates a random ID for each request processed
// by the HTTP server. The request ID is added to the request context and used to
// track various information and correlate logs.
func setRequestID() Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, addToContext(r, contextKeyRequestID, makeRequestID()))
})
}
}
// setResponseHeaders adds web security headers suitable for API
// responses to an http.Handler
func setResponseHeaders() Middleware {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Security-Policy", "default-src 'none'; object-src 'none';")
w.Header().Add("X-Frame-Options", "DENY")
w.Header().Add("X-Content-Type-Options", "nosniff")
w.Header().Add("Strict-Transport-Security", "max-age=31536000;")
h.ServeHTTP(w, r)
})
}
}