micro/options.go

308 lines
6.9 KiB
Go
Raw Permalink Normal View History

2016-01-02 22:14:56 +03:00
package micro
2015-12-21 02:50:16 +03:00
import (
2018-03-03 14:53:52 +03:00
"context"
2016-01-27 02:32:27 +03:00
"time"
2020-01-29 18:45:11 +03:00
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/config"
"github.com/micro/go-micro/v2/config/cmd"
"github.com/micro/go-micro/v2/debug/profile"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/transport"
2015-12-21 02:50:16 +03:00
)
// Options for micro service
2015-12-21 02:50:16 +03:00
type Options struct {
Auth auth.Auth
2015-12-21 02:50:16 +03:00
Broker broker.Broker
2016-01-01 04:16:21 +03:00
Cmd cmd.Cmd
Config config.Config
2015-12-21 02:50:16 +03:00
Client client.Client
Server server.Server
Registry registry.Registry
Transport transport.Transport
Profile profile.Profile
2015-12-21 02:50:16 +03:00
2016-01-01 04:16:21 +03:00
// Before and After funcs
BeforeStart []func() error
BeforeStop []func() error
AfterStart []func() error
2016-01-01 04:16:21 +03:00
AfterStop []func() error
2015-12-21 02:50:16 +03:00
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
Signal bool
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{
Auth: auth.DefaultAuth,
2016-01-01 04:16:21 +03:00
Broker: broker.DefaultBroker,
2016-01-02 03:38:57 +03:00
Cmd: cmd.DefaultCmd,
Config: config.DefaultConfig,
2016-01-01 04:16:21 +03:00
Client: client.DefaultClient,
Server: server.DefaultServer,
Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport,
Context: context.Background(),
Signal: true,
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
// Broker to be used for service
2015-12-21 04:13:29 +03:00
func Broker(b broker.Broker) Option {
return func(o *Options) {
o.Broker = b
// 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
}
}
// Client to be used for service
2015-12-21 04:13:29 +03:00
func Client(c client.Client) Option {
return func(o *Options) {
o.Client = c
}
}
2017-01-07 18:05:51 +03:00
// Context specifies a context for the service.
// Can be used to signal shutdown of the service and for extra option values.
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// 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
}
}
// Profile to be used for debug profile
func Profile(p profile.Profile) Option {
return func(o *Options) {
o.Profile = p
}
}
// Server to be used for service
2015-12-21 04:13:29 +03:00
func Server(s server.Server) Option {
return func(o *Options) {
o.Server = s
}
}
// 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
// Update Client and Server
o.Client.Init(client.Registry(r))
o.Server.Init(server.Registry(r))
2018-04-17 10:30:36 +03:00
// Update Broker
o.Broker.Init(broker.Registry(r))
2015-12-21 04:13:29 +03:00
}
}
2020-01-29 18:45:11 +03:00
// Tracer sets the tracer for the service
func Tracer(t trace.Tracer) Option {
return func(o *Options) {
o.Server.Init(server.Tracer(t))
}
}
// Auth sets the auth for the service
func Auth(a auth.Auth) Option {
return func(o *Options) {
o.Auth = a
o.Server.Init(server.Auth(a))
}
}
// Config sets the config for the service
func Config(c config.Config) Option {
return func(o *Options) {
o.Config = c
}
}
2017-12-21 00:43:24 +03: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))
}
}
// 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
// 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
2019-02-11 21:37:25 +03: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 04:16:21 +03: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 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
}
}
// Flags that can be passed to service
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
}
}
// Action can be used to parse user provided cli options
func Action(a func(*cli.Context) error) 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
}
}
2017-01-08 01:51:34 +03:00
// RegisterTTL specifies the TTL to use when registering the service
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
}
}
2017-01-08 01:51:34 +03:00
// RegisterInterval specifies the interval on which to re-register
2016-01-27 02:32:27 +03:00
func RegisterInterval(t time.Duration) Option {
return func(o *Options) {
2019-01-24 16:22:17 +03:00
o.Server.Init(server.RegisterInterval(t))
2016-01-27 02:32:27 +03:00
}
}
// WrapClient is a convenience method for wrapping a Client with
// some middleware component. A list of wrappers can be provided.
2018-12-01 14:10:37 +03:00
// Wrappers are applied in reverse order so the last is executed first.
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-12-06 20:01:53 +03: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...))
}
}
// 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
// BeforeStart run funcs before service starts
2016-01-01 04:16:21 +03:00
func BeforeStart(fn func() error) Option {
return func(o *Options) {
o.BeforeStart = append(o.BeforeStart, fn)
}
}
// BeforeStop run funcs before service stops
func BeforeStop(fn func() error) Option {
return func(o *Options) {
o.BeforeStop = append(o.BeforeStop, fn)
}
}
// AfterStart run funcs after service starts
func AfterStart(fn func() error) Option {
return func(o *Options) {
o.AfterStart = append(o.AfterStart, fn)
}
}
// AfterStop run funcs after service stops
2016-01-01 04:16:21 +03:00
func AfterStop(fn func() error) Option {
return func(o *Options) {
o.AfterStop = append(o.AfterStop, fn)
}
}