-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult.go
150 lines (120 loc) · 2.59 KB
/
result.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//Copyright
//go mvc web framework
//http result
package gomvc
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"time"
)
//execute error result
func (ctx *HttpContext) ExeError(err string) {
e := ErrorResult{Data: err}
e.Execute(ctx)
}
//http result interface
type HttpResult interface {
Execute(ctx *HttpContext)
}
//static file
type FileResult struct {
Data string //file path
}
//render file
func (file *FileResult) Execute(ctx *HttpContext) {
http.ServeFile(ctx.Resonse, ctx.Request, file.Data)
}
//http raw content
type ContentResult struct {
ModifyTime time.Time //modify time
ContentType string //content type
Data interface{} //content
}
//render file
func (c *ContentResult) Execute(ctx *HttpContext) {
if r, ok := c.Data.(io.ReadSeeker); ok {
http.ServeContent(ctx.Resonse, ctx.Request, c.ContentType, c.ModifyTime, r)
return
}
fmt.Fprintln(ctx.Resonse, c.Data)
//http.ServeContent(ctx.Resonse, ctx.Request, c.Name, c.ModifyTime, c.Data)
}
//javascript ContentType = "application/x-javascript";
type JavaScriptResult struct {
Data interface{} //data
}
//render javascript
func (j *JavaScriptResult) Execute(ctx *HttpContext) {
//TODO
}
//json ContentType = "application/json"
type JsonResult struct {
Data interface{} //data
}
//render json
func (j *JsonResult) Execute(ctx *HttpContext) {
b, err := json.Marshal(j.Data)
if err != nil {
ctx.ExeError(err.Error())
return
}
ctx.ContentType(ContentTypeJson)
ctx.Write(b)
}
//xml ContentType = "application/xml"
type XmlResult struct {
Data interface{} //data
}
//render xml
func (x *XmlResult) Execute(ctx *HttpContext) {
b, err := xml.Marshal(x.Data)
if err != nil {
ctx.ExeError(err.Error())
return
}
ctx.ContentType(ContentTypeXml)
ctx.Write(b)
}
//jsonp
type JsonpResult struct {
Data interface{} //data
}
//render jsonp
func (j *JsonpResult) Execute(ctx *HttpContext) {
//TODO
}
//view
type ViewResult struct {
}
//Partial View
type PartialViewResult struct {
}
//redirect
type RedirectResult struct {
Permanent bool //permanent redirect or not
Path string
}
//http 404 TODO
type NotFoundResult struct {
}
//TODO: render 404.xxx view page
func (r *NotFoundResult) Execute(ctx *HttpContext) {
http.Error(ctx.Resonse, MsgNotFound, http.StatusNotFound)
}
//Error
type ErrorResult struct {
Data string
}
//TODO: render error.xxx view page
func (r *ErrorResult) Execute(ctx *HttpContext) {
http.Error(ctx.Resonse, r.Data, http.StatusInternalServerError)
}
//VoidResult
type VoidResult struct {
}
func (r *VoidResult) Execute(ctx *HttpContext) {
ctx.Resonse.Write([]byte(``))
}