-
Notifications
You must be signed in to change notification settings - Fork 2
/
middlewares.go
44 lines (38 loc) · 901 Bytes
/
middlewares.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
package jin
import (
"fmt"
"jin/log"
"net/http"
"runtime"
"strings"
)
func trace(message string) string {
var pcs [32]uintptr
n := runtime.Callers(3, pcs[:])
var str strings.Builder
str.WriteString(message + "\nTraceback:")
for _, pc := range pcs[:n] {
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
str.WriteString(fmt.Sprintf("\n\t%s:%d", file, line))
}
return str.String()
}
func Recovery() HandlerFunc {
return func(context *Context) {
defer func() {
if err := recover(); err != nil {
message := fmt.Sprintf("%s", err)
log.Errorf("%s\n\n", trace(message))
context.Fail(http.StatusInternalServerError, "Internal Server Error")
}
}()
context.Next()
}
}
func DefaultLogger() HandlerFunc {
return func(context *Context) {
context.Next()
log.Infof(" %s - [%d] - %s", context.Req.Method, context.StatusCode, context.Req.RequestURI)
}
}