-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
102 lines (84 loc) · 1.84 KB
/
controller.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
package ext
import (
"io"
"time"
"net/http"
"github.com/h2object/h2object/template"
)
type Controller struct{
Request *Request
Response *Response
}
func NewController(request *Request, response *Response) *Controller {
return &Controller{
Request: request,
Response: response,
}
}
func (r *Controller) JsonError(status int, err error) {
JsonErrorResult{
code: status,
err: err,
pretty: r.Request.Pretty,
}.Apply(r.Request, r.Response)
}
func (r *Controller) TextError(status int, err error) {
TextErrorResult{
code: status,
err: err,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Json(data interface{}) {
JsonResult{
obj: data,
pretty: r.Request.Pretty,
callback: r.Request.Callback,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Xml(data interface{}) {
XmlResult{
obj: data,
pretty: r.Request.Pretty,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Text(text string) {
TextResult{
text: text,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Html(html string) {
HtmlResult{
html: html,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Read(content_type string, rd io.Reader, sz int64) {
ReaderResult{
content_type: content_type,
read: rd,
size: sz,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Template(t template.Template, data interface{}) {
TemplateResult{
t: t,
data: data,
}.Apply(r.Request, r.Response)
}
func (r *Controller) Redirect(uri string, query string) {
RedirectResult{
url: uri,
rawQuery: query,
}.Apply(r.Request, r.Response)
}
func (r *Controller) File(path string) {
http.ServeFile(r.Response.Out, r.Request.Request, path)
}
func (r *Controller) Binary(name string, rd io.Reader, size int64) {
BinaryResult{
Reader: rd,
Name: name,
Length: size,
Delivery: Attachment,
ModTime: time.Now(),
}.Apply(r.Request, r.Response)
}