-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
170 lines (132 loc) · 3.35 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
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
package framework
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"text/template"
)
type IResponse interface {
// json输出
Json(obj any) IResponse
// jsonp输出
Jsonp(obj any) IResponse
// xml输出
Xml(obj any) IResponse
// html输出
Html(file string, obj any) IResponse
// string
Text(format string, values ...any) IResponse
// 重定向
Redirect(path string) IResponse
// header
SetHeader(key string, val string) IResponse
// Cookie
SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse
// 设置状态码
SetStatus(code int) IResponse
// 设置200状态
SetOkStatus() IResponse
}
func (ctx *Context) Json(obj any) IResponse {
byt, err := json.Marshal(obj)
if err != nil {
return ctx.SetStatus(http.StatusInternalServerError)
}
ctx.SetHeader("Content-Type", "application/json")
ctx.responseWriter.Write(byt)
return ctx
}
func (ctx *Context) Jsonp(obj any) IResponse {
// 获取请求参数callback
callbackFunc, _ := ctx.QueryString("callback", "callback_function")
ctx.SetHeader("Content-Type", "application/javascript")
// 输出到前端页面的时候需要注意下进行字符过滤,否则有可能造成xss攻击
callback := template.JSEscapeString(callbackFunc)
// 输出函数名
_, err := ctx.responseWriter.Write([]byte(callback))
if err != nil {
return ctx
}
// 输出左括号
_, err = ctx.responseWriter.Write([]byte("("))
if err != nil {
return ctx
}
// 数据函数参数
ret, err := json.Marshal(obj)
if err != nil {
return ctx
}
_, err = ctx.responseWriter.Write(ret)
if err != nil {
return ctx
}
// 输出右括号
_, err = ctx.responseWriter.Write([]byte(")"))
if err != nil {
return ctx
}
return ctx
}
func (ctx *Context) Xml(obj any) IResponse {
byt, err := xml.Marshal(obj)
if err != nil {
return ctx.SetStatus(http.StatusInternalServerError)
}
ctx.SetHeader("Content-Type", "application/xml")
ctx.responseWriter.Write(byt)
return ctx
}
// html输出
func (ctx *Context) Html(file string, obj any) IResponse {
t, err := template.New("output").ParseFiles(file)
if err != nil {
return ctx
}
if err := t.Execute(ctx.responseWriter, obj); err != nil {
return ctx
}
ctx.SetHeader("Content-Type", "application/html")
return ctx
}
// string
func (ctx *Context) Text(format string, values ...any) IResponse {
out := fmt.Sprintf(format, values...)
ctx.SetHeader("Content-Type", "application/text")
ctx.responseWriter.Write([]byte(out))
return ctx
}
// 重定向
func (ctx *Context) Redirect(path string) IResponse {
http.Redirect(ctx.responseWriter, ctx.request, path, http.StatusMovedPermanently)
return ctx
}
func (ctx *Context) SetHeader(key string, val string) IResponse {
ctx.responseWriter.Header().Add(key, val)
return ctx
}
func (ctx *Context) SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) IResponse {
if path == "" {
path = "/"
}
http.SetCookie(ctx.responseWriter, &http.Cookie{
Name: key,
Value: val,
MaxAge: maxAge,
Path: path,
Domain: domain,
Secure: secure,
HttpOnly: httpOnly,
})
return ctx
}
func (ctx *Context) SetStatus(code int) IResponse {
ctx.responseWriter.WriteHeader(code)
return ctx
}
// 设置200状态
func (ctx *Context) SetOkStatus() IResponse {
ctx.responseWriter.WriteHeader(http.StatusOK)
return ctx
}