2016-01-02 21:14:56 +02:00
|
|
|
package micro
|
2015-12-21 01:50:16 +02:00
|
|
|
|
|
|
|
import (
|
2018-03-03 13:53:52 +02:00
|
|
|
"context"
|
2016-01-27 01:32:27 +02:00
|
|
|
"time"
|
|
|
|
|
2021-11-08 10:52:39 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/auth"
|
|
|
|
"go-micro.dev/v4/broker"
|
|
|
|
"go-micro.dev/v4/cache"
|
|
|
|
"go-micro.dev/v4/client"
|
|
|
|
"go-micro.dev/v4/config"
|
|
|
|
"go-micro.dev/v4/debug/profile"
|
|
|
|
"go-micro.dev/v4/debug/trace"
|
2022-09-29 16:44:53 +02:00
|
|
|
"go-micro.dev/v4/logger"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/registry"
|
|
|
|
"go-micro.dev/v4/runtime"
|
|
|
|
"go-micro.dev/v4/selector"
|
|
|
|
"go-micro.dev/v4/server"
|
|
|
|
"go-micro.dev/v4/store"
|
|
|
|
"go-micro.dev/v4/transport"
|
2022-07-11 15:37:34 +02:00
|
|
|
"go-micro.dev/v4/util/cmd"
|
2015-12-21 01:50:16 +02:00
|
|
|
)
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Options for micro service.
|
2015-12-21 01:50:16 +02:00
|
|
|
type Options struct {
|
2023-04-26 02:16:34 +02:00
|
|
|
Registry registry.Registry
|
|
|
|
Store store.Store
|
|
|
|
Auth auth.Auth
|
|
|
|
Cmd cmd.Cmd
|
|
|
|
Config config.Config
|
|
|
|
Client client.Client
|
|
|
|
Server server.Server
|
|
|
|
|
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
|
|
|
|
2021-08-31 16:31:16 +02:00
|
|
|
Cache cache.Cache
|
2020-05-11 18:09:28 +02:00
|
|
|
Runtime runtime.Runtime
|
2020-02-25 18:42:43 +02:00
|
|
|
Profile profile.Profile
|
2023-04-26 02:16:34 +02:00
|
|
|
Transport transport.Transport
|
2022-09-29 16:44:53 +02:00
|
|
|
Logger logger.Logger
|
2023-04-26 02:16:34 +02:00
|
|
|
Broker broker.Broker
|
2016-01-01 03:16:21 +02:00
|
|
|
// Before and After funcs
|
|
|
|
BeforeStart []func() error
|
2017-01-07 16:52:23 +02:00
|
|
|
AfterStart []func() error
|
2016-01-01 03:16:21 +02:00
|
|
|
AfterStop []func() error
|
2015-12-21 01:50:16 +02:00
|
|
|
|
2023-04-26 02:16:34 +02:00
|
|
|
BeforeStop []func() error
|
2019-11-16 13:13:34 +02:00
|
|
|
|
|
|
|
Signal bool
|
2016-01-01 03:16:21 +02:00
|
|
|
}
|
2015-12-21 01:50:16 +02:00
|
|
|
|
2016-01-01 03:16:21 +02:00
|
|
|
func newOptions(opts ...Option) Options {
|
|
|
|
opt := Options{
|
2020-02-10 10:26:28 +02:00
|
|
|
Auth: auth.DefaultAuth,
|
2016-01-01 03:16:21 +02:00
|
|
|
Broker: broker.DefaultBroker,
|
2021-08-31 16:31:16 +02:00
|
|
|
Cache: cache.DefaultCache,
|
2016-01-02 02:38:57 +02:00
|
|
|
Cmd: cmd.DefaultCmd,
|
2020-03-12 20:13:03 +02:00
|
|
|
Config: config.DefaultConfig,
|
2016-01-01 03:16:21 +02:00
|
|
|
Client: client.DefaultClient,
|
|
|
|
Server: server.DefaultServer,
|
2020-05-01 19:05:09 +02:00
|
|
|
Store: store.DefaultStore,
|
2016-01-01 03:16:21 +02:00
|
|
|
Registry: registry.DefaultRegistry,
|
2020-05-11 18:09:28 +02:00
|
|
|
Runtime: runtime.DefaultRuntime,
|
2016-01-01 03:16:21 +02:00
|
|
|
Transport: transport.DefaultTransport,
|
2017-01-07 16:52:23 +02:00
|
|
|
Context: context.Background(),
|
2019-11-16 13:13:34 +02:00
|
|
|
Signal: true,
|
2022-09-29 16:44:53 +02:00
|
|
|
Logger: logger.DefaultLogger,
|
2015-12-21 01:50:16 +02:00
|
|
|
}
|
|
|
|
|
2016-01-01 03:16:21 +02:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
2015-12-21 01:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
2015-12-21 03:13:29 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Broker to be used for service.
|
2015-12-21 03:13:29 +02:00
|
|
|
func Broker(b broker.Broker) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Broker = b
|
2016-04-18 22:41:03 +02:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Broker(b))
|
|
|
|
o.Server.Init(server.Broker(b))
|
2015-12-21 03:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:31:16 +02:00
|
|
|
func Cache(c cache.Cache) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Cache = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 03:16:21 +02:00
|
|
|
func Cmd(c cmd.Cmd) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Cmd = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Client to be used for service.
|
2015-12-21 03:13:29 +02:00
|
|
|
func Client(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 17:05:51 +02:00
|
|
|
// Context specifies a context for the service.
|
2020-02-07 23:48:24 +02:00
|
|
|
// Can be used to signal shutdown of the service and for extra option values.
|
2017-01-07 16:52:23 +02:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:29:03 +02:00
|
|
|
// Handle will register a handler without any fuss
|
|
|
|
func Handle(v interface{}) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Handle(
|
|
|
|
o.Server.NewHandler(v),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 13:13:34 +02: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Profile to be used for debug profile.
|
2020-02-25 18:42:43 +02:00
|
|
|
func Profile(p profile.Profile) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Profile = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Server to be used for service.
|
2015-12-21 03:13:29 +02:00
|
|
|
func Server(s server.Server) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Store sets the store to use.
|
2020-05-01 19:05:09 +02:00
|
|
|
func Store(s store.Store) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Store = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:41:03 +02:00
|
|
|
// Registry sets the registry for the service
|
2022-09-30 16:27:07 +02:00
|
|
|
// and the underlying components.
|
2015-12-21 03:13:29 +02:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Registry = r
|
2016-04-18 22:41:03 +02:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Registry(r))
|
|
|
|
o.Server.Init(server.Registry(r))
|
2018-04-17 09:30:36 +02:00
|
|
|
// Update Broker
|
|
|
|
o.Broker.Init(broker.Registry(r))
|
2015-12-21 03:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Tracer sets the tracer for the service.
|
2020-01-29 17:45:11 +02:00
|
|
|
func Tracer(t trace.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(server.Tracer(t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Auth sets the auth for the service.
|
2020-02-03 10:16:02 +02:00
|
|
|
func Auth(a auth.Auth) Option {
|
|
|
|
return func(o *Options) {
|
2020-02-10 10:26:28 +02:00
|
|
|
o.Auth = a
|
2020-02-03 10:16:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Config sets the config for the service.
|
2020-03-12 20:13:03 +02:00
|
|
|
func Config(c config.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Config = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Selector sets the selector for the service client.
|
2017-12-20 23:43:24 +02:00
|
|
|
func Selector(s selector.Selector) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client.Init(client.Selector(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:41:03 +02:00
|
|
|
// Transport sets the transport for the service
|
2022-09-30 16:27:07 +02:00
|
|
|
// and the underlying components.
|
2015-12-21 03:13:29 +02:00
|
|
|
func Transport(t transport.Transport) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Transport = t
|
2016-04-18 22:41:03 +02:00
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Transport(t))
|
|
|
|
o.Server.Init(server.Transport(t))
|
2015-12-21 03:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-01 03:16:21 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Runtime sets the runtime.
|
2020-05-11 18:09:28 +02:00
|
|
|
func Runtime(r runtime.Runtime) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Runtime = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 03:16:21 +02:00
|
|
|
// Convenience options
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Address sets the address of the server.
|
2019-02-11 20:37:25 +02:00
|
|
|
func Address(addr string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(server.Address(addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Name of the service.
|
2016-01-01 03:16:21 +02:00
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(server.Name(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Version of the service.
|
2016-01-01 03:16:21 +02:00
|
|
|
func Version(v string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(server.Version(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Metadata associated with the service.
|
2016-01-02 21:14:56 +02:00
|
|
|
func Metadata(md map[string]string) Option {
|
2016-01-02 21:12:17 +02:00
|
|
|
return func(o *Options) {
|
2016-01-02 21:14:56 +02:00
|
|
|
o.Server.Init(server.Metadata(md))
|
2016-01-02 21:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Flags that can be passed to service.
|
2016-01-02 21:14:56 +02:00
|
|
|
func Flags(flags ...cli.Flag) Option {
|
2016-01-02 21:12:17 +02:00
|
|
|
return func(o *Options) {
|
2016-01-02 21:14:56 +02:00
|
|
|
o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
|
2016-01-02 21:12:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Action can be used to parse user provided cli options.
|
2020-01-25 12:41:25 +02:00
|
|
|
func Action(a func(*cli.Context) error) Option {
|
2016-01-01 03:16:21 +02:00
|
|
|
return func(o *Options) {
|
2016-01-02 21:14:56 +02:00
|
|
|
o.Cmd.App().Action = a
|
2016-01-01 03:16:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// RegisterTTL specifies the TTL to use when registering the service.
|
2016-01-27 01:32:27 +02:00
|
|
|
func RegisterTTL(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2016-01-27 14:23:18 +02:00
|
|
|
o.Server.Init(server.RegisterTTL(t))
|
2016-01-27 01:32:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// RegisterInterval specifies the interval on which to re-register.
|
2016-01-27 01:32:27 +02:00
|
|
|
func RegisterInterval(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2019-01-24 15:22:17 +02:00
|
|
|
o.Server.Init(server.RegisterInterval(t))
|
2016-01-27 01:32:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:41:03 +02:00
|
|
|
// WrapClient is a convenience method for wrapping a Client with
|
|
|
|
// some middleware component. A list of wrappers can be provided.
|
2018-12-01 13:10:37 +02:00
|
|
|
// Wrappers are applied in reverse order so the last is executed first.
|
2016-04-18 22:41:03 +02:00
|
|
|
func WrapClient(w ...client.Wrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
// apply in reverse
|
|
|
|
for i := len(w); i > 0; i-- {
|
2016-04-19 01:47:15 +02:00
|
|
|
o.Client = w[i-1](o.Client)
|
2016-04-18 22:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WrapCall is a convenience method for wrapping a Client CallFunc.
|
2016-12-06 19:01:53 +02:00
|
|
|
func WrapCall(w ...client.CallWrapper) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client.Init(client.WrapCall(w...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WrapHandler adds a handler Wrapper to a list of options passed into the server.
|
2016-04-18 22:41:03 +02:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server.
|
2016-04-18 22:41:03 +02:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Add opt to server option.
|
2022-07-30 11:56:14 +02:00
|
|
|
func AddListenOption(option server.Option) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 03:16:21 +02:00
|
|
|
// Before and Afters
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// BeforeStart run funcs before service starts.
|
2016-01-01 03:16:21 +02:00
|
|
|
func BeforeStart(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeStart = append(o.BeforeStart, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// BeforeStop run funcs before service stops.
|
2017-01-07 16:52:23 +02:00
|
|
|
func BeforeStop(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeStop = append(o.BeforeStop, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// AfterStart run funcs after service starts.
|
2017-01-07 16:52:23 +02:00
|
|
|
func AfterStart(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterStart = append(o.AfterStart, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// AfterStop run funcs after service stops.
|
2016-01-01 03:16:21 +02:00
|
|
|
func AfterStop(fn func() error) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterStop = append(o.AfterStop, fn)
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 16:44:53 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// Logger sets the logger for the service.
|
2022-09-29 16:44:53 +02:00
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|