/
middleware.go
58 lines (53 loc) · 1.43 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
package server
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/urfave/negroni"
)
func (rtr *Router) catchall(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
w2 := negroni.NewResponseWriter(w)
// defer is first in last out, this will run if in case any
// uncaught panic happens within the api logic, except if
// it happens within another go routine created within
defer func() {
rc := recover()
diff := float64(time.Since(t1).Microseconds()) / 1000
diffStr := fmt.Sprintf("%f", diff)
if rc != nil {
rtr.log.Error("HTTPS_MW", errors.New("Uncaught Exception: "+rc.(error).Error()))
// build generic 500 error
jsonBody, _ := json.Marshal(map[string]string{
"error": "There was an internal server error",
})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonBody)
} else if rtr.logRequest {
if !contains(rtr.logSkipPath, r.URL.Path) {
msg, _ := json.Marshal(map[string]string{
"method": r.Method,
"status": strconv.Itoa(w2.Status()),
"src": r.RemoteAddr,
"ms": diffStr,
})
rtr.log.Out(r.URL.Path, string(msg))
}
}
}()
next.ServeHTTP(w2, r)
})
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}