Skip to content

Commit a819b77

Browse files
committed
Merge pull request #1 from containous/add-route-priority
Add route priority
2 parents bd09be0 + 6b9da47 commit a819b77

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

mux.go

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"path"
1212
"regexp"
13+
"sort"
1314

1415
"github.com/gorilla/context"
1516
)
@@ -305,6 +306,17 @@ func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
305306
return nil
306307
}
307308

309+
type routes []*Route
310+
311+
func (r routes) Len() int { return len(r) }
312+
func (r routes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
313+
func (r routes) Less(i, j int) bool { return r[i].GetPriority() > r[j].GetPriority() }
314+
315+
// SortRoutes sort routes by route priority
316+
func (r *Router) SortRoutes() {
317+
sort.Sort(routes(r.routes))
318+
}
319+
308320
// ----------------------------------------------------------------------------
309321
// Context
310322
// ----------------------------------------------------------------------------

route.go

+15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type Route struct {
3535
name string
3636
// Error resulted from building a route.
3737
err error
38+
// Priority of this route
39+
priority int
3840

3941
buildVarsFunc BuildVarsFunc
4042
}
@@ -127,6 +129,19 @@ func (r *Route) GetName() string {
127129
return r.name
128130
}
129131

132+
// Priority -----------------------------------------------------------------------
133+
134+
// Priority sets the priority for the route
135+
func (r *Route) Priority(priority int) *Route {
136+
r.priority = priority
137+
return r
138+
}
139+
140+
// GetPriority returns the priority for the route.
141+
func (r *Route) GetPriority() int {
142+
return r.priority
143+
}
144+
130145
// ----------------------------------------------------------------------------
131146
// Matchers
132147
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)