mirror of
https://github.com/rjNemo/go-microservices-tuto
synced 2026-06-12 13:26:45 +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 port = ":5000"
|
||||||
|
const productPath = "/products"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// create a logger to control application wide logging
|
// create a logger to control application wide logging
|
||||||
|
|
@ -26,14 +27,14 @@ func main() {
|
||||||
productsHandler := handlers.New(logger)
|
productsHandler := handlers.New(logger)
|
||||||
|
|
||||||
// register the handler method to the router
|
// register the handler method to the router
|
||||||
productsHandler.RegisterRoutes(router)
|
productsHandler.RegisterRoutes(router, productPath)
|
||||||
|
|
||||||
// creates a production-ready server using the handler
|
// creates a production-ready server using the handler
|
||||||
srv := server.New(router, port)
|
srv := server.New(router, port)
|
||||||
|
|
||||||
// start a non blocking application server
|
// start a non blocking application server
|
||||||
go func() {
|
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
|
logger.Fatalf("Server failed: %v", srv.ListenAndServe()) // TODO: use ListenAndServeTLS in production
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterRoutes associates path to controller
|
// 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
|
// GET
|
||||||
getRouter := r.Methods(http.MethodGet).Subrouter()
|
getRouter := r.Methods(http.MethodGet).Subrouter()
|
||||||
getRouter.HandleFunc("/", p.GetProducts)
|
getRouter.HandleFunc("/", p.GetProducts)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue