2019-08-20 14:48:51 +03:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-08-28 01:08:35 +03:00
|
|
|
"github.com/google/uuid"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/proxy"
|
|
|
|
"github.com/unistack-org/micro/v3/proxy/mucp"
|
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
regRouter "github.com/unistack-org/micro/v3/router/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/tunnel"
|
|
|
|
tmucp "github.com/unistack-org/micro/v3/tunnel/mucp"
|
2019-08-20 14:48:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options configure network
|
|
|
|
type Options struct {
|
2019-08-28 01:08:35 +03:00
|
|
|
// Id of the node
|
|
|
|
Id string
|
2019-08-20 14:48:51 +03:00
|
|
|
// Name of the network
|
|
|
|
Name string
|
|
|
|
// Address to bind to
|
|
|
|
Address string
|
2019-09-18 20:56:02 +03:00
|
|
|
// Advertise sets the address to advertise
|
|
|
|
Advertise string
|
2019-12-08 02:28:39 +03:00
|
|
|
// Nodes is a list of nodes to connect to
|
|
|
|
Nodes []string
|
2019-08-20 23:12:21 +03:00
|
|
|
// Tunnel is network tunnel
|
|
|
|
Tunnel tunnel.Tunnel
|
|
|
|
// Router is network router
|
|
|
|
Router router.Router
|
|
|
|
// Proxy is network proxy
|
|
|
|
Proxy proxy.Proxy
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// Id sets the id of the network node
|
|
|
|
func Id(id string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Id = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name sets the network name
|
2019-08-20 14:48:51 +03:00
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 01:08:35 +03:00
|
|
|
// Address sets the network address
|
2019-08-20 14:48:51 +03:00
|
|
|
func Address(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 20:56:02 +03:00
|
|
|
// Advertise sets the address to advertise
|
|
|
|
func Advertise(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Advertise = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 02:28:39 +03:00
|
|
|
// Nodes is a list of nodes to connect to
|
|
|
|
func Nodes(n ...string) Option {
|
2019-08-29 16:53:30 +03:00
|
|
|
return func(o *Options) {
|
2019-12-08 02:28:39 +03:00
|
|
|
o.Nodes = n
|
2019-08-29 16:53:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 23:12:21 +03:00
|
|
|
// Tunnel sets the network tunnel
|
|
|
|
func Tunnel(t tunnel.Tunnel) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tunnel = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Router sets the network router
|
|
|
|
func Router(r router.Router) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Router = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Proxy sets the network proxy
|
|
|
|
func Proxy(p proxy.Proxy) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Proxy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// DefaultOptions returns network default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
2020-08-18 23:38:29 +03:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
Name: "go.micro",
|
|
|
|
Address: ":0",
|
|
|
|
Tunnel: tmucp.NewTunnel(),
|
|
|
|
Router: regRouter.NewRouter(),
|
|
|
|
Proxy: mucp.NewProxy(),
|
2019-08-20 14:48:51 +03:00
|
|
|
}
|
|
|
|
}
|