-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontext.go
341 lines (297 loc) Β· 9.57 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package app
import (
"bufio"
"context"
"encoding/json"
"fmt"
"html/template"
"log"
"net"
"net/http"
"golang.org/x/text/language"
"github.com/gowww/check"
"github.com/gowww/fatal"
"github.com/gowww/i18n"
"github.com/gowww/router"
)
// A Context contains the data for a handler.
type Context struct {
Res http.ResponseWriter
Req *http.Request
}
// contextHandle wraps the router for setting headers and deferring their write.
func contextHandle(h http.Handler) http.Handler {
return Handler(func(c *Context) {
cw := &contextWriter{ResponseWriter: c.Res}
defer func() {
if cw.status != 0 {
c.Res.WriteHeader(cw.status)
}
}()
cw.Header().Set("Cache-Control", "no-cache")
cw.Header().Set("Connection", "keep-alive")
h.ServeHTTP(cw, c.Req)
})
}
// logWriter keeps the status code from WriteHeader to allow setting headers after a Context.Status call.
// Required when using Context.Status with Context.JSON, for example.
type contextWriter struct {
http.ResponseWriter
status int
}
func (cw *contextWriter) WriteHeader(status int) {
cw.status = status
}
func (cw *contextWriter) Write(b []byte) (int, error) {
if cw.status != 0 {
cw.ResponseWriter.WriteHeader(cw.status)
cw.status = 0
}
return cw.ResponseWriter.Write(b)
}
// CloseNotify implements the http.CloseNotifier interface.
// No channel is returned if CloseNotify is not implemented by an upstream response writer.
func (cw *contextWriter) CloseNotify() <-chan bool {
n, ok := cw.ResponseWriter.(http.CloseNotifier)
if !ok {
return nil
}
return n.CloseNotify()
}
// Flush implements the http.Flusher interface.
// Nothing is done if Flush is not implemented by an upstream response writer.
func (cw *contextWriter) Flush() {
f, ok := cw.ResponseWriter.(http.Flusher)
if ok {
f.Flush()
}
}
// Hijack implements the http.Hijacker interface.
// Error http.ErrNotSupported is returned if Hijack is not implemented by an upstream response writer.
func (cw *contextWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := cw.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, http.ErrNotSupported
}
return h.Hijack()
}
// Push implements the http.Pusher interface.
// http.ErrNotSupported is returned if Push is not implemented by an upstream response writer or not supported by the client.
func (cw *contextWriter) Push(target string, opts *http.PushOptions) error {
p, ok := cw.ResponseWriter.(http.Pusher)
if !ok {
return http.ErrNotSupported
}
return p.Push(target, opts)
}
// Get returns a context value.
func (c *Context) Get(key interface{}) interface{} {
return c.Req.Context().Value(key)
}
// Set sets a context value.
func (c *Context) Set(key, val interface{}) {
c.Req = c.Req.WithContext(context.WithValue(c.Req.Context(), key, val))
}
// PathValue returns the value of path parameter.
func (c *Context) PathValue(key string) string {
return router.Parameter(c.Req, key)
}
// FormValue gets the form value from the request.
func (c *Context) FormValue(key string) string {
return c.Req.FormValue(key)
}
// HasFormValue checks if the form value exists in the request.
func (c *Context) HasFormValue(key string) bool {
return c.Req.FormValue(key) != ""
}
// Write writes the response.
func (c *Context) Write(b []byte) (int, error) {
return c.Res.Write(b)
}
// Text writes the response with a string.
func (c *Context) Text(s string) {
c.Write([]byte(s))
}
// Textf writes the response with a formatted string.
func (c *Context) Textf(s string, a ...interface{}) {
fmt.Fprintf(c.Res, s, a...)
}
// Bytes writes the response with a bytes slice.
func (c *Context) Bytes(b []byte) {
c.Write(b)
}
// Status sets the HTTP status of the response.
func (c *Context) Status(code int) *Context {
c.Res.WriteHeader(code)
return c
}
// View writes the response with a rendered view.
// This data is always part of the rendering:
// . the GlobalViewData
// .c the Context
// .errors the translated errors map
func (c *Context) View(name string, data ...ViewData) {
mdata := mergeViewData(data)
mdata["c"] = c
switch errs := mdata["errors"].(type) {
case check.TranslatedErrors:
break
case check.Errors:
mdata["errors"] = c.TErrors(errs)
default:
mdata["errors"] = make(check.TranslatedErrors)
}
err := views.ExecuteTemplate(c, name, mdata)
if err != nil {
c.Panic(err)
}
}
// JSON writes the response with a marshalled JSON.
// If v has a JSON() interface{} method, it will be used.
func (c *Context) JSON(v interface{}) {
c.Res.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(c.Res)
if vjson, ok := v.(interface {
JSON() interface{}
}); ok {
if err := enc.Encode(vjson.JSON()); err != nil {
c.Panic(err)
}
return
}
if err := enc.Encode(v); err != nil {
c.Panic(err)
}
}
// Check uses a check.Checker to validate request's data and always returns the non-nil errors map.
func (c *Context) Check(checker check.Checker) check.Errors {
return checker.CheckRequest(c.Req)
}
// TErrors returns translated checking errors.
func (c *Context) TErrors(errs check.Errors) check.TranslatedErrors {
return errs.T(i18n.RequestTranslator(c.Req))
}
// BadRequest uses a check.Checker to validate request form data, and a view name to execute on fail.
// If you don't provide a view name (empty string), the response will be a JSON errors map.
//
// If the check fails, it sets the status to "400 Bad Request" and returns true, allowing you to exit from the handler.
func (c *Context) BadRequest(checker check.Checker, view string, data ...ViewData) bool {
errs := c.Check(checker)
if errs.Empty() {
return false
}
c.Status(http.StatusBadRequest)
if view == "" {
c.JSON(errs)
} else {
data = append(data, ViewData{"errors": errs})
c.View(view, data...)
}
return true
}
// Redirect redirects the client to the url with status code.
func (c *Context) Redirect(url string, status int) {
http.Redirect(c.Res, c.Req, url, status)
}
// Cookie returns the value of the named cookie.
// If multiple cookies match the given name, only one cookie value will be returned.
// If the secret key is set for app, value will be decrypted before returning.
// If cookie is not found or the decryption fails, an empty string is returned.
func (c *Context) Cookie(name string) string {
ck, _ := c.Req.Cookie(name)
if ck == nil {
return ""
}
if encrypter == nil {
return ck.Value
}
v, err := encrypter.DecryptBase64([]byte(ck.Value))
if err != nil {
c.DeleteCookie(name)
return ""
}
return string(v)
}
// SetCookie sets a cookie to the response.
// If the secret key is set for app, value will be encrypted.
// If the app is not in a production environment, the "secure" flag will be set to false.
func (c *Context) SetCookie(cookie *http.Cookie) {
if !production {
cookie.Secure = false
}
if encrypter != nil {
v, err := encrypter.EncryptBase64([]byte(cookie.Value))
if err != nil {
c.Panic(err)
}
cookie.Value = string(v)
}
http.SetCookie(c.Res, cookie)
}
// DeleteCookie removes a cookie from the client.
func (c *Context) DeleteCookie(name string) {
http.SetCookie(c.Res, &http.Cookie{Name: name, MaxAge: -1})
}
// translator returns the request translator.
func (c *Context) translator() *i18n.Translator {
rt := i18n.RequestTranslator(c.Req)
if rt == nil {
panic("app: no locales, no translator set")
}
return rt
}
// Locale returns the locale used for the client.
func (c *Context) Locale() language.Tag {
return c.translator().Locale()
}
// T returns the translation associated to key, for the client locale.
func (c *Context) T(key string, a ...interface{}) string {
return c.translator().T(key, a...)
}
// Tn returns the translation associated to key, for the client locale.
// If the translation defines plural forms (zero, one, other), it uses the most appropriate.
// All i18n.TnPlaceholder in the translation are replaced with number n.
// If translation is not found, an empty string is returned.
func (c *Context) Tn(key string, n int, args ...interface{}) string {
return c.translator().Tn(key, n, args...)
}
// THTML works like T but returns an HTML unescaped translation. An "nl2br" function is applied to the result.
func (c *Context) THTML(key string, a ...interface{}) template.HTML {
return c.translator().THTML(key, a...)
}
// TnHTML works like Tn but returns an HTML unescaped translation. An "nl2br" function is applied to the result.
func (c *Context) TnHTML(key string, n int, args ...interface{}) template.HTML {
return c.translator().TnHTML(key, n, args...)
}
// FmtNumber returns a formatted number with decimal and thousands marks.
func (c *Context) FmtNumber(n interface{}) string {
return i18n.FmtNumber(c.translator().Locale(), n)
}
// TODO: HTML link helper with i18n. See https://support.google.com/webmasters/answer/189077?hl=en.
// Push initiates an HTTP/2 server push if supported.
// See net/http.Pusher for documentation.
func (c *Context) Push(target string, opts *http.PushOptions) {
if pusher, ok := c.Res.(http.Pusher); ok {
pusher.Push(target, opts)
}
}
// NotFound responds with the "not found" handler.
func (c *Context) NotFound() {
if rt.NotFoundHandler != nil {
rt.NotFoundHandler.ServeHTTP(c.Res, c.Req)
} else {
http.NotFound(c.Res, c.Req)
}
}
// Log logs the message with the client address.
func (c *Context) Log(msg string) {
log.Printf("Serving %s: %s", c.Req.RemoteAddr, msg)
}
// Panic logs error with stack trace and responds with the error handler if set.
func (c *Context) Panic(err error) {
panic(fmt.Errorf("Failed serving %s: %v", c.Req.RemoteAddr, err))
}
// Error returns the error value stored in request's context after a recovering or a Context.Error call.
func (c *Context) Error() error {
return fmt.Errorf("%v", fatal.Error(c.Req))
}