-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
118 lines (112 loc) · 3.02 KB
/
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
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
package mserver
import (
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"net/http"
"net/url"
)
type Responser interface {
// Json Json输出
Json(obj interface{}) Responser
// Xml Xml输出
Xml(obj interface{}) Responser
// Html Html输出
Html(template string, obj interface{}) Responser
// Text string输出
Text(format string, values ...interface{}) Responser
// Redirect 重定向
Redirect(path string) Responser
// SetCookie 设置Cookie
SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) Responser
// SetStatus 设置状态码
SetStatus(code int) Responser
// SetHeader 设置header
SetHeader(key string, val string) Responser
// SetOkStatus 设置200状态
SetOkStatus() Responser
// Header header修订
Header(key, value string) Responser
}
func (ctx *Context) SetHeader(key string, val string) Responser {
ctx.resp.Header().Add(key, val)
return ctx
}
func (ctx *Context) Json(obj interface{}) Responser {
byt, err := json.Marshal(obj)
if err != nil {
return ctx.SetStatus(http.StatusInternalServerError)
}
ctx.SetHeader("Content-Type", "application/json")
ctx.respData = byt
return ctx
}
func (ctx *Context) SetStatus(code int) Responser {
ctx.respStatusCode = code
return ctx
}
func (ctx *Context) SetOkStatus() Responser {
ctx.resp.WriteHeader(http.StatusOK)
return ctx
}
func (ctx *Context) Redirect(path string) Responser {
http.Redirect(ctx.resp, ctx.req, path, http.StatusMovedPermanently)
return ctx
}
func (ctx *Context) Xml(obj interface{}) Responser {
byt, err := xml.Marshal(obj)
if err != nil {
return ctx.SetStatus(http.StatusInternalServerError)
}
ctx.SetHeader("Content-Type", "application/html")
ctx.respData = byt
return ctx
}
func (ctx *Context) Html(file string, obj interface{}) Responser {
// 读取模版文件,创建template实例
t, err := template.New("output").ParseFiles(file)
if err != nil {
return ctx
}
// 执行Execute方法将obj和模版进行结合
if err := t.Execute(ctx.resp, obj); err != nil {
return ctx
}
ctx.SetHeader("Content-Type", "application/html")
return ctx
}
func (ctx *Context) Text(format string, values ...interface{}) Responser {
out := fmt.Sprintf(format, values...)
ctx.SetHeader("Content-Type", "application/text")
ctx.respData = []byte(out)
return ctx
}
func (ctx *Context) SetCookie(key string, val string, maxAge int, path string,
domain string, secure bool, httpOnly bool) Responser {
if path == "" {
path = "/"
}
http.SetCookie(ctx.resp, &http.Cookie{
Name: key,
Value: url.QueryEscape(val),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: 1,
Secure: secure,
HttpOnly: httpOnly,
})
return ctx
}
// Header 是set和del方法的缩写,主要是重新响应的header
// 如果 value == "", 执行`ctx.resp.Header().Set(key, value)`
// 否则执行 `ctx.resp.Header().Set(key, value)`
func (ctx *Context) Header(key, value string) Responser {
if value == "" {
ctx.resp.Header().Del(key)
return ctx
}
ctx.resp.Header().Set(key, value)
return ctx
}