Default handlers for NotFound and MethodNotAllowed returning json reason added to control y data servers

This commit is contained in:
Héctor Hurtado
2020-04-07 12:22:15 +02:00
parent 4988c48e03
commit e1788b2874
3 changed files with 33 additions and 4 deletions
+12
View File
@@ -33,6 +33,7 @@ import (
// server exposes list, get, delete and add route endpoints.
func configRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/routes/{id}", removeRoute).
Methods(http.MethodDelete)
r.HandleFunc("/routes/{id}", getRoute).
@@ -41,9 +42,20 @@ func configRouter() *mux.Router {
Methods(http.MethodGet)
r.HandleFunc("/routes", addRoute).
Methods(http.MethodPost)
r.NotFoundHandler = http.HandlerFunc(defNotFoundHandler)
r.MethodNotAllowedHandler = http.HandlerFunc(defMethodNotAllowedHandler)
return r
}
func defNotFoundHandler(w http.ResponseWriter, h *http.Request) {
httperror.ErrorJSON(w, "Data server: Not found", http.StatusNotFound)
}
func defMethodNotAllowedHandler(w http.ResponseWriter, h *http.Request) {
httperror.ErrorJSON(w, "Data server: Method not allowed", http.StatusMethodNotAllowed)
}
// funcRemove Method used to ask the route model module to delete a route
var funcRemove func(id string) error = user.Routes.Delete