Skip to content

Commit 660ef4e

Browse files
committed
Make WrapMiddlewares public
This will help configuring sets for post routing middlewares.
1 parent 187e480 commit 660ef4e

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

rest/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ type ResourceHandler struct {
4242

4343
// Optional global middlewares that can be used to wrap the all REST endpoints.
4444
// They are used in the defined order, the first wrapping the second, ...
45+
// They are run pre REST routing, request.PathParams is not set yet.
4546
// They can be used for instance to manage CORS or authentication.
4647
// (see the CORS example in go-json-rest-example)
47-
// They are run pre REST routing, request.PathParams is not set yet.
4848
PreRoutingMiddlewares []Middleware
4949

5050
// Custom logger for the access log,
@@ -96,9 +96,9 @@ func (rh *ResourceHandler) SetRoutes(routes ...*Route) error {
9696
return nil
9797
}
9898

99+
// Instantiate all the middlewares.
99100
func (rh *ResourceHandler) instantiateMiddlewares() {
100101

101-
// instantiate all the middlewares
102102
middlewares := []Middleware{
103103
// log as the first, depend on timer and recorder.
104104
&logMiddleware{
@@ -132,11 +132,11 @@ func (rh *ResourceHandler) instantiateMiddlewares() {
132132
)
133133

134134
rh.handlerFunc = rh.adapter(
135-
wrapMiddlewares(middlewares, rh.app()),
135+
WrapMiddlewares(middlewares, rh.app()),
136136
)
137137
}
138138

139-
// Middleware that handles the transition between http and rest objects.
139+
// Handle the transition between http and rest objects.
140140
func (rh *ResourceHandler) adapter(handler HandlerFunc) http.HandlerFunc {
141141
return func(origWriter http.ResponseWriter, origRequest *http.Request) {
142142

rest/middleware.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ type Middleware interface {
99
MiddlewareFunc(handler HandlerFunc) HandlerFunc
1010
}
1111

12-
// wrapMiddlewares calls the MiddlewareFunc in the reverse order and returns an HandlerFunc ready
13-
// to be executed.
14-
func wrapMiddlewares(middlewares []Middleware, handler HandlerFunc) HandlerFunc {
12+
// WrapMiddlewares calls the MiddlewareFunc methods in the reverse order and returns an HandlerFunc
13+
// ready to be executed. This can be used to wrap a set of middlewares, post routing, on a per Route
14+
// basis.
15+
func WrapMiddlewares(middlewares []Middleware, handler HandlerFunc) HandlerFunc {
1516
wrapped := handler
1617
for i := len(middlewares) - 1; i >= 0; i-- {
1718
wrapped = middlewares[i].MiddlewareFunc(wrapped)

rest/middleware_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestWrapMiddlewares(t *testing.T) {
3434
// do nothing
3535
}
3636

37-
handlerFunc := wrapMiddlewares([]Middleware{a, b, c}, app)
37+
handlerFunc := WrapMiddlewares([]Middleware{a, b, c}, app)
3838

3939
// fake request
4040
r := &Request{

0 commit comments

Comments
 (0)