2019-08-20 12:48:51 +01:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-08-27 23:08:35 +01:00
|
|
|
"github.com/google/uuid"
|
2019-08-20 12:48:51 +01:00
|
|
|
"github.com/micro/go-micro/network/resolver"
|
2019-08-22 18:57:20 +01:00
|
|
|
"github.com/micro/go-micro/network/resolver/registry"
|
2019-08-20 21:12:21 +01:00
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/proxy/mucp"
|
|
|
|
"github.com/micro/go-micro/router"
|
|
|
|
"github.com/micro/go-micro/tunnel"
|
2019-08-20 12:48:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options configure network
|
|
|
|
type Options struct {
|
2019-08-27 23:08:35 +01:00
|
|
|
// Id of the node
|
|
|
|
Id string
|
2019-08-20 12:48:51 +01:00
|
|
|
// Name of the network
|
|
|
|
Name string
|
|
|
|
// Address to bind to
|
|
|
|
Address string
|
2019-09-18 18:56:02 +01:00
|
|
|
// Advertise sets the address to advertise
|
|
|
|
Advertise string
|
2019-09-19 16:32:15 +01:00
|
|
|
// Peers is a list of peers to connect to
|
|
|
|
Peers []string
|
2019-08-20 21:12:21 +01: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 12:48:51 +01:00
|
|
|
// Resolver is network resolver
|
|
|
|
Resolver resolver.Resolver
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01: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 12:48:51 +01:00
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:08:35 +01:00
|
|
|
// Address sets the network address
|
2019-08-20 12:48:51 +01:00
|
|
|
func Address(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 18:56:02 +01:00
|
|
|
// Advertise sets the address to advertise
|
|
|
|
func Advertise(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Advertise = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:32:15 +01:00
|
|
|
// Peers is a list of peers to connect to
|
|
|
|
func Peers(n ...string) Option {
|
2019-08-29 14:53:30 +01:00
|
|
|
return func(o *Options) {
|
2019-09-19 16:32:15 +01:00
|
|
|
o.Peers = n
|
2019-08-29 14:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:12:21 +01: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 12:48:51 +01:00
|
|
|
// Resolver is the network resolver
|
|
|
|
func Resolver(r resolver.Resolver) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Resolver = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions returns network default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
2019-08-27 23:08:35 +01:00
|
|
|
Id: uuid.New().String(),
|
2019-08-20 12:48:51 +01:00
|
|
|
Name: DefaultName,
|
|
|
|
Address: DefaultAddress,
|
2019-08-20 21:12:21 +01:00
|
|
|
Tunnel: tunnel.NewTunnel(),
|
|
|
|
Router: router.DefaultRouter,
|
|
|
|
Proxy: mucp.NewProxy(),
|
2019-08-22 18:57:20 +01:00
|
|
|
Resolver: ®istry.Resolver{},
|
2019-08-20 12:48:51 +01:00
|
|
|
}
|
|
|
|
}
|