Skip to content

Commit 280b3d5

Browse files
emilevaugejuliens
authored andcommitted
Add route priority
Signed-off-by: Emile Vauge <[email protected]>
1 parent 9bd5b95 commit 280b3d5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

mux.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/http"
1212
"path"
1313
"regexp"
14+
"sort"
1415
)
1516

1617
var (
@@ -403,6 +404,17 @@ func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
403404
return nil
404405
}
405406

407+
type routes []*Route
408+
409+
func (r routes) Len() int { return len(r) }
410+
func (r routes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
411+
func (r routes) Less(i, j int) bool { return r[i].GetPriority() > r[j].GetPriority() }
412+
413+
// SortRoutes sort routes by route priority
414+
func (r *Router) SortRoutes() {
415+
sort.Sort(routes(r.routes))
416+
}
417+
406418
// ----------------------------------------------------------------------------
407419
// Context
408420
// ----------------------------------------------------------------------------

route.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type Route struct {
2323
name string
2424
// Error resulted from building a route.
2525
err error
26+
// Priority of this route
27+
priority int
2628

2729
// "global" reference to all named routes
2830
namedRoutes map[string]*Route
@@ -153,6 +155,19 @@ func (r *Route) GetName() string {
153155
return r.name
154156
}
155157

158+
// Priority -----------------------------------------------------------------------
159+
160+
// Priority sets the priority for the route
161+
func (r *Route) Priority(priority int) *Route {
162+
r.priority = priority
163+
return r
164+
}
165+
166+
// GetPriority returns the priority for the route.
167+
func (r *Route) GetPriority() int {
168+
return r.priority
169+
}
170+
156171
// ----------------------------------------------------------------------------
157172
// Matchers
158173
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)