/
context.go
250 lines (216 loc) · 5.34 KB
/
context.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package echo
import (
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"time"
"github.com/2637309949/dolphin/packages/logrus"
"github.com/2637309949/dolphin/packages/web/core"
"github.com/eriklott/mustache"
"github.com/labstack/echo/v4"
)
var _ core.Context = &Context{}
type Context struct {
echo.Context
next func()
}
// Set defined TODO
func (c *Context) Set(k string, v interface{}) {
c.Context.Set(k, v)
}
// Get defined TODO
func (c *Context) Get(k string) interface{} {
return c.Context.Get(k)
}
// Param defined TODO
func (c *Context) Param(k string) string {
return c.Context.Param(k)
}
// Query defined TODO
func (c *Context) Query(k string) string {
return c.Context.QueryParam(k)
}
// MultipartForm defined TODO
func (c *Context) MultipartForm() (*multipart.Form, error) {
return c.Context.MultipartForm()
}
// Header defined TODO
func (c *Context) Header(key, value string) {
c.Context.Response().Writer.Header().Set(key, value)
}
// GetHeader defined TODO
func (c *Context) GetHeader(key string) string {
return c.Context.Request().Header.Get(key)
}
// Request defined TODO
func (c *Context) SetRequest(r *http.Request) {
c.Context.SetRequest(r)
}
// Request defined TODO
func (c *Context) Request() *http.Request {
return c.Context.Request()
}
// ShouldBindWith defined TODO
func (c *Context) ShouldBindWith(v interface{}) error {
return c.Context.Bind(v)
}
// GetRawData defined TODO
func (c *Context) GetRawData() ([]byte, error) {
return ioutil.ReadAll(c.Context.Request().Body)
}
// SetCookie defined TODO
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
if path == "" {
path = "/"
}
c.Context.SetCookie(&http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
Secure: secure,
HttpOnly: httpOnly,
})
}
// Cookie defined TODO
func (c *Context) Cookie(name string) (string, error) {
cookie, err := c.Context.Request().Cookie(name)
if err != nil {
return "", err
}
val, _ := url.QueryUnescape(cookie.Value)
return val, nil
}
// Success defined TODO
func (c *Context) Success(data interface{}) {
c.Context.JSON(http.StatusOK, map[string]interface{}{
"code": 200,
"data": data,
})
}
// JSON defined TODO
func (c *Context) JSON(code int, i interface{}) {
c.Context.JSON(code, i)
}
// Next defined TODO
func (c *Context) Next() {
if c.next != nil {
c.next()
}
}
// Abort defined TODO
func (c *Context) Abort() {
}
// Fail defined TODO
func (c *Context) Fail(err error) {
c.Context.JSON(http.StatusOK, map[string]interface{}{
"code": 500,
"detail": err.Error(),
})
}
// File defined TODO
func (c *Context) File(r io.Reader, filename string, context ...interface{}) {
c.Context.Response().Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
file, err := ioutil.TempFile("", "*")
if err != nil {
return
}
defer os.Remove(file.Name())
bte, err := ioutil.ReadAll(r)
if err != nil {
logrus.Error(c, err)
return
}
str, err := mustache.NewTemplate().Render(string(bte), context...)
if err != nil {
logrus.Error(c, err)
return
}
if _, err = file.WriteString(str); err != nil {
logrus.Error(c, err)
return
}
http.ServeFile(c.Context.Response().Writer, c.Context.Request(), file.Name())
}
// HTML defined TODO
func (c *Context) HTML(r io.Reader, context ...interface{}) {
c.Context.Response().Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
file, err := ioutil.TempFile("", "*")
if err != nil {
return
}
defer os.Remove(file.Name())
bte, err := ioutil.ReadAll(r)
if err != nil {
return
}
str, err := mustache.NewTemplate().Render(string(bte), context...)
if err != nil {
return
}
if _, err = file.WriteString(str); err != nil {
return
}
http.ServeFile(c.Context.Response().Writer, c.Context.Request(), file.Name())
}
// XML defined TODO
func (c *Context) XML(r io.Reader, context ...interface{}) {
c.Context.Response().Writer.Header().Set("Content-Type", "application/xml; charset=utf-8")
file, err := ioutil.TempFile("", "*")
if err != nil {
return
}
defer os.Remove(file.Name())
bte, err := ioutil.ReadAll(r)
if err != nil {
return
}
str, err := mustache.NewTemplate().Render(string(bte), context...)
if err != nil {
return
}
if _, err = file.WriteString(str); err != nil {
return
}
http.ServeFile(c.Context.Response().Writer, c.Context.Request(), file.Name())
}
// XML defined TODO
func (c *Context) String(data string, context ...interface{}) {
str, err := mustache.NewTemplate().Render(data, context...)
if err != nil {
return
}
c.Context.String(200, str)
}
// Request defined TODO
func (c *Context) ResponseWriter() http.ResponseWriter {
return c.Context.Response().Writer
}
// Request defined TODO
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return c.Context.Request().Context().Deadline()
}
// Done defined TODO
func (c *Context) Done() <-chan struct{} {
return c.Context.Request().Context().Done()
}
// Err defined TODO
func (c *Context) Err() error {
return c.Context.Request().Context().Err()
}
// Value defined TODO
func (c *Context) Value(key interface{}) interface{} {
return c.Context.Request().Context().Err()
}
func NewContext(ctx echo.Context, cb ...func()) *Context {
var cb1 func()
if len(cb) > 0 {
cb1 = cb[0]
}
return &Context{ctx, cb1}
}