-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
251 lines (213 loc) Β· 5.11 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
package gow
import (
"net/http"
"strconv"
"net/url"
"strings"
"path"
)
type (
Context interface {
Base() string
Header(key string, value ...string) string
Host() string
Ip() string
Url() string
Uri() string
Ext() string
Method() string
UserAgent() string
Referer() string
IsSSL() bool
IsAjax() bool
Request() *http.Request
Response() *Response
Text(text string)
Param(key string) string
QueryParam(key string) string
Input() map[string]string
String(key string) string
Strings(key string) []string
Flash(key string, v ...interface{}) interface{}
SetBody(body []byte)
Status(code int)
Json(data interface{})
ContentType(contentType string)
Render(tpl string, model map[string]interface{})
Tpl(tpl string, data map[string]interface{}) string
}
context struct {
request *http.Request
response *Response
path string
pnames []string
pvalues []string
query url.Values
gow *Gow
flashData map[string]interface{}
}
)
// Flash sets values to this context or gets by key string.
// The flash items are alive in this context only.
func (ctx *context) Flash(key string, v ...interface{}) interface{} {
if len(v) > 0 {
return ctx.flashData[key]
}
ctx.flashData[key] = v[0]
return nil
}
// Input returns all input data map.
func (ctx *context) Input() map[string]string {
data := make(map[string]string)
for key, v := range ctx.request.Form {
data[key] = v[0]
}
return data
}
// Strings returns string slice of given key.
func (ctx *context) Strings(key string) []string {
return ctx.request.Form[key]
}
// String returns input value of given key.
func (ctx *context) String(key string) string {
return ctx.request.FormValue(key)
}
// StringOr returns input value of given key instead of def string if empty.
func (ctx *context) StringOr(key string, def string) string {
value := ctx.String(key)
if value == "" {
return def
}
return value
}
// Int returns input value of given key.
func (ctx *context) Int(key string) int {
str := ctx.String(key)
i, _ := strconv.Atoi(str)
return i
}
// IntOr returns input value of given key instead of def int if empty.
func (ctx *context) IntOr(key string, def int) int {
i := ctx.Int(key)
if i == 0 {
return def
}
return i
}
// Float returns input value of given key.
func (ctx *context) Float(key string) float64 {
str := ctx.String(key)
f, _ := strconv.ParseFloat(str, 64)
return f
}
// FloatOr returns input value of given key instead of def float if empty.
func (ctx *context) FloatOr(key string, def float64) float64 {
f := ctx.Float(key)
if f == 0.0 {
return def
}
return f
}
// Bool returns input value of given key.
func (ctx *context) Bool(key string) bool {
str := ctx.String(key)
b, _ := strconv.ParseBool(str)
return b
}
// ContentType sets content-type string.
func (ctx *context) ContentType(contentType string) {
ctx.response.ContentType(contentType)
}
func (ctx *context) Tpl(tpl string, data map[string]interface{}) string {
//b, e := ctx.gow.TplEngine.Render(tpl+".html", data)
//if e != nil {
// panic(e)
//}
//return string(b)
return ""
}
func (ctx *context) Render(tpl string, model map[string]interface{}) {
b, e := _gow.TplEngine.Render(tpl, model)
if e != nil {
panic(e)
}
ctx.ContentType("text/html; charset=utf-8")
ctx.response.Body = b
}
// Gow returns *Gow instance in this context.
func (ctx *context) Gow() *Gow {
return ctx.gow
}
// Gow returns *Gow instance in this context.
func (ctx *context) Text(text string) {
ctx.response.Text(text)
}
func (ctx *context) Json(data interface{}) {
ctx.response.Json(data)
}
func (ctx *context) Request() *http.Request {
return ctx.request
}
func (ctx *context) Response() *Response {
return ctx.response
}
func (ctx *context) IsAjax() bool {
return ctx.request.Header.Get("X-Requested-With") == "XMLHttpRequest"
}
func (ctx *context) IsSSL() bool {
return ctx.request.TLS != nil
}
func (ctx *context) Ip() string {
return strings.Split(ctx.request.RemoteAddr, ":")[0]
}
func (ctx *context) Referer() string {
return ctx.request.Referer()
}
func (ctx *context) UserAgent() string {
return ctx.request.UserAgent()
}
func (ctx *context) Method() string {
return ctx.request.Method
}
func (ctx *context) Url() string {
return ctx.request.URL.String()
}
func (ctx *context) Uri() string {
return ctx.request.RequestURI
}
func (ctx *context) Ext() string {
return path.Ext(ctx.request.URL.Path)
}
func (ctx *context) Host() string {
return ctx.request.Host
}
func (ctx *context) Header(key string, value ...string) string {
if len(value) == 0 {
return ctx.request.Header.Get(key)
}
if len(value) == 1 {
ctx.response.Headers[key] = value[0]
}
return ""
}
func (ctx *context) Base() string {
baseUrl := "://" + ctx.Host() + "/"
if ctx.IsSSL() {
baseUrl = "https" + baseUrl
} else {
baseUrl = "http" + baseUrl
}
return baseUrl
}
func (ctx *context) Param(key string) string {
return ""
}
func (ctx *context) QueryParam(key string) string {
return ctx.request.Form.Get(key)
}
func (ctx *context) SetBody(body []byte) {
ctx.response.Body = body
}
func (ctx *context) Status(code int) {
ctx.response.Status = code
}