micro/network/options.go

114 lines
2.3 KiB
Go
Raw Normal View History

package network
import (
"github.com/google/uuid"
"github.com/micro/go-micro/v3/network/resolver"
"github.com/micro/go-micro/v3/network/resolver/noop"
"github.com/micro/go-micro/v3/proxy"
"github.com/micro/go-micro/v3/proxy/mucp"
"github.com/micro/go-micro/v3/router"
regRouter "github.com/micro/go-micro/v3/router/registry"
"github.com/micro/go-micro/v3/tunnel"
2020-08-10 19:31:21 +03:00
tmucp "github.com/micro/go-micro/v3/tunnel/mucp"
)
type Option func(*Options)
// Options configure network
type Options struct {
// Id of the node
Id string
// 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
// Tunnel is network tunnel
Tunnel tunnel.Tunnel
// Router is network router
Router router.Router
// Proxy is network proxy
Proxy proxy.Proxy
// Resolver is network resolver
Resolver resolver.Resolver
}
// 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
func Name(n string) Option {
return func(o *Options) {
o.Name = n
}
}
// Address sets the network address
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 {
return func(o *Options) {
2019-12-08 02:28:39 +03:00
o.Nodes = n
}
}
// 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
}
}
// 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{
Id: uuid.New().String(),
Name: "go.micro",
Address: ":0",
2020-08-10 19:31:21 +03:00
Tunnel: tmucp.NewTunnel(),
Router: regRouter.NewRouter(),
Proxy: mucp.NewProxy(),
Resolver: new(noop.Resolver),
}
}