forked from sourcegraph/appdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
37 lines (29 loc) · 751 Bytes
/
handler.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
package traceapp
import (
"fmt"
"log"
"net/http"
"runtime/debug"
)
type handlerFunc func(http.ResponseWriter, *http.Request) error
// ServeHTTP implements http.Handler.
func (h handlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var rb responseBuffer
handleError := func(err error) {
log.Printf("%s %s: HTTP %d: %s", r.Method, r.URL.RequestURI(), rb.Status, err.Error())
// Never cache error responses.
w.Header().Set("cache-control", "no-cache, max-age=0")
http.Error(w, err.Error(), rb.Status)
}
defer func() {
if rv := recover(); rv != nil {
handleError(fmt.Errorf("handler panic\n\n%s\n\n%s", rv, debug.Stack()))
}
}()
err := h(&rb, r)
if err != nil {
handleError(err)
return
}
rb.WriteTo(w)
}