-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinterceptors.go
More file actions
83 lines (76 loc) · 1.75 KB
/
interceptors.go
File metadata and controls
83 lines (76 loc) · 1.75 KB
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
package golax
import (
"encoding/json"
"log"
)
// InterceptorError prints an error in JSON format if Context.LastError is
// not nil.
// Example:
// {
// "status_code": 404,
// "error_code": 1000023,
// "description_code": "User 'fulanez' not found.",
// }
var InterceptorError = &Interceptor{
Documentation: Doc{
Name: "Error",
Description: `
Print JSON error in this form:
´´´json
{
"status_code": 404,
"error_code": 21,
"description_code": "User '231223' not found."
}
´´´
`,
},
After: func(c *Context) {
if nil != c.LastError {
json.NewEncoder(c.Response).Encode(c.LastError)
}
},
}
// InterceptorLog prints an access log to standard output.
// Example:
// 2016/02/20 11:09:17 GET /favicon.ico 404 59B
// 2016/02/20 11:09:34 GET /service/v1/ 405 68B
// 2016/02/20 11:09:46 GET /service/v1/doc 405 68B
var InterceptorLog = &Interceptor{
Documentation: Doc{
Name: "Log",
Description: `
Log all HTTP requests to stdout in this form:
´´´
2016/02/20 11:09:17 GET /favicon.ico 404 59B
2016/02/20 11:09:34 GET /service/v1/ 405 68B
2016/02/20 11:09:46 GET /service/v1/doc 405 68B
´´´
`,
},
After: func(c *Context) {
log.Printf(
"%s\t%s\t%d\t%dB",
c.Request.Method,
c.Request.URL.RequestURI(),
c.Response.StatusCode,
c.Response.Length,
)
},
}
// InterceptorNoCache set some headers to force response to not be cached
// by user agent.
var InterceptorNoCache = &Interceptor{
Documentation: Doc{
Name: "InterceptorNoCache",
Description: `
Avoid caching via http headers
`,
},
Before: func(c *Context) {
add := c.Response.Header().Add
add("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0")
add("Pragma", "no-cache")
add("Expires", "0")
},
}