Update static router

This commit is contained in:
Asim Aslam 2020-08-22 20:55:43 +01:00
parent 7c7df6b35d
commit 6f0594eebe

View File

@ -1,9 +1,18 @@
// Package static is a static router which returns the service name as the address + port
package static package static
import ( import (
"fmt"
"net"
"github.com/micro/go-micro/v3/router" "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 // NewRouter returns an initialized static router
func NewRouter(opts ...router.Option) router.Router { func NewRouter(opts ...router.Option) router.Router {
options := router.DefaultOptions() 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) { func (s *static) Lookup(service string, opts ...router.LookupOption) ([]router.Route, error) {
options := router.NewLookup(opts...) 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{ return []router.Route{
router.Route{ router.Route{
Address: service, Service: service,
Service: options.Address, Address: options.Address,
Gateway: options.Gateway, Gateway: options.Gateway,
Network: options.Network, Network: options.Network,
Router: options.Router, Router: options.Router,