-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_response.go
46 lines (40 loc) · 1.05 KB
/
context_response.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
package xhttp
import (
"bytes"
xjson "github.com/goclub/json"
"net/http"
)
func (c *Context) Redirect(url string, code int) (err error) {
http.Redirect(c.Writer, c.Request, url, code)
return
}
func (c *Context) WriteStatusCode(statusCode int) {
c.Writer.WriteHeader(statusCode)
}
// WriteBytes 等同于 writer.Write(data) ,但函数签名返回 error 不返回 int
func (c *Context) WriteBytes(b []byte) error {
_, err := c.Writer.Write(b)
if err != nil {
return err
}
return nil
}
// Render 设置 header Content-Type text/html; charset=UTF-8 并输出 buffer
func (c *Context) Render(render func(buffer *bytes.Buffer) error) error {
buffer := bytes.NewBuffer(nil)
err := render(buffer)
if err != nil {
return err
}
c.Writer.Header().Set("Content-Type", "text/html; charset=UTF-8")
return c.WriteBytes(buffer.Bytes())
}
// WriteJSON 响应 json
func (c *Context) WriteJSON(v interface{}) error {
data, err := xjson.Marshal(v)
if err != nil {
return err
}
c.Writer.Header().Set("Content-Type", "application/json")
return c.WriteBytes(data)
}