-
Notifications
You must be signed in to change notification settings - Fork 11
/
context.go
279 lines (241 loc) · 7.57 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package echo
import (
"context"
"io"
"mime/multipart"
"net/http"
"time"
"github.com/admpub/events"
pkgCode "github.com/webx-top/echo/code"
"github.com/webx-top/echo/engine"
"github.com/webx-top/echo/logger"
"github.com/webx-top/echo/param"
)
// Context represents context for the current request. It holds request and
// response objects, path parameters, data and registered handler.
type Context interface {
context.Context
events.Emitterer
SetEmitterer(events.Emitterer)
Handler() Handler
//Transaction
SetTransaction(t Transaction)
Transaction() Transaction
Begin() error
Rollback() error
Commit() error
End(succeed bool) error
//Standard Context
StdContext() context.Context
WithContext(ctx context.Context) *http.Request
SetValue(key string, value interface{})
SetValidator(Validator)
Validator() Validator
Validate(item interface{}, args ...interface{}) error
Translator
SetTranslator(Translator)
Request() engine.Request
Response() engine.Response
Handle(Context) error
Logger() logger.Logger
Object() *xContext
Echo() *Echo
Route() *Route
Reset(engine.Request, engine.Response)
Dispatch(route string) Handler
//----------------
// Param
//----------------
Path() string
P(int, ...string) string
Param(string, ...string) string
// ParamNames returns path parameter names.
ParamNames() []string
ParamValues() []string
SetParamNames(names ...string)
SetParamValues(values ...string)
// Host
HostParamNames() []string
HostParamValues() []string
HostParam(string, ...string) string
HostP(int, ...string) string
SetHostParamNames(names ...string)
SetHostParamValues(values ...string)
// Queries returns the query parameters as map. It is an alias for `engine.URL#Query()`.
Queries() map[string][]string
QueryValues(string) []string
QueryxValues(string) param.StringSlice
Query(string, ...string) string
//----------------
// Form data
//----------------
Form(string, ...string) string
FormValues(string) []string
FormxValues(string) param.StringSlice
// Forms returns the form parameters as map. It is an alias for `engine.Request#Form().All()`.
Forms() map[string][]string
// Param+
Px(int, ...string) param.String
Paramx(string, ...string) param.String
Queryx(string, ...string) param.String
Formx(string, ...string) param.String
// string to param.String
Atop(string) param.String
ToParamString(string) param.String
ToStringSlice([]string) param.StringSlice
//----------------
// Context data
//----------------
Set(string, interface{})
Get(string, ...interface{}) interface{}
Incr(key string, n interface{}, defaults ...interface{}) int64
Decr(key string, n interface{}, defaults ...interface{}) int64
Delete(...string)
Stored() Store
Internal() *param.SafeMap
//----------------
// Bind
//----------------
Bind(interface{}, ...FormDataFilter) error
BindAndValidate(interface{}, ...FormDataFilter) error
MustBind(interface{}, ...FormDataFilter) error
MustBindAndValidate(interface{}, ...FormDataFilter) error
BindWithDecoder(interface{}, BinderValueCustomDecoders, ...FormDataFilter) error
BindAndValidateWithDecoder(interface{}, BinderValueCustomDecoders, ...FormDataFilter) error
MustBindWithDecoder(interface{}, BinderValueCustomDecoders, ...FormDataFilter) error
MustBindAndValidateWithDecoder(interface{}, BinderValueCustomDecoders, ...FormDataFilter) error
//----------------
// Response data
//----------------
Render(string, interface{}, ...int) error
RenderBy(string, func(string) ([]byte, error), interface{}, ...int) ([]byte, error)
HTML(string, ...int) error
String(string, ...int) error
Blob([]byte, ...int) error
JSON(interface{}, ...int) error
JSONBlob([]byte, ...int) error
JSONP(string, interface{}, ...int) error
XML(interface{}, ...int) error
XMLBlob([]byte, ...int) error
Stream(func(io.Writer) bool) error
SSEvent(string, chan interface{}) error
File(string, ...http.FileSystem) error
CacheableFile(string, time.Duration, ...http.FileSystem) error
Attachment(io.Reader, string, time.Time, ...bool) error
CacheableAttachment(io.Reader, string, time.Time, time.Duration, ...bool) error
NotModified() error
NoContent(...int) error
Redirect(string, ...int) error
Error(err error)
NewError(code pkgCode.Code, msg string, args ...interface{}) *Error
NewErrorWith(err error, code pkgCode.Code, args ...interface{}) *Error
SetCode(int)
Code() int
SetData(Data)
Data() Data
IsValidCache(modifiedAt time.Time) bool
SetCacheHeader(modifiedAt time.Time, maxAge ...time.Duration)
// ServeContent sends static content from `io.Reader` and handles caching
// via `If-Modified-Since` request header. It automatically sets `Content-Type`
// and `Last-Modified` response headers.
ServeContent(io.Reader, string, time.Time, ...time.Duration) error
ServeCallbackContent(func(Context) (io.Reader, error), string, time.Time, ...time.Duration) error
//----------------
// FuncMap
//----------------
SetFunc(string, interface{})
GetFunc(string) interface{}
ResetFuncs(map[string]interface{})
Funcs() map[string]interface{}
PrintFuncs()
//----------------
// Render
//----------------
SetAuto(on bool) Context
Fetch(string, interface{}) ([]byte, error)
SetRenderer(Renderer)
SetRenderDataWrapper(DataWrapper)
Renderer() Renderer
RenderDataWrapper() DataWrapper
//----------------
// Cookie
//----------------
SetCookieOptions(*CookieOptions)
CookieOptions() *CookieOptions
NewCookie(string, string) *http.Cookie
Cookie() Cookier
GetCookie(string) string
// SetCookie @param:key,value,maxAge(seconds),path(/),domain,secure,httpOnly,sameSite(lax/strict/default)
SetCookie(string, string, ...interface{})
//----------------
// Session
//----------------
SetSessionOptions(*SessionOptions)
SessionOptions() *SessionOptions
SetSessioner(Sessioner)
Session() Sessioner
Flash(...string) interface{}
//----------------
// Request data
//----------------
Header(string) string
IsAjax() bool
IsPjax() bool
PjaxContainer() string
Method() string
Format() string
SetFormat(string)
IsPost() bool
IsGet() bool
IsPut() bool
IsDel() bool
IsHead() bool
IsPatch() bool
IsOptions() bool
IsSecure() bool
IsWebsocket() bool
IsUpload() bool
ResolveContentType() string
WithFormatExtension(bool)
SetDefaultExtension(string)
DefaultExtension() string
ResolveFormat() string
Accept() *Accepts
Protocol() string
Site() string
RequestURI() string
Scheme() string
Domain() string
Host() string
Proxy() []string
Referer() string
Port() int
RealIP() string
HasAnyRequest() bool
MapForm(i interface{}, names ...string) error
MapData(i interface{}, data map[string][]string, names ...string) error
SaveUploadedFile(fieldName string, saveAbsPath string, saveFileName ...func(*multipart.FileHeader) (string, error)) (*multipart.FileHeader, error)
SaveUploadedFileToWriter(string, io.Writer) (*multipart.FileHeader, error)
//Multiple file upload
SaveUploadedFiles(fieldName string, savePath func(*multipart.FileHeader) (string, error)) error
SaveUploadedFilesToWriter(fieldName string, writer func(*multipart.FileHeader) (io.Writer, error)) error
//----------------
// Hook
//----------------
AddPreResponseHook(func() error) Context
SetPreResponseHook(...func() error) Context
OnHostFound(func(Context) (bool, error)) Context
FireHostFound() (bool, error)
}
type eCtxKey struct{}
var contextKey eCtxKey
func FromStdContext(c context.Context) (Context, bool) {
ctx, ok := c.Value(contextKey).(Context)
return ctx, ok
}
func ToStdContext(ctx context.Context, eCtx Context) context.Context {
return context.WithValue(ctx, contextKey, eCtx)
}
func AsStdContext(eCtx Context) context.Context {
return ToStdContext(eCtx, eCtx)
}