2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-13 00:30:42 +03:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/micro/go-micro/registry"
|
2019-06-06 18:37:40 +03:00
|
|
|
)
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
var (
|
2019-06-19 23:22:14 +03:00
|
|
|
// DefaultAddress is default router address
|
|
|
|
DefaultAddress = ":9093"
|
|
|
|
// DefaultAdvertise is default address advertised to the network
|
|
|
|
DefaultAdvertise = ":9094"
|
2019-06-13 00:30:42 +03:00
|
|
|
)
|
|
|
|
|
2019-06-13 14:09:49 +03:00
|
|
|
// Options are router options
|
2019-06-06 18:37:40 +03:00
|
|
|
type Options struct {
|
2019-06-13 17:12:07 +03:00
|
|
|
// ID is router id
|
2019-06-10 21:50:54 +03:00
|
|
|
ID string
|
2019-06-19 23:22:14 +03:00
|
|
|
// Address is router address
|
2019-06-07 15:29:09 +03:00
|
|
|
Address string
|
2019-06-19 23:22:14 +03:00
|
|
|
// Advertise is the address advertised to the network
|
|
|
|
Advertise string
|
|
|
|
// Registry is the local registry
|
|
|
|
Registry registry.Registry
|
|
|
|
// Networkis the network registry
|
|
|
|
Network registry.Registry
|
2019-06-07 15:29:09 +03:00
|
|
|
// Table is routing table
|
|
|
|
Table Table
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// ID sets Router ID
|
|
|
|
func ID(id string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.ID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:12:07 +03:00
|
|
|
// Address sets router service address
|
2019-06-07 19:20:22 +03:00
|
|
|
func Address(a string) Option {
|
2019-06-07 15:29:09 +03:00
|
|
|
return func(o *Options) {
|
2019-06-07 19:20:22 +03:00
|
|
|
o.Address = a
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// Advertise sets the address that is advertise to the network
|
|
|
|
func Advertise(n string) Option {
|
2019-06-10 21:50:54 +03:00
|
|
|
return func(o *Options) {
|
2019-06-19 23:22:14 +03:00
|
|
|
o.Advertise = n
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// RoutingTable sets the routing table
|
2019-06-13 00:30:42 +03:00
|
|
|
func RoutingTable(t Table) Option {
|
2019-06-07 19:20:22 +03:00
|
|
|
return func(o *Options) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.Table = t
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// Registry sets the local registry
|
|
|
|
func Registry(r registry.Registry) Option {
|
2019-06-13 00:30:42 +03:00
|
|
|
return func(o *Options) {
|
2019-06-19 23:22:14 +03:00
|
|
|
o.Registry = r
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// Network sets the network registry
|
|
|
|
func Network(r registry.Registry) Option {
|
2019-06-13 00:30:42 +03:00
|
|
|
return func(o *Options) {
|
2019-06-19 23:22:14 +03:00
|
|
|
o.Network = r
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
// NOTE: by default both local and network registies use default registry i.e. mdns
|
|
|
|
return Options{
|
2019-06-19 23:22:14 +03:00
|
|
|
ID: uuid.New().String(),
|
|
|
|
Address: DefaultAddress,
|
|
|
|
Advertise: DefaultAdvertise,
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
Network: registry.DefaultRegistry,
|
|
|
|
Table: NewTable(),
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|