@@ -40,11 +40,22 @@ type ResourceHandler struct {
40
40
// Note: If a charset parameter exists, it MUST be UTF-8
41
41
EnableRelaxedContentType bool
42
42
43
+ // Optional global middlewares that can be used to wrap the all REST endpoints.
44
+ // They are used in the defined order, the first wrapping the second, ...
45
+ // They are run first, wrapping all go-json-rest middlewares,
46
+ // * request.PathParams is not set yet
47
+ // * "panic" won't be caught and converted to 500
48
+ // * request.Env["STATUS_CODE"] and request.Env["ELAPSED_TIME"] are set.
49
+ // They can be used for extra logging, or reporting.
50
+ // (see statsd example in in https://github.com/ant0ine/go-json-rest-examples)
51
+ OuterMiddlewares []Middleware
52
+
43
53
// Optional global middlewares that can be used to wrap the all REST endpoints.
44
54
// They are used in the defined order, the first wrapping the second, ...
45
55
// They are run pre REST routing, request.PathParams is not set yet.
56
+ // They are run post auto error handling, "panic" will be converted to 500 errors.
46
57
// They can be used for instance to manage CORS or authentication.
47
- // (see the CORS example in go-json-rest-example )
58
+ // (see the CORS and Auth examples in https://github.com/ant0ine/ go-json-rest-examples )
48
59
PreRoutingMiddlewares []Middleware
49
60
50
61
// Custom logger for the access log,
@@ -99,13 +110,19 @@ func (rh *ResourceHandler) SetRoutes(routes ...*Route) error {
99
110
// Instantiate all the middlewares.
100
111
func (rh * ResourceHandler ) instantiateMiddlewares () {
101
112
102
- middlewares := []Middleware {
103
- // log as the first, depend on timer and recorder.
113
+ middlewares := []Middleware {}
114
+
115
+ middlewares = append (middlewares ,
116
+ rh .OuterMiddlewares ... ,
117
+ )
118
+
119
+ // log as the first, depend on timer and recorder.
120
+ middlewares = append (middlewares ,
104
121
& logMiddleware {
105
122
rh .Logger ,
106
123
rh .EnableLogAsJson ,
107
124
},
108
- }
125
+ )
109
126
110
127
if rh .EnableGzip {
111
128
middlewares = append (middlewares , & gzipMiddleware {})
0 commit comments