2020-08-22 22:55:43 +03:00
|
|
|
// Package static is a static router which returns the service name as the address + port
|
2020-06-24 13:46:51 +03:00
|
|
|
package static
|
|
|
|
|
2020-07-01 19:06:59 +03:00
|
|
|
import (
|
2020-08-22 22:55:43 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/router"
|
2020-07-01 19:06:59 +03:00
|
|
|
)
|
2020-06-24 13:46:51 +03:00
|
|
|
|
2020-08-22 22:55:43 +03:00
|
|
|
var (
|
|
|
|
// DefaulPort is the port to append where nothing is set
|
|
|
|
DefaultPort = 8080
|
|
|
|
)
|
|
|
|
|
2020-06-24 13:46:51 +03:00
|
|
|
// NewRouter returns an initialized static router
|
|
|
|
func NewRouter(opts ...router.Option) router.Router {
|
|
|
|
options := router.DefaultOptions()
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2020-08-21 11:23:01 +03:00
|
|
|
return &static{options}
|
2020-06-24 13:46:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type static struct {
|
|
|
|
options router.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *static) Init(opts ...router.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.options)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *static) Options() router.Options {
|
|
|
|
return s.options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *static) Table() router.Table {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
func (s *static) Lookup(service string, opts ...router.LookupOption) ([]router.Route, error) {
|
|
|
|
options := router.NewLookup(opts...)
|
|
|
|
|
2020-08-23 15:10:48 +03:00
|
|
|
_, _, err := net.SplitHostPort(service)
|
2020-08-22 22:55:43 +03:00
|
|
|
if err == nil {
|
|
|
|
// use the address
|
|
|
|
options.Address = service
|
|
|
|
} else {
|
|
|
|
options.Address = fmt.Sprintf("%s:%d", service, DefaultPort)
|
|
|
|
}
|
|
|
|
|
2020-08-21 11:23:01 +03:00
|
|
|
return []router.Route{
|
|
|
|
router.Route{
|
2020-08-22 22:55:43 +03:00
|
|
|
Service: service,
|
|
|
|
Address: options.Address,
|
2020-08-21 11:23:01 +03:00
|
|
|
Gateway: options.Gateway,
|
|
|
|
Network: options.Network,
|
|
|
|
Router: options.Router,
|
|
|
|
},
|
|
|
|
}, nil
|
2020-06-24 13:46:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *static) Watch(opts ...router.WatchOption) (router.Watcher, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *static) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *static) String() string {
|
|
|
|
return "static"
|
|
|
|
}
|