2016-01-02 22:14:56 +03:00
|
|
|
package micro
|
2015-12-21 02:50:16 +03:00
|
|
|
|
|
|
|
import (
|
2016-01-27 02:32:27 +03:00
|
|
|
"time"
|
|
|
|
|
2016-01-02 22:12:17 +03:00
|
|
|
"github.com/micro/cli"
|
2015-12-21 02:50:16 +03:00
|
|
|
"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"
|
2016-04-18 23:41:03 +03:00
|
|
|
"github.com/micro/go-micro/selector"
|
2015-12-21 02:50:16 +03:00
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
"github.com/micro/go-micro/transport"
|
2016-01-06 19:25:12 +03:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2015-12-21 02:50:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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-27 15:23:18 +03:00
|
|
|
// Register loop interval
|
2016-01-27 02:32:27 +03:00
|
|
|
RegisterInterval time.Duration
|
|
|
|
|
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-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2016-01-01 04:16:21 +03:00
|
|
|
}
|
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,
|
2016-01-02 03:38:57 +03:00
|
|
|
Cmd: cmd.DefaultCmd,
|
2016-01-01 04:16:21 +03:00
|
|
|
Client: client.DefaultClient,
|
|
|
|
Server: server.DefaultServer,
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
Transport: transport.DefaultTransport,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
2015-12-21 04:13:29 +03:00
|
|
|
|
|
|
|
func Broker(b broker.Broker) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Broker = b
|
2016-04-18 23:41:03 +03:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Broker(b))
|
|
|
|
o.Server.Init(server.Broker(b))
|
2015-12-21 04:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 23:41:03 +03:00
|
|
|
// Registry sets the registry for the service
|
|
|
|
// and the underlying components
|
2015-12-21 04:13:29 +03:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Registry = r
|
2016-04-18 23:41:03 +03:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Registry(r))
|
|
|
|
o.Server.Init(server.Registry(r))
|
|
|
|
// Update Selector
|
|
|
|
o.Client.Options().Selector.Init(selector.Registry(r))
|
2015-12-21 04:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 23:41:03 +03:00
|
|
|
// Transport sets the transport for the service
|
|
|
|
// and the underlying components
|
2015-12-21 04:13:29 +03:00
|
|
|
func Transport(t transport.Transport) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Transport = t
|
2016-04-18 23:41:03 +03:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Transport(t))
|
|
|
|
o.Server.Init(server.Transport(t))
|
2015-12-21 04:13:29 +03:00
|
|
|
}
|
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 22:14:56 +03:00
|
|
|
// Metadata associated with the service
|
|
|
|
func Metadata(md map[string]string) Option {
|
2016-01-02 22:12:17 +03:00
|
|
|
return func(o *Options) {
|
2016-01-02 22:14:56 +03:00
|
|
|
o.Server.Init(server.Metadata(md))
|
2016-01-02 22:12:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 22:14:56 +03:00
|
|
|
func Flags(flags ...cli.Flag) Option {
|
2016-01-02 22:12:17 +03:00
|
|
|
return func(o *Options) {
|
2016-01-02 22:14:56 +03:00
|
|
|
o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
|
2016-01-02 22:12:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 22:14:56 +03:00
|
|
|
func Action(a func(*cli.Context)) Option {
|
2016-01-01 04:16:21 +03:00
|
|
|
return func(o *Options) {
|
2016-01-02 22:14:56 +03:00
|
|
|
o.Cmd.App().Action = a
|
2016-01-01 04:16:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 02:32:27 +03:00
|
|
|
func RegisterTTL(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2016-01-27 15:23:18 +03:00
|
|
|
o.Server.Init(server.RegisterTTL(t))
|
2016-01-27 02:32:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterInterval(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.RegisterInterval = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 23:41:03 +03:00
|
|
|
// WrapClient is a convenience method for wrapping a Client with
|
|
|
|
// some middleware component. A list of wrappers can be provided.
|
|
|
|
func WrapClient(w ...client.Wrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
// apply in reverse
|
|
|
|
for i := len(w); i > 0; i-- {
|
2016-04-19 02:47:15 +03:00
|
|
|
o.Client = w[i-1](o.Client)
|
2016-04-18 23:41:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapHandler adds a handler Wrapper to a list of options passed into the server
|
|
|
|
func WrapHandler(w ...server.HandlerWrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
var wrappers []server.Option
|
|
|
|
|
|
|
|
for _, wrap := range w {
|
|
|
|
wrappers = append(wrappers, server.WrapHandler(wrap))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init once
|
|
|
|
o.Server.Init(wrappers...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server
|
|
|
|
func WrapSubscriber(w ...server.SubscriberWrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
var wrappers []server.Option
|
|
|
|
|
|
|
|
for _, wrap := range w {
|
|
|
|
wrappers = append(wrappers, server.WrapSubscriber(wrap))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init once
|
|
|
|
o.Server.Init(wrappers...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 04:16:21 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|