serve service from custom path

This commit is contained in:
Ruidy Nemausat 2020-07-19 09:02:37 +02:00
parent b8ef90f3d2
commit 13a6e80ce2
2 changed files with 5 additions and 3 deletions

View file

@ -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
}()

View file

@ -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)