Skip to content

Commit 9b28a19

Browse files
committed
Introduce OuterMiddlewares
They are optional middlewares that wrap all go-json-rest internal middlewares. Main use cases are additional logging and reporting.
1 parent 660ef4e commit 9b28a19

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

rest/handler.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,22 @@ type ResourceHandler struct {
4040
// Note: If a charset parameter exists, it MUST be UTF-8
4141
EnableRelaxedContentType bool
4242

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+
4353
// Optional global middlewares that can be used to wrap the all REST endpoints.
4454
// They are used in the defined order, the first wrapping the second, ...
4555
// 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.
4657
// 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)
4859
PreRoutingMiddlewares []Middleware
4960

5061
// Custom logger for the access log,
@@ -99,13 +110,19 @@ func (rh *ResourceHandler) SetRoutes(routes ...*Route) error {
99110
// Instantiate all the middlewares.
100111
func (rh *ResourceHandler) instantiateMiddlewares() {
101112

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,
104121
&logMiddleware{
105122
rh.Logger,
106123
rh.EnableLogAsJson,
107124
},
108-
}
125+
)
109126

110127
if rh.EnableGzip {
111128
middlewares = append(middlewares, &gzipMiddleware{})

0 commit comments

Comments
 (0)