-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.go
54 lines (51 loc) · 1.5 KB
/
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
45
46
47
48
49
50
51
52
53
54
package goapi
import (
"fmt"
"net/http"
"time"
)
func notFind() func(ctx *Context) {
return func(ctx *Context) {
http.NotFound(ctx.Writer, ctx.Request)
if lg, ok := ctx.log.(*levelHandleLogger); ok && lg.log == nil {
return
}
status := "404"
statusText := http.StatusText(404)
if isDefaultLogger(ctx.log) {
status = colorError(status)
statusText = colorError(statusText)
}
ctx.Logger().Info("[0.000ms] %v - \"%v %v\" %v %v", ctx.Request.RemoteAddr,
ctx.Request.Method, ctx.Request.URL.Path, status, statusText)
}
}
func setLogger() func(ctx *Context) {
return func(ctx *Context) {
if lg, ok := ctx.log.(*levelHandleLogger); ok && lg.log == nil {
ctx.Next()
return
}
begin := time.Now()
ctx.Next()
elapsed := time.Since(begin)
if resp, ok := ctx.Writer.(*ResponseWriter); ok {
status := fmt.Sprintf("%v", resp.Status())
statusText := http.StatusText(resp.Status())
if isDefaultLogger(ctx.log) && len(status) == 3 {
if status[0] == '1' || status[0] == '2' {
status = colorInfo(status)
statusText = colorInfo(statusText)
} else if status[0] == '4' || status[0] == '5' {
status = colorError(status)
statusText = colorError(statusText)
} else if status[0] == '3' {
status = colorWarning(status)
statusText = colorWarning(statusText)
}
}
ctx.Logger().Info("[%.3fms] %v - \"%v %v\" %v %v", float64(elapsed.Nanoseconds())/1e6, ctx.Request.RemoteAddr,
ctx.Request.Method, ctx.Request.URL.Path, status, statusText)
}
}
}