2019-06-06 18:37:40 +03:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2020-06-24 13:46:51 +03:00
|
|
|
"context"
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
"github.com/google/uuid"
|
2020-07-27 15:22:00 +03:00
|
|
|
"github.com/micro/go-micro/v3/registry"
|
|
|
|
"github.com/micro/go-micro/v3/registry/mdns"
|
2019-06-06 18:37:40 +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-07-10 09:45:27 +03:00
|
|
|
// Id is router id
|
|
|
|
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-07-24 19:16:52 +03:00
|
|
|
// Gateway is network gateway
|
2019-06-28 00:52:51 +03:00
|
|
|
Gateway string
|
2019-07-24 19:16:52 +03:00
|
|
|
// Network is network address
|
2019-07-09 17:45:42 +03:00
|
|
|
Network string
|
2019-06-19 23:22:14 +03:00
|
|
|
// Registry is the local registry
|
|
|
|
Registry registry.Registry
|
2019-10-09 18:03:06 +03:00
|
|
|
// Advertise is the advertising strategy
|
|
|
|
Advertise Strategy
|
2020-06-24 13:46:51 +03:00
|
|
|
// Context for additional options
|
|
|
|
Context context.Context
|
2020-07-27 15:22:00 +03:00
|
|
|
// Precache the route table on router startup
|
|
|
|
Precache bool
|
2019-06-06 18:37:40 +03:00
|
|
|
}
|
|
|
|
|
2019-07-10 09:45:27 +03:00
|
|
|
// Id sets Router Id
|
|
|
|
func Id(id string) Option {
|
2019-06-10 21:50:54 +03:00
|
|
|
return func(o *Options) {
|
2019-07-10 09:45:27 +03:00
|
|
|
o.Id = id
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-09 17:45:42 +03:00
|
|
|
// Gateway sets network gateway
|
|
|
|
func Gateway(g string) Option {
|
2019-06-10 21:50:54 +03:00
|
|
|
return func(o *Options) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Gateway = g
|
2019-06-10 21:50:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// Network sets router network
|
|
|
|
func Network(n string) Option {
|
2019-06-28 00:52:51 +03:00
|
|
|
return func(o *Options) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Network = n
|
2019-06-28 00:52:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 19:16:52 +03:00
|
|
|
// Registry sets the local registry
|
|
|
|
func Registry(r registry.Registry) Option {
|
2019-06-07 19:20:22 +03:00
|
|
|
return func(o *Options) {
|
2019-07-24 19:16:52 +03:00
|
|
|
o.Registry = r
|
2019-06-07 15:29:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:05:21 +03:00
|
|
|
// Advertise sets route advertising strategy
|
2019-10-09 18:03:06 +03:00
|
|
|
func Advertise(a Strategy) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Advertise = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:22:00 +03:00
|
|
|
// Precache sets whether to precache the route table
|
|
|
|
func Precache(b bool) Option {
|
2020-07-01 12:05:21 +03:00
|
|
|
return func(o *Options) {
|
2020-07-27 15:22:00 +03:00
|
|
|
o.Precache = b
|
2020-07-01 12:05:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
2019-10-09 19:23:02 +03:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
Address: DefaultAddress,
|
|
|
|
Network: DefaultNetwork,
|
2020-07-27 15:22:00 +03:00
|
|
|
Registry: mdns.NewRegistry(),
|
2019-12-02 20:36:20 +03:00
|
|
|
Advertise: AdvertiseLocal,
|
2020-06-24 13:46:51 +03:00
|
|
|
Context: context.Background(),
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|