@@ -102,12 +102,19 @@ func (composer *Composer) Only() *RouteConstraint {
102
102
}
103
103
}
104
104
105
- //When is a constraint that gives you all the route information to filter upon.
106
- func (rc * RouteConstraint ) When (routeFilter func (* RouteModel ) bool ) * RouteConstraint {
105
+ //WhenRouteMatches is a constraint that gives you all the route information to filter upon.
106
+ func (rc * RouteConstraint ) WhenRouteMatches (routeFilter func (* RouteModel ) bool ) * RouteConstraint {
107
107
rc .routeFilter = routeFilter
108
108
return rc
109
109
}
110
110
111
+ //When is a constraint that gives you the option to only apply upon a condition being true
112
+ func (rc * RouteConstraint ) When (condition func () bool ) * RouteConstraint {
113
+ return rc .WhenRouteMatches (func (r * RouteModel ) bool {
114
+ return condition ()
115
+ })
116
+ }
117
+
111
118
//Get constrains the middleware to only apply for 'GET' requests
112
119
func (rc * RouteConstraint ) Get () * RouteConstraint {
113
120
return rc .methodConstraint ("GET" )
@@ -134,7 +141,7 @@ func (rc *RouteConstraint) Delete() *RouteConstraint {
134
141
}
135
142
136
143
func (rc * RouteConstraint ) methodConstraint (method string ) * RouteConstraint {
137
- return rc .When (func (route * RouteModel ) bool {
144
+ return rc .WhenRouteMatches (func (route * RouteModel ) bool {
138
145
return route .Method == method
139
146
})
140
147
}
@@ -162,16 +169,17 @@ type Route struct {
162
169
//you have chosen for your application.
163
170
func (composer * Composer ) BuildRoutes () Routes {
164
171
routes := make (Routes , 0 , 10 )
165
- for _ , route := range composer .routeRegistry .routes {
172
+ for i := len (composer .routeRegistry .routes ) - 1 ; i >= 0 ; i -- {
173
+ route := composer .routeRegistry .routes [i ]
166
174
middleware := composer .middlewareRegistry .middlewareFor (route )
167
175
168
176
handler := build (route .Handler , middleware )
169
- route := & Route {
177
+ builtRoute := & Route {
170
178
Method : route .Method ,
171
179
Pattern : route .Pattern ,
172
180
Handler : handler ,
173
181
}
174
- routes = append (routes , route )
182
+ routes = append (routes , builtRoute )
175
183
}
176
184
return routes
177
185
}
0 commit comments