forked from QLeelulu/goku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devhelper.go
79 lines (69 loc) · 2 KB
/
devhelper.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
package goku
import (
//"fmt"
"html/template"
"net/http"
"os"
"path"
"runtime"
)
type devErrorContext struct {
ShowDetail bool
Request *http.Request
Err string
StatusCode int
Stack string
OsEnviron []string
GoRoot string
GoNumGoroutine int
GoVersion string
GokuVersion string
}
type devErrorHanller struct {
view string
TemplateEnginer TemplateEnginer
}
func (eh *devErrorHanller) showErrorInfo(ctx *HttpContext, err string, statusCode int, showDetail bool, stack string) {
ec := &devErrorContext{
ShowDetail: showDetail,
Request: ctx.Request,
Err: err,
StatusCode: statusCode,
GoVersion: runtime.Version(),
GokuVersion: GetVersion(),
}
if showDetail {
ec.Stack = stack
ec.OsEnviron = os.Environ()
ec.GoRoot = runtime.GOROOT()
ec.GoNumGoroutine = runtime.NumGoroutine()
}
vd := &ViewData{Model: ec}
ctx.SetHeader("Content-Type", "text/html")
eh.TemplateEnginer.Render(eh.view, "", vd, ctx.responseContentCache)
}
func createDevErrorHandler() *devErrorHanller {
//pwd, _ := os.Getwd()
_, filename, _, _ := runtime.Caller(1)
pwd := path.Dir(filename)
eh := &devErrorHanller{
view: path.Join(pwd, "views/error.html"),
TemplateEnginer: &DefaultTemplateEngine{
UseCache: false, // true
TemplateCache: make(map[string]*template.Template),
},
}
return eh
}
var devErrorHanlle *devErrorHanller = createDevErrorHandler()
type devErrorResult struct {
StatusCode int
Err string
ShowDetail bool
Stack string
}
func (er *devErrorResult) ExecuteResult(ctx *HttpContext) {
ctx.responseContentCache.Reset()
ctx.Status(er.StatusCode)
devErrorHanlle.showErrorInfo(ctx, er.Err, er.StatusCode, er.ShowDetail, er.Stack)
}