-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmiddleware.go
More file actions
69 lines (60 loc) · 1.93 KB
/
middleware.go
File metadata and controls
69 lines (60 loc) · 1.93 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
package main
import (
"fmt"
"net/http"
"runtime/debug"
"time"
)
type Middleware func(http.Handler) http.Handler
type ResponseWriterWrapper struct {
http.ResponseWriter
StatusCode int
BytesSent int
}
func (rw *ResponseWriterWrapper) WriteHeader(code int) {
rw.StatusCode = code
rw.ResponseWriter.WriteHeader(code)
}
func (rw *ResponseWriterWrapper) Write(data []byte) (int, error) {
bytesWritten, err := rw.ResponseWriter.Write(data)
rw.BytesSent += bytesWritten
return bytesWritten, err
}
func logRequestMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wrapper := &ResponseWriterWrapper{ResponseWriter: w, StatusCode: http.StatusOK}
timer := time.Now()
next.ServeHTTP(wrapper, r)
fmt.Printf("%d - %5dµs %21s %4s %42s %6d %11s - %s - %s\n",
wrapper.StatusCode, time.Since(timer).Microseconds(), r.RemoteAddr,
r.Method, r.URL.Path, wrapper.BytesSent,
wrapper.Header().Get("X-PrivTracker"), r.Referer(), r.UserAgent())
})
}
func headersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.TLS != nil {
w.Header().Set("Strict-Transport-Security", "max-age=31536000") // hsts
}
w.Header().Set("Server", "PrivTracker")
next.ServeHTTP(w, r)
})
}
func recoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Recovered from panic: %v\n%s\n", err, debug.Stack())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}
func chainMiddleware(handler http.Handler, middlewares ...Middleware) http.Handler {
// Apply middlewares in reverse order (outermost to innermost)
for i := len(middlewares) - 1; i >= 0; i-- {
handler = middlewares[i](handler)
}
return handler
}