-
Notifications
You must be signed in to change notification settings - Fork 18
/
filters.go
117 lines (111 loc) · 2.78 KB
/
filters.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package W
import (
"fmt"
"runtime"
"time"
"github.com/fatih/color"
"github.com/kokizzu/gotro/I"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/S"
)
func PanicFilter(ctx *Context) {
defer func() {
err := recover()
defer func() {
if err2 := recover(); err2 != nil {
L.Print(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`)
L.Print(`Nested Error on PanicFilter (should not be happening)`)
L.Describe(err)
L.Describe(err2)
start := 0
for {
pc, file, line, ok := runtime.Caller(start)
if !ok {
break
}
name := runtime.FuncForPC(pc).Name()
start += 1
L.Print("\t" + file + `:` + I.ToStr(line) + ` ` + name)
}
}
}()
if err != nil {
//L.Panic(errors.New(`Internal Server Error`), ``, err)
err2, ok := err.(error)
if !ok {
err2 = fmt.Errorf("%# v", err)
}
err_str := err2.Error()
L.LOG.Errorf(err_str)
str := L.StackTrace(2)
L.LOG.Criticalf("StackTrace: %s", str)
ctx.Title += ` (error)`
var detail string
if ctx.Engine.DebugMode {
detail = err_str + string(L.StackTrace(3))
} else {
ref_code := `EREF:` + S.RandomCB63(4)
L.LOG.Notice(ref_code) // no need to print stack trace, should be handled by PanicFilter
detail = `Detailed error message not available on production mode, error reference code for webmaster: ` + ref_code
ctx.Engine.SendDebugMail(ctx.RequestDebugStr() + S.WebBR + S.WebBR + err_str + S.WebBR + detail)
}
ctx.Error(500, detail)
}
}()
ctx.Next()(ctx)
}
func LogFilter(ctx *Context) {
start := time.Now()
url := string(ctx.RequestCtx.RequestURI())
method := string(ctx.RequestCtx.Method())
if ctx.Engine.DebugMode {
L.LOG.Notice(method, url, ctx.Posts().String())
}
ctx.Next()(ctx)
L.Trace()
var codeStr string
code := ctx.Response.StatusCode()
if code < 400 {
codeStr = L.BgGreen(`%s`, color.BlueString(`%3d`, code))
} else {
codeStr = L.BgRed(`%3d`, code)
}
ones := `nano`
elapsed := float64(time.Since(start).Nanoseconds())
if elapsed > 1000000000.0 {
elapsed /= 1000000000.0
ones = `sec`
} else if elapsed > 1000000.0 {
elapsed /= 1000000.0
ones = `mili`
} else if elapsed > 1000.0 {
elapsed /= 1000.0
ones = `micro`
}
referrer := ctx.RequestCtx.Referer()
msg := fmt.Sprintf(`[%s] %7d %7.2f %5s | %4s %-40s | %-40s | %15s | %s | %s | %s`,
codeStr,
ctx.Buffer.Len(),
elapsed,
ones,
method,
url,
referrer,
ctx.Session.IpAddr,
ctx.Session.UserAgent,
ctx.Session.String(),
ctx.Posts().String(),
)
msg = S.Replace(msg, `%`, `%%`)
L.LOG.Notice(msg)
}
func SessionFilter(ctx *Context) {
ctx.Session = &Session{}
ctx.Session.Load(ctx)
prev := ctx.Session.HeaderString()
ctx.Next()(ctx)
if ctx.Session.Changed {
ctx.Log("session changed, previously: " + prev)
}
ctx.Session.Save(ctx)
}