/
framework_response.go
168 lines (141 loc) · 3.44 KB
/
framework_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
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
package gin
import (
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"net/http"
"net/url"
)
// IResponse代表返回方法
type IResponse interface {
// Json输出
IJson(obj interface{}) IResponse
// Jsonp输出
IJsonp(obj interface{}) IResponse
//xml输出
IXml(obj interface{}) IResponse
// html输出
IHtml(file string, obj interface{}) IResponse
// string
IText(format string, values ...interface{}) IResponse
// 重定向
IRedirect(path string) IResponse
// header
ISetHeader(key string, val string) IResponse
// Cookie
ISetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse
// 设置状态码
ISetStatus(code int) IResponse
// 设置200状态
ISetOkStatus() IResponse
}
func (ctx *Context) IJson(obj interface{}) IResponse {
byt, err := json.Marshal(obj)
if err != nil {
return ctx.ISetStatus(http.StatusInternalServerError)
}
ctx.ISetHeader("Content-Type", "application/json")
ctx.Writer.Write(byt)
return ctx
}
// Jsonp输出
func (ctx *Context) IJsonp(obj interface{}) IResponse {
callbackFunc := ctx.Query("callback")
ctx.ISetHeader("Content-Type", "application/javascript")
//进行字符串过滤,防止xss攻击
callback := template.JSEscapeString(callbackFunc)
//输出函数名
_, err := ctx.Writer.Write([]byte(callback))
if err != nil {
return ctx
}
//输出左括号
_, err = ctx.Writer.Write([]byte("("))
if err != nil {
return ctx
}
//数据函数参数
ret, err := json.Marshal(obj)
if err != nil {
return ctx
}
_, err = ctx.Writer.Write(ret)
if err != nil {
return ctx
}
//输出右括号
_, err = ctx.Writer.Write([]byte(")"))
if err != nil {
return ctx
}
return ctx
}
// xml输出
func (ctx *Context) IXml(obj interface{}) IResponse {
byt, err := xml.Marshal(obj)
if err != nil {
return ctx.ISetStatus(http.StatusInternalServerError)
}
ctx.ISetHeader("Content-Type", "application/html")
ctx.Writer.Write(byt)
return ctx
}
// html输出
func (ctx *Context) IHtml(file string, obj interface{}) IResponse {
//读取模板文件,创建template实例
t, err := template.New("output").ParseFiles(file)
if err != nil {
return ctx
}
//执行execute方法将obj和模板结合
if err := t.Execute(ctx.Writer, obj); err != nil {
return ctx
}
ctx.ISetHeader("Content-Type", "application/html")
return ctx
}
// string
func (ctx *Context) IText(format string, values ...interface{}) IResponse {
out := fmt.Sprintf(format, values...)
ctx.ISetHeader("Content-Type", "application/text")
ctx.Writer.Write([]byte(out))
return ctx
}
// 重定向
func (ctx *Context) IRedirect(path string) IResponse {
http.Redirect(ctx.Writer, ctx.Request, path, http.StatusMovedPermanently)
return ctx
}
// header
func (ctx *Context) ISetHeader(key string, val string) IResponse {
ctx.Writer.Header().Add(key, val)
return ctx
}
// Cookie
func (ctx *Context) ISetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse {
if path == "" {
path = "/"
}
http.SetCookie(ctx.Writer, &http.Cookie{
Name: key,
Value: url.QueryEscape(val),
MaxAge: maxAge,
Path: path,
Domain: domain,
SameSite: 1,
Secure: secure,
HttpOnly: httpOnly,
})
return ctx
}
// 设置状态码
func (ctx *Context) ISetStatus(code int) IResponse {
ctx.Writer.WriteHeader(code)
return ctx
}
// 设置200状态
func (ctx *Context) ISetOkStatus() IResponse {
ctx.Writer.WriteHeader(http.StatusOK)
return ctx
}