diff options
author | Franck Cuny <franck@lumberjaph.net> | 2013-04-28 14:04:28 -0700 |
---|---|---|
committer | Franck Cuny <franck@lumberjaph.net> | 2013-04-28 14:04:28 -0700 |
commit | f97475a5404b5693a47cbbee19adf81c650b13f2 (patch) | |
tree | 48967c97b532b6f04df24cf25219f836d387e6f4 /router.go | |
parent | Use `knownPaths` to return the list of paths in GetRouteList. (diff) | |
download | path-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.go | 41 |
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 +} |