2016-01-02 19:14:56 +00:00
|
|
|
package micro
|
2015-12-20 23:50:16 +00:00
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2016-01-26 23:32:27 +00:00
|
|
|
"time"
|
|
|
|
|
2016-01-02 19:12:17 +00:00
|
|
|
"github.com/micro/cli"
|
2015-12-20 23:50:16 +00:00
|
|
|
"github.com/micro/go-micro/broker"
|
|
|
|
"github.com/micro/go-micro/client"
|
2019-06-21 17:20:41 +01:00
|
|
|
"github.com/micro/go-micro/client/selector"
|
2019-06-21 13:36:11 +01:00
|
|
|
"github.com/micro/go-micro/config/cmd"
|
2015-12-20 23:50:16 +00:00
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
"github.com/micro/go-micro/server"
|
2019-07-10 07:47:17 +01:00
|
|
|
"github.com/micro/go-micro/transport"
|
2015-12-20 23:50:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Broker broker.Broker
|
2016-01-01 01:16:21 +00:00
|
|
|
Cmd cmd.Cmd
|
2015-12-20 23:50:16 +00:00
|
|
|
Client client.Client
|
|
|
|
Server server.Server
|
|
|
|
Registry registry.Registry
|
|
|
|
Transport transport.Transport
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
// Before and After funcs
|
|
|
|
BeforeStart []func() error
|
2017-01-07 14:52:23 +00:00
|
|
|
BeforeStop []func() error
|
|
|
|
AfterStart []func() error
|
2016-01-01 01:16:21 +00:00
|
|
|
AfterStop []func() error
|
2015-12-20 23:50:16 +00:00
|
|
|
|
2016-01-06 16:25:12 +00:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2019-11-16 03:13:34 -08:00
|
|
|
|
|
|
|
Signal bool
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
2015-12-20 23:50:16 +00:00
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
func newOptions(opts ...Option) Options {
|
|
|
|
opt := Options{
|
|
|
|
Broker: broker.DefaultBroker,
|
2016-01-02 00:38:57 +00:00
|
|
|
Cmd: cmd.DefaultCmd,
|
2016-01-01 01:16:21 +00:00
|
|
|
Client: client.DefaultClient,
|
|
|
|
Server: server.DefaultServer,
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
Transport: transport.DefaultTransport,
|
2017-01-07 14:52:23 +00:00
|
|
|
Context: context.Background(),
|
2019-11-16 03:13:34 -08:00
|
|
|
Signal: true,
|
2015-12-20 23:50:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
2015-12-20 23:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
2015-12-21 01:13:29 +00:00
|
|
|
|
|
|
|
func Broker(b broker.Broker) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Broker = b
|
2016-04-18 21:41:03 +01:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Broker(b))
|
|
|
|
o.Server.Init(server.Broker(b))
|
2015-12-21 01:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
func Cmd(c cmd.Cmd) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Cmd = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 01:13:29 +00:00
|
|
|
func Client(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 15:05:51 +00:00
|
|
|
// Context specifies a context for the service.
|
|
|
|
// Can be used to signal shutdown of the service.
|
|
|
|
// Can be used for extra option values.
|
2017-01-07 14:52:23 +00:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 03:13:34 -08:00
|
|
|
// HandleSignal toggles automatic installation of the signal handler that
|
|
|
|
// traps TERM, INT, and QUIT. Users of this feature to disable the signal
|
|
|
|
// handler, should control liveness of the service through the context.
|
|
|
|
func HandleSignal(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Signal = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 01:13:29 +00:00
|
|
|
func Server(s server.Server) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:41:03 +01:00
|
|
|
// Registry sets the registry for the service
|
|
|
|
// and the underlying components
|
2015-12-21 01:13:29 +00:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Registry = r
|
2016-04-18 21:41:03 +01: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))
|
2018-04-17 08:30:36 +01:00
|
|
|
// Update Broker
|
|
|
|
o.Broker.Init(broker.Registry(r))
|
2015-12-21 01:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 21:43:24 +00:00
|
|
|
// Selector sets the selector for the service client
|
|
|
|
func Selector(s selector.Selector) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client.Init(client.Selector(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:41:03 +01:00
|
|
|
// Transport sets the transport for the service
|
|
|
|
// and the underlying components
|
2015-12-21 01:13:29 +00:00
|
|
|
func Transport(t transport.Transport) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Transport = t
|
2016-04-18 21:41:03 +01:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Transport(t))
|
|
|
|
o.Server.Init(server.Transport(t))
|
2015-12-21 01:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-01 01:16:21 +00:00
|
|
|
|
|
|
|
// Convenience options
|
|
|
|
|
2019-02-11 18:37:25 +00:00
|
|
|
// Address sets the address of the server
|
|
|
|
func Address(addr string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(server.Address(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
// 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 19:14:56 +00:00
|
|
|
// Metadata associated with the service
|
|
|
|
func Metadata(md map[string]string) Option {
|
2016-01-02 19:12:17 +00:00
|
|
|
return func(o *Options) {
|
2016-01-02 19:14:56 +00:00
|
|
|
o.Server.Init(server.Metadata(md))
|
2016-01-02 19:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 19:14:56 +00:00
|
|
|
func Flags(flags ...cli.Flag) Option {
|
2016-01-02 19:12:17 +00:00
|
|
|
return func(o *Options) {
|
2016-01-02 19:14:56 +00:00
|
|
|
o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
|
2016-01-02 19:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-02 19:14:56 +00:00
|
|
|
func Action(a func(*cli.Context)) Option {
|
2016-01-01 01:16:21 +00:00
|
|
|
return func(o *Options) {
|
2016-01-02 19:14:56 +00:00
|
|
|
o.Cmd.App().Action = a
|
2016-01-01 01:16:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:51:34 +00:00
|
|
|
// RegisterTTL specifies the TTL to use when registering the service
|
2016-01-26 23:32:27 +00:00
|
|
|
func RegisterTTL(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2016-01-27 12:23:18 +00:00
|
|
|
o.Server.Init(server.RegisterTTL(t))
|
2016-01-26 23:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:51:34 +00:00
|
|
|
// RegisterInterval specifies the interval on which to re-register
|
2016-01-26 23:32:27 +00:00
|
|
|
func RegisterInterval(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2019-01-24 13:22:17 +00:00
|
|
|
o.Server.Init(server.RegisterInterval(t))
|
2016-01-26 23:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:41:03 +01:00
|
|
|
// WrapClient is a convenience method for wrapping a Client with
|
|
|
|
// some middleware component. A list of wrappers can be provided.
|
2018-12-01 11:10:37 +00:00
|
|
|
// Wrappers are applied in reverse order so the last is executed first.
|
2016-04-18 21:41:03 +01:00
|
|
|
func WrapClient(w ...client.Wrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
// apply in reverse
|
|
|
|
for i := len(w); i > 0; i-- {
|
2016-04-19 00:47:15 +01:00
|
|
|
o.Client = w[i-1](o.Client)
|
2016-04-18 21:41:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 17:01:53 +00:00
|
|
|
// WrapCall is a convenience method for wrapping a Client CallFunc
|
|
|
|
func WrapCall(w ...client.CallWrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client.Init(client.WrapCall(w...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 21:41:03 +01: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 01:16:21 +00:00
|
|
|
// Before and Afters
|
|
|
|
|
|
|
|
func BeforeStart(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeStart = append(o.BeforeStart, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 14:52:23 +00:00
|
|
|
func BeforeStop(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeStop = append(o.BeforeStop, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AfterStart(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterStart = append(o.AfterStart, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 01:16:21 +00:00
|
|
|
func AfterStop(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterStop = append(o.AfterStop, fn)
|
|
|
|
}
|
|
|
|
}
|