-
Notifications
You must be signed in to change notification settings - Fork 16
/
context_render.go
250 lines (209 loc) · 6.11 KB
/
context_render.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package rux
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"time"
"github.com/gookit/goutil/basefn"
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/rux/pkg/render"
)
// Render html template.
//
// please use ShouldRender() instead
func (c *Context) Render(status int, name string, data any) (err error) {
// TODO refactoring...
// return renderer.Render(c.Resp, obj)
if c.router.Renderer == nil {
return errors.New("rux: renderer not registered")
}
var buf = new(bytes.Buffer)
if err = c.router.Renderer.Render(buf, name, data, c); err != nil {
return err
}
c.HTML(status, buf.Bytes())
return
}
// ShouldRender render and response to client
func (c *Context) ShouldRender(status int, obj any, renderer render.Renderer) error {
c.SetStatus(status)
return renderer.Render(c.Resp, obj)
}
// MustRender render and response to client
func (c *Context) MustRender(status int, obj any, renderer render.Renderer) {
c.Respond(status, obj, renderer)
}
// Respond render and response to client
func (c *Context) Respond(status int, obj any, renderer render.Renderer) {
c.SetStatus(status)
err := renderer.Render(c.Resp, obj)
if err != nil {
c.AddError(err)
}
}
/*************************************************************
* data response render
*************************************************************/
// HTTPError response
func (c *Context) HTTPError(msg string, status int) {
http.Error(c.Resp, msg, status)
}
// NoContent serve success but no content response
func (c *Context) NoContent() {
c.Resp.WriteHeader(http.StatusNoContent)
}
// Redirect other URL with status code(3xx e.g 301, 302).
func (c *Context) Redirect(path string, optionalCode ...int) {
// default is 301
code := http.StatusMovedPermanently
if len(optionalCode) > 0 {
code = optionalCode[0]
}
http.Redirect(c.Resp, c.Req, path, code)
}
// Back redirect to referer url
func (c *Context) Back(optionalCode ...int) {
// default is 302
code := basefn.FirstOr(optionalCode, http.StatusFound)
c.Redirect(c.Req.Referer(), code)
}
// Text writes out a string as plain text.
func (c *Context) Text(status int, str string) {
c.Blob(status, httpctype.Text, []byte(str))
}
// HTML writes out as html text. if data is empty, only write headers
func (c *Context) HTML(status int, data []byte) {
c.Blob(status, httpctype.HTML, data)
}
// HTMLString writes out as html text. if data is empty, only write headers
func (c *Context) HTMLString(status int, data string) {
c.Blob(status, httpctype.HTML, []byte(data))
}
// Blob writes out []byte
func (c *Context) Blob(status int, contentType string, data []byte) {
c.Resp.WriteHeader(status)
c.Resp.Header().Set(ContentType, contentType)
if len(data) > 0 {
c.WriteBytes(data)
}
}
// Stream writes out io.Reader
func (c *Context) Stream(status int, contentType string, r io.Reader) {
c.Resp.WriteHeader(status)
c.Resp.Header().Set(ContentType, contentType)
_, err := io.Copy(c.Resp, r)
if err != nil {
c.AddError(err)
}
}
// JSON writes out a JSON response.
func (c *Context) JSON(status int, obj any) {
c.Respond(status, obj, render.JSONRenderer{})
}
// JSONBytes writes out a string as JSON response.
func (c *Context) JSONBytes(status int, bs []byte) {
c.Blob(status, httpctype.JSON, bs)
}
// XML output xml response.
func (c *Context) XML(status int, obj any, indents ...string) {
var indent string
if len(indents) > 0 && indents[0] != "" {
indent = indents[0]
}
c.Respond(status, obj, render.XMLRenderer{Indent: indent})
}
// JSONP is JSONP response.
func (c *Context) JSONP(status int, callback string, obj any) {
c.Respond(status, obj, render.JSONPRenderer{Callback: callback})
}
// File writes the specified file into the body stream in a efficient way.
func (c *Context) File(filePath string) {
http.ServeFile(c.Resp, c.Req, filePath)
}
// FileContent serves given file as text content to response.
func (c *Context) FileContent(file string, names ...string) {
var name string
if len(names) > 0 {
name = names[0]
} else {
name = path.Base(file)
}
f, err := os.Open(file)
if err != nil {
http.Error(c.Resp, "Internal Server Error", 500)
return
}
//noinspection GoUnhandledErrorResult
defer f.Close()
c.setRawContentHeader(c.Resp, false)
http.ServeContent(c.Resp, c.Req, name, time.Now(), f)
}
// Attachment a file to response.
//
// Usage:
//
// c.Attachment("path/to/some.zip", "new-name.zip")
func (c *Context) Attachment(srcFile, outName string) {
c.dispositionContent(c.Resp, http.StatusOK, outName, false)
c.FileContent(srcFile)
}
// Inline file content.
//
// Usage:
//
// c.Inline("testdata/site.md", "new-name.md")
func (c *Context) Inline(srcFile, outName string) {
c.dispositionContent(c.Resp, http.StatusOK, outName, true)
c.FileContent(srcFile)
}
// Binary serve data as Binary response.
//
// Usage:
//
// in, _ := os.Open("./README.md")
// r.Binary(http.StatusOK, in, "readme.md", true)
func (c *Context) Binary(status int, in io.ReadSeeker, outName string, inline bool) {
c.dispositionContent(c.Resp, status, outName, inline)
// _, err := io.Copy(c.Resp, in)
http.ServeContent(c.Resp, c.Req, outName, time.Now(), in)
}
// Stream read
// func (c *Context) Stream(step func(w io.Writer) bool) {
// w := c.Resp
// clientGone := w.(http.CloseNotifier).CloseNotify()
// for {
// select {
// case <-clientGone:
// return
// default:
// keepOpen := step(w)
// w.(http.Flusher).Flush()
// if !keepOpen {
// return
// }
// }
// }
// }
func (c *Context) dispositionContent(w http.ResponseWriter, status int, outName string, inline bool) {
dispositionType := dispositionAttachment
if inline {
dispositionType = dispositionInline
}
// "application/octet-stream"
w.Header().Set(httpctype.Key, httpctype.Binary)
w.Header().Set(ContentDisposition, fmt.Sprintf("%s; filename=%s", dispositionType, outName))
w.WriteHeader(status)
}
func (c *Context) setRawContentHeader(w http.ResponseWriter, addType bool) {
w.Header().Set("Content-Description", "Raw content")
if addType {
w.Header().Set(httpctype.Key, "text/plain")
}
w.Header().Set("Expires", "0")
w.Header().Set("Cache-Control", "must-revalidate")
w.Header().Set("Pragma", "public")
}