Skip to content

Commit

Permalink
Issue 471: Fix panic when removing a route that doesn't exist in w.ro…
Browse files Browse the repository at this point in the history
…utes. (#472)

* Issue 471: Fix panic when removing a route that doesn't exist in w.routes.

* Incorporate PR feedback.
  • Loading branch information
jeffreydwalter committed Jul 14, 2021
1 parent 2326c8e commit f8e15b6
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions web_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,20 @@ func (w *WebService) Route(builder *RouteBuilder) *WebService {

// RemoveRoute removes the specified route, looks for something that matches 'path' and 'method'
func (w *WebService) RemoveRoute(path, method string) error {
if !w.dynamicRoutes {
return errors.New("dynamic routes are not enabled.")
}
w.routesLock.Lock()
defer w.routesLock.Unlock()
newRoutes := make([]Route, (len(w.routes) - 1))
current := 0
for ix := range w.routes {
if w.routes[ix].Method == method && w.routes[ix].Path == path {
continue
}
newRoutes[current] = w.routes[ix]
current++
}
w.routes = newRoutes
return nil
if !w.dynamicRoutes {
return errors.New("dynamic routes are not enabled.")
}
w.routesLock.Lock()
defer w.routesLock.Unlock()
newRoutes := []Route{}
for _, route := range w.routes {
if route.Method == method && route.Path == path {
continue
}
newRoutes = append(newRoutes, route)
}
w.routes = newRoutes
return nil
}

// Method creates a new RouteBuilder and initialize its http method
Expand Down

0 comments on commit f8e15b6

Please sign in to comment.