-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathactionresult.go
More file actions
96 lines (85 loc) · 2.39 KB
/
actionresult.go
File metadata and controls
96 lines (85 loc) · 2.39 KB
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
package goku
import (
"bytes"
"io"
"net/http"
//"fmt"
)
type ActionResulter interface {
ExecuteResult(ctx *HttpContext)
}
type ActionResult struct {
StatusCode int
Headers map[string]string
Body *bytes.Buffer
notShowDevError bool // just for devhelper
}
func (ar *ActionResult) ExecuteResult(ctx *HttpContext) {
if ar.Headers != nil {
for k, v := range ar.Headers {
ctx.SetHeader(k, v)
}
}
// if ar.StatusCode == 0 {
// ar.StatusCode = 200
// }
if !ar.notShowDevError && ar.StatusCode >= 400 && ctx.requestHandler.ServerConfig.Debug {
der := &devErrorResult{
StatusCode: ar.StatusCode,
Err: ar.Body.String(),
ShowDetail: true,
}
der.ExecuteResult(ctx)
} else {
ctx.Status(ar.StatusCode)
if ar.Body != nil && ar.Body.Len() > 0 {
// TODO: which way is the fastest ?
//ctx.Write(ar.Body.Bytes())
//ar.Body.WriteTo(ctx.responseWriter)
ctx.WriteBuffer(ar.Body)
}
}
}
type ViewResult struct {
ActionResult
ViewEngine ViewEnginer
TemplateEngine TemplateEnginer
ViewData map[string]interface{}
ViewModel interface{}
ViewName string
Layout string
IsPartial bool // if is Partial, not use layout
}
func (vr *ViewResult) Render(ctx *HttpContext, wr io.Writer) {
if vr.ViewEngine == nil {
vr.ViewEngine = ctx.requestHandler.ViewEnginer
}
if vr.TemplateEngine == nil {
vr.TemplateEngine = ctx.requestHandler.TemplateEnginer
}
vi := &ViewInfo{
Controller: ctx.RouteData.Controller,
Action: ctx.RouteData.Action,
View: vr.ViewName,
Layout: vr.Layout,
IsPartial: vr.IsPartial,
}
viewData := &ViewData{
Data: vr.ViewData,
Model: vr.ViewModel,
Globals: globalViewData,
}
viewFile, layoutFile := vr.ViewEngine.FindView(vi)
vr.TemplateEngine.Render(viewFile, layoutFile, viewData, wr)
}
func (vr *ViewResult) ExecuteResult(ctx *HttpContext) {
vr.notShowDevError = true
vr.Render(ctx, vr.Body)
vr.ActionResult.ExecuteResult(ctx)
}
type ContentResult struct {
FilePath string
}
func (cr *ContentResult) ExecuteResult(ctx *HttpContext) {
http.ServeFile(ctx, ctx.Request, cr.FilePath)
}