about summary refs log tree commit diff
path: root/router.go
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-28 14:04:28 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-28 14:04:28 -0700
commitf97475a5404b5693a47cbbee19adf81c650b13f2 (patch)
tree48967c97b532b6f04df24cf25219f836d387e6f4 /router.go
parentUse `knownPaths` to return the list of paths in GetRouteList. (diff)
downloadpath-router-f97475a5404b5693a47cbbee19adf81c650b13f2.tar.gz
Add a few more methods for future introspection to the Router.
The HasPath method check if a path is know by the router and return a
boolean.

The RemovePath method is useful to remove dynamically a path from the
router.

The GetAllRoutesByMethods returns a list of routes that match a given
method.

I'm not sure I'll keep all those methods available since there's no
immutability on the routes, and I don't want the user to mess with
them.  We will see later.
Diffstat (limited to 'router.go')
-rw-r--r--router.go41
1 files changed, 37 insertions, 4 deletions
diff --git a/router.go b/router.go
index d5bcfd0..c40713d 100644
--- a/router.go
+++ b/router.go
@@ -99,8 +99,41 @@ func (self *Router) GetRouteList() []string {
 	return routes
 }
 
-// func (*dispatcher) AddRoutes() {
-// }
+func (self *Router) HasPath(path string) bool {
+	if self.knownPaths[path] != nil {
+		return true
+	}
+	return false
+}
+
+func (self *Router) RemovePath(path string) error {
+	p := self.HasPath(path)
+	if p == false {
+		return errors.New("foo")
+	}
+	delete(self.knownPaths, path)
+
+	newRoutes := []*Route{}
 
-// func (self *dispatcher) UriFor() {
-// }
+	for _, p := range self.routes {
+		if p.Path != path {
+			newRoutes = append(newRoutes, p)
+		}
+	}
+	self.routes = newRoutes
+	return nil
+}
+
+func (self *Router) GetAllRoutes() []*Route {
+	return self.routes
+}
+
+func (self *Router) GetAllRoutesByMethods (method string) []*Route {
+	routes := []*Route{}
+	for _, r := range self.routes {
+		if r.Method == method {
+			routes = append(routes, r)
+		}
+	}
+	return routes
+}