-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
246 lines (214 loc) · 6.53 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
package dipra
import (
"encoding/json"
"encoding/xml"
"net/http"
"os"
"reflect"
"strings"
)
type (
Header string
MimeType string
AccessControll string
Context struct {
http.ResponseWriter
*http.Request
params *params
Binding
engine *Engine
Writen ResponseWriter
}
)
const (
HeaderAccept Header = "Accept"
HeaderAcceptEncoding Header = "Accept-Encoding"
HeaderAllow Header = "Allow"
HeaderAuthorization Header = "Authorization"
HeaderContentDisposition Header = "Content-Disposition"
HeaderContentEncoding Header = "Content-Encoding"
HeaderContentLength Header = "Content-Length"
HeaderContentType Header = "Content-Type"
HeaderCookie Header = "Cookie"
HeaderSetCookie Header = "Set-Cookie"
HeaderIfModifiedSince Header = "If-Modified-Since"
HeaderLastModified Header = "Last-Modified"
HeaderLocation Header = "Location"
HeaderUpgrade Header = "Upgrade"
HeaderVary Header = "Vary"
HeaderWWWAuthenticate Header = "WWW-Authenticate"
HeaderXForwardedFor Header = "X-Forwarded-For"
HeaderXForwardedProto Header = "X-Forwarded-Proto"
HeaderXForwardedProtocol Header = "X-Forwarded-Protocol"
HeaderXForwardedSsl Header = "X-Forwarded-Ssl"
HeaderXUrlScheme Header = "X-Url-Scheme"
HeaderXHTTPMethodOverride Header = "X-HTTP-Method-Override"
HeaderXRealIP Header = "X-Real-IP"
HeaderXRequestID Header = "X-Request-ID"
HeaderXRequestedWith Header = "X-Requested-With"
HeaderServer Header = "Server"
HeaderOrigin Header = "Origin"
HeaderStatus Header = "Status"
MIMEApplicationJSON MimeType = "application/json"
MIMEApplicationJavaScript MimeType = "application/javascript"
MIMEApplicationXML MimeType = "application/xml"
MIMETextXML MimeType = "text/xml"
MIMEApplicationForm MimeType = "application/x-www-form-urlencoded"
MIMEApplicationProtobuf MimeType = "application/protobuf"
MIMEApplicationMsgpack MimeType = "application/msgpack"
MIMETextHTML MimeType = "text/html"
MIMEApplicationYAML MimeType = "application/x-yaml"
MIMETextYAML MimeType = "text/yaml"
MIMETextPlain MimeType = "text/plain"
MIMEMultipartForm MimeType = "multipart/form-data"
MIMEOctetStream MimeType = "application/octet-stream"
AccessControllOrigin AccessControll = "Access-Control-Allow-Origin"
ACcessControllMethod AccessControll = "Access-Control-Allow-Methods"
ACcessControllHeaders AccessControll = "Access-Control-Allow-Headers"
AccessControllCredential AccessControll = "Access-Control-Allow-Credentials"
AccessControllMaxAge AccessControll = "Access-Control-Max-Age"
AccessControllExposeHeaders AccessControll = "Access-Control-Expose-Headers"
AccessControllReqMethod AccessControll = "Access-Control-Request-Method"
AccessControllReqHeaders AccessControll = "Access-Control-Request-Headers"
)
// Reset Context and response
func (c *Context) Reset(w http.ResponseWriter, r *http.Request) {
c.Writen.Reset(w)
c.ResponseWriter = w
c.Request = r
c.SetBind(r)
}
// Query by Key ?=key=value
func (c *Context) Query(param string) string {
return c.getQuery(param, "")
}
// // Param by wlidcard /:id
func (c *Context) Param(param string) string {
return c.params.getParam(param)
}
// GetQuery By param
func (c *Context) getQuery(param string, DefaultQuery string) string {
q := c.URL.Query()
paramValue := q.Get(param)
if len(paramValue) == 0 {
if len(DefaultQuery) == 0 {
return ""
}
return DefaultQuery
}
return paramValue
}
// JSON response
func (c *Context) JSON(code int, body interface{}) error {
out, err := json.Marshal(body)
if err != nil {
defaulterrorHTTP(c.ResponseWriter, http.StatusInternalServerError, err)
}
p := map[string]string{
string(HeaderContentType): string(MIMEApplicationJSON),
}
c.Writen.WriteHeader(p)
c.Writen.WriteStatus(code)
c.Writen.Write(out)
return nil
}
// JSONP Response
func (c *Context) JSONP(code int, body interface{}) error {
out, err := json.Marshal(body)
if err != nil {
defaulterrorHTTP(c.ResponseWriter, http.StatusInternalServerError, err)
}
p := map[string]string{
string(HeaderContentType): string(MIMEApplicationJavaScript),
}
c.Writen.WriteHeader(p)
c.Writen.WriteStatus(code)
c.Writen.Write(out)
return nil
}
// String response
func (c *Context) String(code int, body string) (err error) {
if reflect.ValueOf(body).Kind() != reflect.String {
return http.ErrNotSupported
}
p := map[string]string{
string(HeaderContentType): string(MIMETextPlain),
}
c.Writen.WriteHeader(p)
c.Writen.WriteStatus(code)
c.Writen.Write([]byte(body))
return err
}
// XML response
func (c *Context) XML(code int, body interface{}) (err error) {
xml, err := xml.MarshalIndent(body, "", "")
if err != nil {
return err
}
p := map[string]string{
string(HeaderContentType): string(MIMEApplicationXML),
}
c.Writen.WriteHeader(p)
c.Writen.WriteStatus(code)
c.Writen.Write(xml)
return err
}
// SetCookie by input
func (c *Context) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.GetResponse(), cookie)
}
// GetCookies all
func (c *Context) GetCookies() []*http.Cookie {
return c.Cookies()
}
// GetCookie is get cookie by name
func (c *Context) GetCookie(name string) (*http.Cookie, error) {
return c.Cookie(name)
}
// Redirect http url
func (c *Context) Redirect(url string, code int) {
http.Redirect(c.GetResponse(), c.GetRequest(), url, code)
}
// File is used get file type
func (c *Context) File(path string) (err error) {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
serveFile := func(path string) {
http.ServeFile(c.GetResponse(), c.GetRequest(), path)
}
s, _ := f.Stat()
if s.IsDir() {
index := strings.TrimSuffix(path, "/") + path
if _, err = os.Open(index); err != nil {
return
}
}
serveFile(path)
return
}
// GetResponse by Reset() or another set http with returns http.ResponseWriter
func (c *Context) GetResponse() http.ResponseWriter {
return c.ResponseWriter
}
// GetRequest returns *http.request
func (c *Context) GetRequest() *http.Request {
return c.Request
}
// SetError for Get Error
func (c *Context) SetError(err error) {
c.engine.HandlerError(err, c)
}
// GetHeader for get value header
func (c *Context) GetHeader(key string) interface{} {
switch key {
case "*":
return c.GetRequest().Header
case "":
return ""
default:
return c.GetRequest().Header.Get(key)
}
}