From f97475a5404b5693a47cbbee19adf81c650b13f2 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 28 Apr 2013 14:04:28 -0700 Subject: 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. --- router.go | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'router.go') 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 +} -- cgit 1.4.1