-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext.go
312 lines (261 loc) · 6.83 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2012 by sdm. All rights reserved.
// license that can be found in the LICENSE file.
package wk
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
// RouteData is wrap of route data
type RouteData map[string]string
// Int parse route value as int
func (r RouteData) Int(name string) (int, bool) {
if s, ok := r[name]; ok {
if i, err := strconv.Atoi(s); err == nil {
return i, true
}
}
return 0, false
}
// Int return route value as int or v
func (r RouteData) IntOr(name string, v int) int {
if s, ok := r[name]; ok {
if i, err := strconv.Atoi(s); err == nil {
return i
}
}
return v
}
// Bool parse route value as bool
func (r RouteData) Bool(name string) (bool, bool) {
if s, ok := r[name]; ok {
if b, err := strconv.ParseBool(s); err == nil {
return b, true
}
}
return false, false
}
// BoolOr return route value as bool or v
func (r RouteData) BoolOr(name string, v bool) bool {
if s, ok := r[name]; ok {
if b, err := strconv.ParseBool(s); err == nil {
return b
}
}
return v
}
// Float parse route value as float64
func (r RouteData) Float(name string) (float64, bool) {
if s, ok := r[name]; ok {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f, true
}
}
return 0, false
}
// FloatOr return route value as float64 or v
func (r RouteData) FloatOr(name string, v float64) float64 {
if s, ok := r[name]; ok {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
}
return v
}
// Str return route value
func (r RouteData) Str(name string) (string, bool) {
if s, ok := r[name]; ok {
return s, true
}
return "", false
}
// Str return route value or v if name donesn't exist
func (r RouteData) StrOr(name string, v string) string {
if s, ok := r[name]; ok {
return s
}
return v
}
// HttpContext is wrap of request & response
type HttpContext struct {
// Server is current http server
Server *HttpServer
// Request is *http.Request
Request *http.Request
// Resonse is http.ResponseWriter
Resonse http.ResponseWriter
// Method is http method
Method string
// RequestPath is
RequestPath string
// PhysicalPath is path of static file
PhysicalPath string
// RouteData
RouteData RouteData
// ViewData
ViewData map[string]interface{}
// Result
Result HttpResult
// Error
Error error
// Flash is flash variables of request life cycle
Flash map[string]interface{}
// Session
Session Session
// SessionIsNew is true if create session in this request
SessionIsNew bool
}
// String
func (ctx *HttpContext) String() string {
return fmt.Sprintf("%s %s %v %v \n;", ctx.Method, ctx.RequestPath, ctx.Result, ctx.Error)
}
// RouteValue return route value by name
func (ctx *HttpContext) RouteValue(name string) (string, bool) {
v, ok := ctx.RouteData[name]
return v, ok
}
// FV is alias of FormValue
func (ctx *HttpContext) FV(name string) string {
return ctx.Request.FormValue(name)
}
// FormValue is alias of Request FormValue
func (ctx *HttpContext) FormValue(name string) string {
return ctx.Request.FormValue(name)
}
// FormInt parse form value as int
func (ctx *HttpContext) FormInt(name string) (int, bool) {
if s := ctx.FormValue(name); s != "" {
if i, err := strconv.Atoi(s); err == nil {
return i, true
}
}
return 0, false
}
// FormIntOr return form value as int or v
func (ctx *HttpContext) FormIntOr(name string, v int) int {
if s := ctx.FormValue(name); s != "" {
if i, err := strconv.Atoi(s); err == nil {
return i
}
}
return v
}
// FormBool parse form value as bool
func (ctx *HttpContext) FormBool(name string) (bool, bool) {
if s := ctx.FormValue(name); s != "" {
if b, err := strconv.ParseBool(s); err == nil {
return b, true
}
}
return false, false
}
// FormBoolOr return form value as bool or v
func (ctx *HttpContext) FormBoolOr(name string, v bool) bool {
if s := ctx.FormValue(name); s != "" {
if b, err := strconv.ParseBool(s); err == nil {
return b
}
}
return v
}
// FormFloat parse form value as float64
func (ctx *HttpContext) FormFloat(name string) (float64, bool) {
if s := ctx.FormValue(name); s != "" {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f, true
}
}
return 0, false
}
// FormFloatOr return form value as float64 or v
func (ctx *HttpContext) FormFloatOr(name string, v float64) float64 {
if s := ctx.FormValue(name); s != "" {
if f, err := strconv.ParseFloat(s, 64); err == nil {
return f
}
}
return v
}
// // QueryValue return value from request URL query
// func (ctx *HttpContext) QueryValue(name string) []string {
// return ctx.Request.URL.Query()[name]
// }
// ReqHeader return request header by name
func (ctx *HttpContext) ReqHeader(name string) string {
return ctx.Request.Header.Get(name)
}
// ResHeader return response header by name
func (ctx *HttpContext) ResHeader(name string) string {
return ctx.Resonse.Header().Get(name)
}
// SetHeader set resonse http header
func (ctx *HttpContext) SetHeader(key string, value string) {
ctx.Resonse.Header().Set(key, value)
}
// AddHeader add response http header
func (ctx *HttpContext) AddHeader(key string, value string) {
ctx.Resonse.Header().Add(key, value)
}
// ContentType set response header Content-Type
func (ctx *HttpContext) ContentType(ctype string) {
ctx.Resonse.Header().Set(HeaderContentType, ctype)
}
// Status write status code to http header
func (ctx *HttpContext) Status(code int) {
ctx.Resonse.WriteHeader(code)
}
// Accept return request header Accept
func (ctx *HttpContext) Accept() string {
return ctx.Request.Header.Get(HeaderAccept)
}
// Write writes b to resposne
func (ctx *HttpContext) Write(b []byte) (int, error) {
return ctx.Resonse.Write(b)
}
// Expires set reponse header Expires
func (ctx *HttpContext) Expires(t string) {
ctx.SetHeader(HeaderExpires, t)
}
// SetCookie set cookie to response
func (ctx *HttpContext) SetCookie(cookie *http.Cookie) {
http.SetCookie(ctx.Resonse, cookie)
}
// Cookie return cookie from request
func (ctx *HttpContext) Cookie(name string) (*http.Cookie, error) {
return ctx.Request.Cookie(name)
}
// SessionId return sessionid of current context
func (ctx *HttpContext) SessionId() string {
return string(ctx.Session)
}
// Flush flush response immediately
func (ctx *HttpContext) Flush() {
// _, buf, _ := ctx.Resonse.(http.Hijacker).Hijack()
// if buf != nil {
// buf.Flush()
// }
f, ok := ctx.Resonse.(http.Flusher)
if ok && f != nil {
f.Flush()
}
}
// GetFlash return value in Context.Flash
func (ctx *HttpContext) GetFlash(key string) (v interface{}, ok bool) {
if ctx.Flash == nil {
return nil, false
}
v, ok = ctx.Flash[key]
return
}
// SetFlash set value to Context.Flash
func (ctx *HttpContext) SetFlash(key string, v interface{}) {
if ctx.Flash == nil {
ctx.Flash = make(map[string]interface{})
}
ctx.Flash[key] = v
}
// ReadBody read Request.Body
func (ctx *HttpContext) ReadBody() ([]byte, error) {
return ioutil.ReadAll(ctx.Request.Body)
}