2019-06-06 17:37:40 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2019-06-12 23:30:42 +02:00
|
|
|
"github.com/google/uuid"
|
2020-01-30 13:39:00 +02:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2019-06-06 17:37:40 +02:00
|
|
|
)
|
|
|
|
|
2019-06-13 13:09:49 +02:00
|
|
|
// Options are router options
|
2019-06-06 17:37:40 +02:00
|
|
|
type Options struct {
|
2019-07-10 08:45:27 +02:00
|
|
|
// Id is router id
|
|
|
|
Id string
|
2019-06-19 22:22:14 +02:00
|
|
|
// Address is router address
|
2019-06-07 14:29:09 +02:00
|
|
|
Address string
|
2019-07-24 18:16:52 +02:00
|
|
|
// Gateway is network gateway
|
2019-06-27 23:52:51 +02:00
|
|
|
Gateway string
|
2019-07-24 18:16:52 +02:00
|
|
|
// Network is network address
|
2019-07-09 16:45:42 +02:00
|
|
|
Network string
|
2019-06-19 22:22:14 +02:00
|
|
|
// Registry is the local registry
|
|
|
|
Registry registry.Registry
|
2019-10-09 17:03:06 +02:00
|
|
|
// Advertise is the advertising strategy
|
|
|
|
Advertise Strategy
|
2019-07-29 19:57:40 +02:00
|
|
|
// Client for calling router
|
|
|
|
Client client.Client
|
2019-06-06 17:37:40 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 08:45:27 +02:00
|
|
|
// Id sets Router Id
|
|
|
|
func Id(id string) Option {
|
2019-06-10 20:50:54 +02:00
|
|
|
return func(o *Options) {
|
2019-07-10 08:45:27 +02:00
|
|
|
o.Id = id
|
2019-06-10 20:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:12:07 +02:00
|
|
|
// Address sets router service address
|
2019-06-07 18:20:22 +02:00
|
|
|
func Address(a string) Option {
|
2019-06-07 14:29:09 +02:00
|
|
|
return func(o *Options) {
|
2019-06-07 18:20:22 +02:00
|
|
|
o.Address = a
|
2019-06-07 14:29:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:57:40 +02:00
|
|
|
// Client to call router service
|
|
|
|
func Client(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:45:42 +02:00
|
|
|
// Gateway sets network gateway
|
|
|
|
func Gateway(g string) Option {
|
2019-06-10 20:50:54 +02:00
|
|
|
return func(o *Options) {
|
2019-07-09 16:45:42 +02:00
|
|
|
o.Gateway = g
|
2019-06-10 20:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 16:45:42 +02:00
|
|
|
// Network sets router network
|
|
|
|
func Network(n string) Option {
|
2019-06-27 23:52:51 +02:00
|
|
|
return func(o *Options) {
|
2019-07-09 16:45:42 +02:00
|
|
|
o.Network = n
|
2019-06-27 23:52:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:16:52 +02:00
|
|
|
// Registry sets the local registry
|
|
|
|
func Registry(r registry.Registry) Option {
|
2019-06-07 18:20:22 +02:00
|
|
|
return func(o *Options) {
|
2019-07-24 18:16:52 +02:00
|
|
|
o.Registry = r
|
2019-06-07 14:29:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 17:03:06 +02:00
|
|
|
// Strategy sets route advertising strategy
|
|
|
|
func Advertise(a Strategy) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Advertise = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 23:30:42 +02:00
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
2019-10-09 18:23:02 +02:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
Address: DefaultAddress,
|
|
|
|
Network: DefaultNetwork,
|
|
|
|
Registry: registry.DefaultRegistry,
|
2019-12-02 19:36:20 +02:00
|
|
|
Advertise: AdvertiseLocal,
|
2019-06-07 00:29:24 +02:00
|
|
|
}
|
|
|
|
}
|