2019-06-06 16:37:40 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-12 22:30:42 +01:00
|
|
|
"github.com/google/uuid"
|
2019-07-08 16:51:55 +01:00
|
|
|
"github.com/micro/go-micro/network/router/table"
|
2019-06-12 22:30:42 +01:00
|
|
|
"github.com/micro/go-micro/registry"
|
2019-06-06 16:37:40 +01:00
|
|
|
)
|
|
|
|
|
2019-06-12 22:30:42 +01:00
|
|
|
var (
|
2019-06-19 21:22:14 +01:00
|
|
|
// DefaultAddress is default router address
|
|
|
|
DefaultAddress = ":9093"
|
2019-06-27 22:52:51 +01:00
|
|
|
// DefaultNetwork is default micro network
|
2019-07-09 15:45:42 +01:00
|
|
|
DefaultNetwork = "go.micro"
|
2019-06-12 22:30:42 +01:00
|
|
|
)
|
|
|
|
|
2019-06-13 12:09:49 +01:00
|
|
|
// Options are router options
|
2019-06-06 16:37:40 +01:00
|
|
|
type Options struct {
|
2019-07-10 07:45:27 +01:00
|
|
|
// Id is router id
|
|
|
|
Id string
|
2019-06-19 21:22:14 +01:00
|
|
|
// Address is router address
|
2019-06-07 13:29:09 +01:00
|
|
|
Address string
|
2019-06-27 22:52:51 +01:00
|
|
|
// Gateway is micro network gateway
|
|
|
|
Gateway string
|
2019-07-09 15:45:42 +01:00
|
|
|
// Network is micro network
|
|
|
|
Network string
|
2019-06-19 21:22:14 +01:00
|
|
|
// Registry is the local registry
|
|
|
|
Registry registry.Registry
|
2019-06-07 13:29:09 +01:00
|
|
|
// Table is routing table
|
2019-07-08 16:51:55 +01:00
|
|
|
Table table.Table
|
2019-06-06 16:37:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:45:27 +01:00
|
|
|
// Id sets Router Id
|
|
|
|
func Id(id string) Option {
|
2019-06-10 19:50:54 +01:00
|
|
|
return func(o *Options) {
|
2019-07-10 07:45:27 +01:00
|
|
|
o.Id = id
|
2019-06-10 19:50:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:12:07 +01:00
|
|
|
// Address sets router service address
|
2019-06-07 17:20:22 +01:00
|
|
|
func Address(a string) Option {
|
2019-06-07 13:29:09 +01:00
|
|
|
return func(o *Options) {
|
2019-06-07 17:20:22 +01:00
|
|
|
o.Address = a
|
2019-06-07 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// Gateway sets network gateway
|
|
|
|
func Gateway(g string) Option {
|
2019-06-10 19:50:54 +01:00
|
|
|
return func(o *Options) {
|
2019-07-09 15:45:42 +01:00
|
|
|
o.Gateway = g
|
2019-06-10 19:50:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:45:42 +01:00
|
|
|
// Network sets router network
|
|
|
|
func Network(n string) Option {
|
2019-06-27 22:52:51 +01:00
|
|
|
return func(o *Options) {
|
2019-07-09 15:45:42 +01:00
|
|
|
o.Network = n
|
2019-06-27 22:52:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 07:45:27 +01:00
|
|
|
// Table sets the routing table
|
|
|
|
func Table(t table.Table) Option {
|
2019-06-07 17:20:22 +01:00
|
|
|
return func(o *Options) {
|
2019-06-12 22:30:42 +01:00
|
|
|
o.Table = t
|
2019-06-07 13:29:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// Registry sets the local registry
|
|
|
|
func Registry(r registry.Registry) Option {
|
2019-06-12 22:30:42 +01:00
|
|
|
return func(o *Options) {
|
2019-06-19 21:22:14 +01:00
|
|
|
o.Registry = r
|
2019-06-12 22:30:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
2019-07-10 07:45:27 +01:00
|
|
|
Id: uuid.New().String(),
|
2019-06-26 16:03:19 +01:00
|
|
|
Address: DefaultAddress,
|
2019-06-27 22:52:51 +01:00
|
|
|
Network: DefaultNetwork,
|
2019-06-26 16:03:19 +01:00
|
|
|
Registry: registry.DefaultRegistry,
|
2019-07-08 16:51:55 +01:00
|
|
|
Table: table.NewTable(),
|
2019-06-06 23:29:24 +01:00
|
|
|
}
|
|
|
|
}
|