about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-22 21:06:02 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-22 21:06:02 -0700
commit4166296aab961347932515212abd7eac9bda01a8 (patch)
tree4a55b65e363594eb221b6c61ee643e5693355526
parentAdd a very simple exemple to the framework. (diff)
downloadpath-router-4166296aab961347932515212abd7eac9bda01a8.tar.gz
Routes inside the dispatcher private and check uniqueness of routes
There's no need to expose directly the list of routes inside the router,
we will provide an interface to that later.

Also, when adding a new routes, we verify that the route don't already
exist, and if it's the case, we return an error.
-rw-r--r--dispatcher.go42
-rw-r--r--request_test.go2
-rw-r--r--router_test.go6
3 files changed, 24 insertions, 26 deletions
diff --git a/dispatcher.go b/dispatcher.go
index 7e17e54..54c607d 100644
--- a/dispatcher.go
+++ b/dispatcher.go
@@ -8,37 +8,35 @@ import (
 )
 
 type Dispatcher struct {
-	Routes      []*Route
-	RouteAccess map[string]*Route
+	routes     []*Route
+	knownPaths map[string]map[string]bool
 }
 
 func BuildDispatcher() Dispatcher {
 	router := Dispatcher{}
-	router.RouteAccess = map[string]*Route{}
+	router.knownPaths = map[string]map[string]bool{}
 	return router
 }
 
-func (self *Dispatcher) GetRoute(route *Route) *Route {
-	// r := self.RouteAccess[route.Path]
-
-	// if r != nil {
-	// 	return r
-	// }
-
-	return nil
+func (self *Dispatcher) 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 *Dispatcher) AddRoute(route *Route) {
-	r := self.GetRoute(route)
-
-	if r == nil {
-		route.init()
-		self.Routes = append(self.Routes, route)
-	// 	self.RouteAccess[route.Path] = route
-	} else {
-	// 	// r.Executors[method] = code
-	// 	// r.Methods[method] = true
+func (self *Dispatcher) AddRoute(route *Route) error {
+	if self.routeIsKnown(route) == true {
+		return errors.New(fmt.Sprintf("Can't add twice the same route. The route %s with the method %s is already added", route.Path, route.Method))
 	}
+
+	route.init()
+	self.routes = append(self.routes, route)
+	self.knownPaths[route.Path][route.Method] = true
+	return nil
 }
 
 func (self *Dispatcher) Match(request *http.Request) (*Match, error) {
@@ -48,7 +46,7 @@ func (self *Dispatcher) Match(request *http.Request) (*Match, error) {
 	method := request.Method
 	components := strings.Split(request.URL.Path, "/")
 
-	for _, r := range self.Routes {
+	for _, r := range self.routes {
 		match := r.Match(method, components)
 		if match != nil {
 			matches = append(matches, match)
diff --git a/request_test.go b/request_test.go
index 5eb58a0..715baae 100644
--- a/request_test.go
+++ b/request_test.go
@@ -19,7 +19,7 @@ func buildDispatcher() Dispatcher {
 
 func TestBasicRequest(t *testing.T) {
 	router := buildDispatcher()
-	t.Log(router.Routes[0].components)
+	t.Log(router.routes[0].components)
 	r := &http.Request{Method: "GET", URL: &url.URL{Path: "/foo/bar"}}
 	m, _ := router.Match(r)
 	request := NewRequest(r, m)
diff --git a/router_test.go b/router_test.go
index bc6bc0c..8ee2bcc 100644
--- a/router_test.go
+++ b/router_test.go
@@ -18,13 +18,13 @@ func TestBasic(t *testing.T) {
 	router.AddRoute(&Route{Method: "GET", Path: "/blog/:year/:month/:day", Code: testRoute})
 	router.AddRoute(&Route{Method: "GET", Path: "/blog", Code: testRoute})
 
-	if router.Routes[0].Path != "/" {
+	if router.routes[0].Path != "/" {
 		t.Fatal()
 	}
-	if router.Routes[1].Path != "/blog/:year/:month/:day" {
+	if router.routes[1].Path != "/blog/:year/:month/:day" {
 		t.Fatal()
 	}
-	if router.Routes[2].Path != "/blog" {
+	if router.routes[2].Path != "/blog" {
 		t.Fatal()
 	}
 }