diff --git a/main.go b/main.go index ef5b44f..926b15b 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( ) const port = ":5000" +const productPath = "/products" func main() { // create a logger to control application wide logging @@ -26,14 +27,14 @@ func main() { productsHandler := handlers.New(logger) // register the handler method to the router - productsHandler.RegisterRoutes(router) + productsHandler.RegisterRoutes(router, productPath) // creates a production-ready server using the handler srv := server.New(router, port) // start a non blocking application server go func() { - logger.Printf("Server started at address http://localhost%s ...", port) + logger.Printf("Server started at http://localhost%s ...", port) logger.Fatalf("Server failed: %v", srv.ListenAndServe()) // TODO: use ListenAndServeTLS in production }() diff --git a/products/handlers/routes.go b/products/handlers/routes.go index ffc68dc..258d664 100644 --- a/products/handlers/routes.go +++ b/products/handlers/routes.go @@ -8,7 +8,8 @@ import ( ) // RegisterRoutes associates path to controller -func (p *Products) RegisterRoutes(r *mux.Router) { +func (p *Products) RegisterRoutes(m *mux.Router, path string) { + r := m.StrictSlash(true).PathPrefix(path).Subrouter() // GET getRouter := r.Methods(http.MethodGet).Subrouter() getRouter.HandleFunc("/", p.GetProducts)