-
Notifications
You must be signed in to change notification settings - Fork 53
/
handler.go
57 lines (53 loc) · 1.31 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package znet
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/sohaha/zlsgo/zlog"
"github.com/sohaha/zlsgo/zutil"
)
// Recovery Recovery
func Recovery(r *Engine, handler PanicFunc) HandlerFunc {
r.router.panic = handler
return func(c *Context) {
defer func() {
if err := recover(); err != nil {
errMsg, ok := err.(error)
if !ok {
errMsg = errors.New(fmt.Sprint(err))
}
handler(c, errMsg)
}
}()
c.Next()
}
}
func requestLog(c *Context) {
if c.Engine.IsDebug() {
var status string
end := time.Now()
c.l.RLock()
statusCode := zutil.GetBuff()
defer func() {
c.l.RUnlock()
zutil.PutBuff(statusCode)
}()
latency := end.Sub(c.startTime)
code := c.prevData.Code
statusCode.WriteString(" ")
statusCode.WriteString(strconv.Itoa(code))
statusCode.WriteString(" ")
s := statusCode.String()
switch {
case code >= 200 && code <= 299:
status = c.Log.ColorBackgroundWrap(zlog.ColorBlack, zlog.ColorGreen, s)
case code >= 300 && code <= 399:
status = c.Log.ColorBackgroundWrap(zlog.ColorBlack, zlog.ColorYellow, s)
default:
status = c.Log.ColorBackgroundWrap(zlog.ColorBlack, zlog.ColorRed, s)
}
ft := fmt.Sprintf("%s %15s %15v %%s %%s", status, c.GetClientIP(), latency)
c.Log.Success(showRouteDebug(c.Log, ft, c.Request.Method, c.Request.RequestURI))
}
}