-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
131 lines (107 loc) · 2.7 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
package flex
import (
"encoding/json"
"net/http"
"net/url"
"github.com/lujin123/flex/binding"
)
type Context struct {
Req *http.Request
Resp *Response
flex *Flex
params Param
}
func (ctx *Context) Path() string {
path := ctx.Req.URL.RawPath
if path == "" {
path = ctx.Req.URL.Path
}
return path
}
func (ctx *Context) Method() string {
return ctx.Req.Method
}
func (ctx *Context) ContentType() string {
return filterHeader(ctx.reqHeader(HeaderContentType))
}
func (ctx *Context) Params() Param {
return ctx.params
}
func (ctx *Context) Param(key string) string {
v, ok := ctx.params[key]
if ok {
return string(v)
}
return ""
}
func (ctx *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool) {
if path == "" {
path = "/"
}
http.SetCookie(ctx.Resp, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
Secure: secure,
HttpOnly: httpOnly,
})
}
func (ctx *Context) Write(code int, b []byte) error {
ctx.Resp.WriteHeader(code)
_, err := ctx.Resp.Write(b)
return err
}
func (ctx *Context) JSON(code int, data interface{}) error {
ctx.writeContentType(MIMEApplicationJSONCharsetUTF8)
ctx.Resp.WriteHeader(code)
return json.NewEncoder(ctx.Resp).Encode(data)
}
///////////////////////////binding request methods///////////////////////////
func (ctx *Context) ShouldBind(ptr interface{}) error {
return ctx.ShouldBindWith(ptr, ctx.binding())
}
func (ctx *Context) ShouldBindWith(ptr interface{}, b binding.Binding) error {
return b.Bind(ctx.Req, ptr)
}
func (ctx *Context) ShouldBindJSON(ptr interface{}) error {
return ctx.ShouldBindWith(ptr, binding.JSON)
}
func (ctx *Context) ShouldBindQuery(ptr interface{}) error {
return ctx.ShouldBindWith(ptr, binding.Query)
}
func (ctx *Context) ShouldBindXML(ptr interface{}) error {
return ctx.ShouldBindWith(ptr, binding.XML)
}
//////////////////////////private methods////////////////////////////
func (ctx *Context) writeContentType(value string) {
header := ctx.Resp.Header()
header.Set(HeaderContentType, value)
}
func (ctx *Context) reqHeader(key string) string {
return ctx.Req.Header.Get(key)
}
func (ctx *Context) reset(r *http.Request, w http.ResponseWriter) {
ctx.Req = r
ctx.Resp.reset(w)
ctx.params = nil
}
func (ctx *Context) binding() binding.Binding {
if ctx.Method() == http.MethodGet {
return binding.Form
}
contentType := ctx.ContentType()
switch contentType {
case MIMEApplicationJSON:
return binding.JSON
case MIMEApplicationXML, MIMETextXML:
return binding.XML
case MIMEApplicationForm:
return binding.FormPost
case MIMEMultipartForm:
return binding.FormMultipart
default:
return binding.Form
}
}