Skip to content

Commit

Permalink
Add route priority
Browse files Browse the repository at this point in the history
Signed-off-by: Emile Vauge <emile@vauge.com>
  • Loading branch information
emilevauge committed Jul 4, 2017
1 parent ac112f7 commit af6ea92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"path"
"regexp"
"sort"
"strings"
)

Expand Down Expand Up @@ -335,6 +336,17 @@ func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
return nil
}

type routes []*Route

func (r routes) Len() int { return len(r) }
func (r routes) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r routes) Less(i, j int) bool { return r[i].GetPriority() > r[j].GetPriority() }

// SortRoutes sort routes by route priority
func (r *Router) SortRoutes() {
sort.Sort(routes(r.routes))
}

// ----------------------------------------------------------------------------
// Context
// ----------------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Route struct {
name string
// Error resulted from building a route.
err error
// Priority of this route
priority int

buildVarsFunc BuildVarsFunc
}
Expand Down Expand Up @@ -131,6 +133,19 @@ func (r *Route) GetName() string {
return r.name
}

// Priority -----------------------------------------------------------------------

// Priority sets the priority for the route
func (r *Route) Priority(priority int) *Route {
r.priority = priority
return r
}

// GetPriority returns the priority for the route.
func (r *Route) GetPriority() int {
return r.priority
}

// ----------------------------------------------------------------------------
// Matchers
// ----------------------------------------------------------------------------
Expand Down

0 comments on commit af6ea92

Please sign in to comment.