From d0d74bdc832353b12139dd904e555b0fbe2b02ee Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Sat, 22 Aug 2020 20:55:43 +0100 Subject: [PATCH] Update static router --- static.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/static.go b/static.go index a25a0e5..28211c5 100644 --- a/static.go +++ b/static.go @@ -1,9 +1,18 @@ +// Package static is a static router which returns the service name as the address + port package static import ( + "fmt" + "net" + "github.com/micro/go-micro/v3/router" ) +var ( + // DefaulPort is the port to append where nothing is set + DefaultPort = 8080 +) + // NewRouter returns an initialized static router func NewRouter(opts ...router.Option) router.Router { options := router.DefaultOptions() @@ -35,10 +44,18 @@ func (s *static) Table() router.Table { func (s *static) Lookup(service string, opts ...router.LookupOption) ([]router.Route, error) { options := router.NewLookup(opts...) + _, _ , err := net.SplitHostPort(service) + if err == nil { + // use the address + options.Address = service + } else { + options.Address = fmt.Sprintf("%s:%d", service, DefaultPort) + } + return []router.Route{ router.Route{ - Address: service, - Service: options.Address, + Service: service, + Address: options.Address, Gateway: options.Gateway, Network: options.Network, Router: options.Router,