Skip to content

Commit 0a3a97e

Browse files
emilevaugeldez
authored andcommitted
Add route priority
Signed-off-by: Emile Vauge <[email protected]>
1 parent d13b762 commit 0a3a97e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mux.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"path"
1212
"regexp"
13+
"sort"
1314
)
1415

1516
var (
@@ -353,6 +354,17 @@ func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
353354
return nil
354355
}
355356

357+
type routes []*Route
358+
359+
func (r routes) Len() int { return len(r) }
360+
func (r routes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
361+
func (r routes) Less(i, j int) bool { return r[i].GetPriority() > r[j].GetPriority() }
362+
363+
// SortRoutes sort routes by route priority
364+
func (r *Router) SortRoutes() {
365+
sort.Sort(routes(r.routes))
366+
}
367+
356368
// ----------------------------------------------------------------------------
357369
// Context
358370
// ----------------------------------------------------------------------------
@@ -372,7 +384,7 @@ type RouteMatch struct {
372384
type contextKey int
373385

374386
const (
375-
varsKey contextKey = iota
387+
varsKey contextKey = iota
376388
routeKey
377389
)
378390

route.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type Route struct {
3939
name string
4040
// Error resulted from building a route.
4141
err error
42+
// Priority of this route
43+
priority int
4244

4345
buildVarsFunc BuildVarsFunc
4446
}
@@ -147,6 +149,19 @@ func (r *Route) GetName() string {
147149
return r.name
148150
}
149151

152+
// Priority -----------------------------------------------------------------------
153+
154+
// Priority sets the priority for the route
155+
func (r *Route) Priority(priority int) *Route {
156+
r.priority = priority
157+
return r
158+
}
159+
160+
// GetPriority returns the priority for the route.
161+
func (r *Route) GetPriority() int {
162+
return r.priority
163+
}
164+
150165
// ----------------------------------------------------------------------------
151166
// Matchers
152167
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)