-
Notifications
You must be signed in to change notification settings - Fork 6
/
middleware.go
50 lines (43 loc) · 1.5 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
package coquelicot
import (
"log"
"net/http"
)
type Adapter func(http.Handler) http.Handler
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
for k := len(adapters) - 1; k >= 0; k-- {
h = adapters[k](h)
}
return h
}
func CORSMiddleware() Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE")
w.Header().Set("Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Encoding, Content-Range, Content-Disposition, Authorization")
// Since we need to support cross-domain cookies, we must support XHR requests
// with credentials, so the Access-Control-Allow-Credentials header is required
// and Access-Control-Allow-Origin cannot be equal to "*" but reply with the same Origin.
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS.
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
})
}
}
func LogMiddleware(logger *log.Logger) Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
path := r.URL.Path
if len(r.URL.RawQuery) > 0 {
path += "?" + r.URL.RawQuery
}
logger.Printf("%s %s [%s]\n", r.Method, path, r.RemoteAddr)
})
}
}