Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 57 lines (47 sloc) 1.321 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
package mango

import (
"regexp"
"sort"
)

// Methods required by sort.Interface.
type matcherArray []*regexp.Regexp

func specificity(matcher *regexp.Regexp) int {
return len(matcher.String())
}
func (this matcherArray) Len() int {
return len(this)
}
func (this matcherArray) Less(i, j int) bool {
// The sign is reversed below so we sort the matchers in descending order
return specificity(this[i]) > specificity(this[j])
}
func (this matcherArray) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}

func Routing(routes map[string]App) Middleware {
matchers := matcherArray{}
handlers := []App{}

// Compile the matchers
for matcher, _ := range routes {
// compile the matchers
matchers = append([]*regexp.Regexp(matchers), regexp.MustCompile(matcher))
}

// sort 'em by descending length
sort.Sort(matchers)

// Attach the handlers to each matcher
for _, matcher := range matchers {
// Attach them to their handlers
handlers = append(handlers, routes[matcher.String()])
}

return func(env Env, app App) (Status, Headers, Body) {
path := []byte(env.Request().URL.Path)
for i, matcher := range matchers {
if matcher.Match(path) {
// Matched a route return it
return handlers[i](env)
}
}

// didn't match any of the other routes. pass upstream.
return app(env)
}
}
Something went wrong with that request. Please try again.