about summary refs log tree commit diff
path: root/router.go
blob: ce71df531551c0b2d0b498dcca8096367d10f389 (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package mooh

import (
	"errors"
	"fmt"
	"net/http"
	"strings"
)

type Router struct {
	routes     []*Route
	knownPaths map[string]map[string]bool
}

func BuildRouter() Router {
	router := Router{}
	router.knownPaths = map[string]map[string]bool{}
	return router
}

func (self *Router) routeIsKnown(route *Route) bool {
	if self.knownPaths[route.Path] == nil {
		self.knownPaths[route.Path] = map[string]bool{}
		return false
	} else if self.knownPaths[route.Path][route.Method] == false {
		return false
	}
	return true
}

func (self *Router) AddRoute(route *Route) error {
	if self.routeIsKnown(route) == true {
		return errors.New(fmt.Sprintf("The route %s with the method %s already exist.", route.Path, route.Method))
	}

	route.init()
	self.routes = append(self.routes, route)
	self.knownPaths[route.Path][route.Method] = true
	return nil
}

func (self *Router) Match(request *http.Request) (*Match, error) {

	matches := []*Match{}

	method := request.Method
	components := strings.Split(request.URL.Path, "/")

	for _, r := range self.routes {
		match := r.Match(method, components)
		if match != nil {
			matches = append(matches, match)
		}
	}

	if len(matches) == 0 {
		return nil, nil
	} else if len(matches) == 1 {
		return matches[0], nil
	} else {
		return self.disambiguateMatches(request.URL.Path, matches)
	}
	return nil, nil
}

func (self *Router) disambiguateMatches(path string, matches []*Match) (*Match, error) {
	min := -1
	found := []*Match{}

	for _, m := range matches {
		req := m.Route.requiredNamedComponents
		vars := len(req)
		if min == -1 || vars < min {
			found = append(found, m)
			min = vars
		} else if vars == min {
			found = append(found, m)
		}
	}

	if len(found) > 1 {
		msg := fmt.Sprintf("Ambiguous match: path %s could match any of:", path)
		for _, f := range found {
			msg = fmt.Sprintf("%s %s", msg, f.Route.Path)
		}
		err := errors.New(msg)
		return nil, err
	}
	return found[0], nil
}

func (self *Router) GetRouteList() []string {
	routes := make([]string, len(self.routes))
	for i, r := range self.routes{
		routes[i] = r.Path
	}
	return routes
}

// func (*dispatcher) AddRoutes() {
// }

// func (self *dispatcher) UriFor() {
// }