-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctx.go
232 lines (194 loc) · 4.85 KB
/
ctx.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
package ela
import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
type Locale interface {
Language() string
Tr(string) string
}
// RequestContext
type Context struct {
w ResponseWriter
r *http.Request
Data map[string]interface{}
status int
headerMap map[string]string
uriParams map[string]string
Locale
}
func (ctx *Context) GetResponseWriter() ResponseWriter {
return ctx.w
}
func (ctx *Context) GetRequest() *http.Request {
return ctx.r
}
func (ctx *Context) GetMethod() string {
return ctx.r.Method
}
func (ctx *Context) SetStatus(status int) {
ctx.status = status
ctx.w.SetStatus(status)
}
func (ctx *Context) GetStatus() int {
return ctx.status
}
func (ctx *Context) SetHeader(key, value string) {
if ctx.headerMap == nil {
ctx.headerMap = make(map[string]string)
}
ctx.headerMap[key] = value
}
// url redirection
func (ctx *Context) Redirect(url string) {
http.Redirect(ctx.w, ctx.r, url, 302)
}
// get cookie
func (ctx *Context) GetCookie(key string) string {
cookie, err := ctx.r.Cookie(key)
if err != nil {
return ""
}
val, _ := url.QueryUnescape(cookie.Value)
return val
}
// set cookie
// args: name, value, max age, path, domain, secure, http only, expires
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
cookie := http.Cookie{}
cookie.Name = name
cookie.Value = url.QueryEscape(value)
if len(others) > 0 {
switch v := others[0].(type) {
case int:
cookie.MaxAge = v
case int64:
cookie.MaxAge = int(v)
case int32:
cookie.MaxAge = int(v)
}
}
cookie.Path = "/"
if len(others) > 1 {
if v, ok := others[1].(string); ok && len(v) > 0 {
cookie.Path = v
}
}
if len(others) > 2 {
if v, ok := others[2].(string); ok && len(v) > 0 {
cookie.Domain = v
}
}
if len(others) > 3 {
switch v := others[3].(type) {
case bool:
cookie.Secure = v
default:
if others[3] != nil {
cookie.Secure = true
}
}
}
if len(others) > 4 {
if v, ok := others[4].(bool); ok && v {
cookie.HttpOnly = true
}
}
if len(others) > 5 {
if v, ok := others[5].(time.Time); ok {
cookie.Expires = v
cookie.RawExpires = v.Format(time.UnixDate)
}
}
ctx.w.Header().Add("Set-Cookie", cookie.String())
}
// write and flush response content
func (ctx *Context) Write(content string) (int, error) {
header := ctx.w.Header()
for k, v := range ctx.headerMap {
header.Set(k, v)
// ctx.r.Header.Set(k, v)
}
ctx.w.WriteHeader(ctx.status)
return ctx.w.Write([]byte(content))
}
func (ctx *Context) serveTemplateWithStatus(templateFile string, status int, useDefaultError bool) {
ctx.SetHeader("Content-Type", "text/html")
// if in debug mode, reload templates
if config.GetStringDefault("_", "mode", "dev") == "dev" {
reloadTemplate()
}
t, err := parseFiles(templatesName...)
if err != nil {
content := "<h2>Server Internal Error!</h2>\n\n" + fmt.Sprintln(err)
servError(ctx, content, 500, useDefaultError)
} else {
if status == 404 {
ctx.w.WriteHeader(404)
} else if status == 500 {
ctx.w.WriteHeader(500)
}
err = t.ExecuteTemplate(ctx.w, templateFile, ctx.Data)
if err != nil {
content := "<h2>Server Internal Error!</h2>\n\n" + fmt.Sprintf("<pre>%s</pre>", fmtErrorHtml(err.Error()))
servError(ctx, content, 500, useDefaultError)
}
}
}
// serve and parse a template
func (ctx *Context) ServeTemplate(templateFile string) {
ctx.serveTemplateWithStatus(templateFile, 200, false)
}
// serve and parse a template for an error, used for error controller
func (ctx *Context) ServeError(status int, templateFile string) {
ctx.serveTemplateWithStatus(templateFile, status, true)
}
func (ctx *Context) setURIParam(params map[string]string) {
ctx.uriParams = params
}
// get uri params when defined router with uri params mode
func (ctx *Context) GetURIParam(key string) (string, error) {
if ctx.uriParams == nil {
return "", fmt.Errorf("%s", "does not exist uri params")
} else {
return ctx.uriParams[key], nil
}
}
// 10M memory for multipart form parsing
var MaxMemory = int64(1024 * 1024 * 10)
func (ctx *Context) parseForm() {
if ctx.r.Form != nil {
return
}
contentType := ctx.r.Header.Get("Content-Type")
if (ctx.r.Method == "POST" || ctx.r.Method == "PUT") &&
len(contentType) > 0 && strings.Contains(contentType, "multipart/form-data") {
ctx.r.ParseMultipartForm(MaxMemory)
} else {
ctx.r.ParseForm()
}
}
func (ctx *Context) GetParam(name string) string {
ctx.parseForm()
return ctx.r.Form.Get(name)
}
// get uri params with default value
func (ctx *Context) GetURIParamDefault(key string, defaultValue string) string {
value, err := ctx.GetURIParam(key)
if err != nil {
return defaultValue
} else {
return value
}
}
func newContext(w http.ResponseWriter, r *http.Request) Context {
ctx := Context{}
ctx.w = NewResponseWriter(w)
ctx.r = r
ctx.Data = make(map[string]interface{})
ctx.SetStatus(200)
return ctx
}