-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
87 lines (81 loc) · 2.81 KB
/
handlers.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
package advhttp
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
// NewPanicRecoveryHandler will wrap a handler in a recover function that will
// catch any panics that occur, and gracefully (actually return a response) handle
// the panic by returning a 500 Internal Server Error response with the panic
// error as the body.
func NewPanicRecoveryHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
var err error
r := recover()
if r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = errors.New("Unknown error")
}
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}()
h.ServeHTTP(w, r)
})
}
// Returns a handler that wraps the given handler with Cross Origin Resource
// Sharing response headers. It uses the default settings which are very
// permissive. The settings can be changed directly on the default cors object,
// or alternatively you can create your own cors object and use `NewCorsHandler()`
func NewDefaultCorsHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ProcessCors(w, r)
h.ServeHTTP(w, r)
})
}
// Returns a handler with a custom cors object and uses that methods `ProcessCors()`
// function before calling the wrapped handler.
func NewCorsHandler(h http.Handler, cors *Cors) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cors.ProcessCors(w, r)
h.ServeHTTP(w, r)
})
}
// Returns a logging handler that wraps the given handler, and logs output to the
// given io.Writer. The logging format is a variation of the `Common Log Format`.
// The Forwarded Variant will utilize the `X-Forwarded-*` headers to log ip, host,
// and proto.
func NewForwardedLoggingHandler(h http.Handler, log io.Writer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Del("X-User-Id")
r.Header.Del("X-Client-Id")
trw := NewResponseWriter(w)
origURI := r.URL.RequestURI()
start := time.Now()
h.ServeHTTP(trw, r)
r.URL, _ = url.Parse(origURI)
fmt.Fprintln(log, trw.LogWithOptions(r, true, time.Now().Sub(start)))
})
}
// Returns a logging handler that wraps the given handler, and logs output to the
// given io.Writer. The logging format is a variation of the `Common Log Format`.
func NewLoggingHandler(h http.Handler, log io.Writer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Del("X-User-Id")
r.Header.Del("X-Client-Id")
trw := NewResponseWriter(w)
origURI := r.URL.RequestURI()
start := time.Now()
h.ServeHTTP(trw, r)
r.URL, _ = url.Parse(origURI)
fmt.Fprintln(log, trw.LogWithOptions(r, true, time.Now().Sub(start)))
})
}