forked from restgo/restgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
77 lines (63 loc) · 1.94 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
package restgo
import (
"github.com/fasthttp-contrib/render"
"github.com/valyala/fasthttp"
"sync"
)
type Context struct {
*fasthttp.RequestCtx
*render.Render
}
type JSON map[string]interface{}
func contextPool() sync.Pool {
return sync.Pool{
New: func() interface{} {
return &Context{}
},
}
}
// Get path parameter or query parameter as int. if not exist, default value will be returned
func (this *Context) ParamInt(key string, defVal int) int {
val, err := this.URI().QueryArgs().GetUint(key)
if err != nil {
return defVal
}
return val
}
// Get path parameter or query parameter as float64. if not exist, default value will be returned
func (this *Context) ParamFloat(key string, defVal float64) float64 {
val, err := this.URI().QueryArgs().GetUfloat(key)
if err != nil {
return defVal
}
return val
}
// Get path parameter or query parameter as string. if not exist, default value will be returned
func (this *Context) ParamString(key string, defVal string) string {
val := this.URI().QueryArgs().Peek(key)
if val == nil {
return defVal
}
return string(val)
}
func (this *Context) ServeJSON(status int, v interface{}) error {
return this.JSON(this.RequestCtx, status, v)
}
func (this *Context) ServeJSONP(status int, callback string, v interface{}) error {
return this.JSONP(this.RequestCtx, status, callback, v)
}
func (this *Context) ServeText(status int, v string) error {
return this.Text(this.RequestCtx, status, v)
}
func (this *Context) ServeXML(status int, v interface{}) error {
return this.XML(this.RequestCtx, status, v)
}
func (this *Context) ServeHTML(status int, name string, binding interface{}, layout ...string) error {
return this.HTML(this.RequestCtx, status, name, binding, layout...)
}
func (this *Context) ServeData(status int, v []byte) error {
return this.Data(this.RequestCtx, status, v)
}
func (this *Context) ServeError(status int, err error) error{
return this.Text(this.RequestCtx, status, err.Error())
}