micro/options.go

125 lines
2.2 KiB
Go
Raw Normal View History

2015-12-21 02:50:16 +03:00
package gomicro
import (
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/client"
2016-01-01 04:16:21 +03:00
"github.com/micro/go-micro/cmd"
2015-12-21 02:50:16 +03:00
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/server"
"github.com/micro/go-micro/transport"
)
type Options struct {
Broker broker.Broker
2016-01-01 04:16:21 +03:00
Cmd cmd.Cmd
2015-12-21 02:50:16 +03:00
Client client.Client
Server server.Server
Registry registry.Registry
Transport transport.Transport
2016-01-01 04:16:21 +03:00
// Before and After funcs
BeforeStart []func() error
AfterStop []func() error
2015-12-21 02:50:16 +03:00
2016-01-01 04:16:21 +03:00
// Alternative options for those implementing the interface
Options map[string]string
}
2015-12-21 02:50:16 +03:00
2016-01-01 04:16:21 +03:00
func newOptions(opts ...Option) Options {
opt := Options{
Broker: broker.DefaultBroker,
Client: client.DefaultClient,
Server: server.DefaultServer,
Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport,
Options: map[string]string{},
2015-12-21 02:50:16 +03:00
}
2016-01-01 04:16:21 +03:00
for _, o := range opts {
o(&opt)
2015-12-21 02:50:16 +03:00
}
2016-01-01 05:45:15 +03:00
// New Command
if opt.Cmd == nil {
opt.Cmd = cmd.NewCmd(
cmd.Name(opt.Server.Options().Name),
cmd.Version(opt.Server.Options().Version),
)
}
2015-12-21 02:50:16 +03:00
return opt
}
2015-12-21 04:13:29 +03:00
func Broker(b broker.Broker) Option {
return func(o *Options) {
o.Broker = b
}
}
2016-01-01 04:16:21 +03:00
func Cmd(c cmd.Cmd) Option {
return func(o *Options) {
o.Cmd = c
}
}
2015-12-21 04:13:29 +03:00
func Client(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
func Server(s server.Server) Option {
return func(o *Options) {
o.Server = s
}
}
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
}
}
func Transport(t transport.Transport) Option {
return func(o *Options) {
o.Transport = t
}
}
2016-01-01 04:16:21 +03:00
// Convenience options
// Name of the service
func Name(n string) Option {
return func(o *Options) {
o.Server.Init(server.Name(n))
}
}
// Version of the service
func Version(v string) Option {
return func(o *Options) {
o.Server.Init(server.Version(v))
}
}
// Metadata associated with the service
func Metadata(md map[string]string) Option {
return func(o *Options) {
o.Server.Init(server.Metadata(md))
}
}
// Before and Afters
func BeforeStart(fn func() error) Option {
return func(o *Options) {
o.BeforeStart = append(o.BeforeStart, fn)
}
}
func AfterStop(fn func() error) Option {
return func(o *Options) {
o.AfterStop = append(o.AfterStop, fn)
}
}