-
Notifications
You must be signed in to change notification settings - Fork 5
/
context_render.go
61 lines (52 loc) · 1.07 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
package xingyun
import (
"encoding/json"
"mime"
"strings"
)
func (ctx *Context) SetContentType(val string) string {
var ctype string
if strings.ContainsRune(val, '/') {
ctype = val
} else {
if !strings.HasPrefix(val, ".") {
val = "." + val
}
ctype = mime.TypeByExtension(val)
}
if ctype != "" {
ctx.ResponseWriter.Header().Set("Content-Type", ctype)
}
return ctype
}
func (ctx *Context) WriteString(s string) {
_, err := ctx.Write([]byte(s))
if err != nil {
ctx.Logger.Errorf(err.Error())
return
}
}
func (ctx *Context) WriteJSON(obj interface{}) {
bs, err := json.Marshal(obj)
if err != nil {
panic(err)
}
ctx.Write(bs)
}
func (ctx *Context) NotModified() {
ctx.WriteHeader(304)
}
func (ctx *Context) NotFound() {
ctx.WriteHeader(404)
}
func (ctx *Context) Unauthorized() {
ctx.WriteHeader(401)
}
func (ctx *Context) Forbidden() {
ctx.WriteHeader(403)
}
func (ctx *Context) Redirect(url_ string) {
ctx.ResponseWriter.Header().Set("Location", url_)
ctx.ResponseWriter.WriteHeader(302)
ctx.ResponseWriter.Write([]byte("Redirecting to: " + url_))
}