mirror of
https://github.com/rjNemo/go-microservices-tuto
synced 2026-06-06 02:16:46 +00:00
serve service from custom path
This commit is contained in:
parent
b8ef90f3d2
commit
13a6e80ce2
2 changed files with 5 additions and 3 deletions
5
main.go
5
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
|
||||
}()
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue