Update top level init
This commit is contained in:
83
options.go
83
options.go
@@ -3,6 +3,7 @@ package gomicro
|
||||
import (
|
||||
"github.com/micro/go-micro/broker"
|
||||
"github.com/micro/go-micro/client"
|
||||
"github.com/micro/go-micro/cmd"
|
||||
"github.com/micro/go-micro/registry"
|
||||
"github.com/micro/go-micro/server"
|
||||
"github.com/micro/go-micro/transport"
|
||||
@@ -10,38 +11,35 @@ import (
|
||||
|
||||
type Options struct {
|
||||
Broker broker.Broker
|
||||
Cmd cmd.Cmd
|
||||
Client client.Client
|
||||
Server server.Server
|
||||
Registry registry.Registry
|
||||
Transport transport.Transport
|
||||
|
||||
// Before and After funcs
|
||||
BeforeStart []func() error
|
||||
AfterStop []func() error
|
||||
|
||||
// Alternative options for those implementing the interface
|
||||
Options map[string]string
|
||||
}
|
||||
|
||||
func newOptions(opts ...Option) Options {
|
||||
var opt Options
|
||||
opt := Options{
|
||||
Broker: broker.DefaultBroker,
|
||||
Cmd: cmd.DefaultCmd,
|
||||
Client: client.DefaultClient,
|
||||
Server: server.DefaultServer,
|
||||
Registry: registry.DefaultRegistry,
|
||||
Transport: transport.DefaultTransport,
|
||||
Options: map[string]string{},
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&opt)
|
||||
}
|
||||
|
||||
if opt.Broker == nil {
|
||||
opt.Broker = broker.DefaultBroker
|
||||
}
|
||||
|
||||
if opt.Client == nil {
|
||||
opt.Client = client.DefaultClient
|
||||
}
|
||||
|
||||
if opt.Server == nil {
|
||||
opt.Server = server.DefaultServer
|
||||
}
|
||||
|
||||
if opt.Registry == nil {
|
||||
opt.Registry = registry.DefaultRegistry
|
||||
}
|
||||
|
||||
if opt.Transport == nil {
|
||||
opt.Transport = transport.DefaultTransport
|
||||
}
|
||||
|
||||
return opt
|
||||
}
|
||||
|
||||
@@ -51,6 +49,12 @@ func Broker(b broker.Broker) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func Cmd(c cmd.Cmd) Option {
|
||||
return func(o *Options) {
|
||||
o.Cmd = c
|
||||
}
|
||||
}
|
||||
|
||||
func Client(c client.Client) Option {
|
||||
return func(o *Options) {
|
||||
o.Client = c
|
||||
@@ -74,3 +78,40 @@ func Transport(t transport.Transport) Option {
|
||||
o.Transport = t
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user