/
httpcontext_renderfunc.go
79 lines (65 loc) 路 2.53 KB
/
httpcontext_renderfunc.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 Context
import (
"github.com/maxzhang1985/yoyogo/ActionResult"
"net/http"
)
func (ctx *HttpContext) HTML(code int, name string, obj interface{}) {
htmlRender := ActionResult.HTMLDebug{Files: nil,
Glob: "../Static/template/**",
Delims: ActionResult.Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
instance := htmlRender.Instance(name, obj)
_ = instance.Render(ctx.Response)
}
func (ctx *HttpContext) IndentedJSON(code int, obj interface{}) {
ctx.Render(code, ActionResult.IndentedJson{Data: obj})
}
func (c *HttpContext) SecureJSON(code int, obj interface{}) {
c.Render(code, ActionResult.SecureJson{Prefix: "", Data: obj})
}
func (c *HttpContext) JSONP(code int, obj interface{}) {
callback := c.QueryStringOrDefault("callback", "")
if callback == "" {
c.Render(code, ActionResult.Json{Data: obj})
return
}
c.Render(code, ActionResult.Jsonp{Callback: callback, Data: obj})
}
func (c *HttpContext) JSON(code int, obj interface{}) {
c.Render(code, ActionResult.Json{Data: obj})
}
// AsciiJSON serializes the given struct as JSON into the response body with unicode to ASCII string.
// It also sets the Content-Type as "application/json".
func (c *HttpContext) AsciiJSON(code int, obj interface{}) {
c.Render(code, ActionResult.AsciiJson{Data: obj})
}
// PureJSON serializes the given struct as JSON into the response body.
// PureJSON, unlike JSON, does not replace special html characters with their unicode entities.
func (c *HttpContext) PureJSON(code int, obj interface{}) {
c.Render(code, ActionResult.PureJson{Data: obj})
}
// XML serializes the given struct as XML into the response body.
// It also sets the Content-Type as "application/xml".
func (c *HttpContext) XML(code int, obj interface{}) {
c.Render(code, ActionResult.XML{Data: obj})
}
// YAML serializes the given struct as YAML into the response body.
func (c *HttpContext) YAML(code int, obj interface{}) {
c.Render(code, ActionResult.YAML{Data: obj})
}
// ProtoBuf serializes the given struct as ProtoBuf into the response body.
func (c *HttpContext) ProtoBuf(code int, obj interface{}) {
c.Render(code, ActionResult.ProtoBuf{Data: obj})
}
// String writes the given string into the response body.
func (c *HttpContext) Text(code int, format string, values ...interface{}) {
c.Render(code, ActionResult.Text{Format: format, Data: values})
}
func (c *HttpContext) File(filepath string) {
http.ServeFile(c.Response, c.Request, filepath)
}
func (c *HttpContext) FileStream(code int, bytes []byte) {
render := ActionResult.FormFileStream(bytes)
c.Render(code, render)
}