forked from mccutchen/go-httpbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
101 lines (88 loc) · 2.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
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
package httpbin
import (
"fmt"
"log"
"net/http"
"time"
)
func cors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
origin = "*"
}
respHeader := w.Header()
respHeader.Set("Access-Control-Allow-Origin", origin)
respHeader.Set("Access-Control-Allow-Credentials", "true")
if r.Method == "OPTIONS" {
respHeader.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS")
respHeader.Set("Access-Control-Max-Age", "3600")
if r.Header.Get("Access-Control-Request-Headers") != "" {
respHeader.Set("Access-Control-Allow-Headers", r.Header.Get("Access-Control-Request-Headers"))
}
}
h.ServeHTTP(w, r)
})
}
func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc {
methodMap := make(map[string]struct{}, len(methods))
for _, m := range methods {
methodMap[m] = struct{}{}
}
return func(w http.ResponseWriter, r *http.Request) {
if _, ok := methodMap[r.Method]; !ok {
http.Error(w, fmt.Sprintf("method %s not allowed", r.Method), http.StatusMethodNotAllowed)
return
}
h.ServeHTTP(w, r)
}
}
func limitRequestSize(maxSize int64, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
r.Body = http.MaxBytesReader(w, r.Body, maxSize)
}
h.ServeHTTP(w, r)
})
}
// metaResponseWriter implements is an http.ResponseWriter and http.Flusher
// that records its status code and body size for logging purposes.
type metaResponseWriter struct {
w http.ResponseWriter
status int
size int
}
func (mw *metaResponseWriter) Write(b []byte) (int, error) {
size, err := mw.w.Write(b)
mw.size += size
return size, err
}
func (mw *metaResponseWriter) WriteHeader(s int) {
mw.w.WriteHeader(s)
mw.status = s
}
func (mw *metaResponseWriter) Flush() {
f := mw.w.(http.Flusher)
f.Flush()
}
func (mw *metaResponseWriter) Header() http.Header {
return mw.w.Header()
}
func (mw *metaResponseWriter) Status() int {
if mw.status == 0 {
return http.StatusOK
}
return mw.status
}
func (mw *metaResponseWriter) Size() int {
return mw.size
}
func logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mw := &metaResponseWriter{w: w}
t := time.Now()
h.ServeHTTP(mw, r)
duration := time.Now().Sub(t)
log.Printf("status=%d method=%s uri=%q size=%d duration=%s", mw.Status(), r.Method, r.URL.RequestURI(), mw.Size(), duration)
})
}