2020-08-28 10:57:42 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/auth"
|
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/config"
|
|
|
|
"github.com/unistack-org/micro/v3/debug/profile"
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-11-18 16:50:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2020-08-28 10:57:42 +03:00
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
"github.com/unistack-org/micro/v3/runtime"
|
|
|
|
"github.com/unistack-org/micro/v3/selector"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
"github.com/unistack-org/micro/v3/store"
|
2020-09-10 00:06:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2020-08-28 10:57:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options for micro service
|
|
|
|
type Options struct {
|
2020-12-03 22:58:05 +03:00
|
|
|
Auth auth.Auth
|
|
|
|
Broker broker.Broker
|
|
|
|
Logger logger.Logger
|
|
|
|
Configs []config.Config
|
|
|
|
Client client.Client
|
|
|
|
Server server.Server
|
|
|
|
Store store.Store
|
|
|
|
Registry registry.Registry
|
|
|
|
Router router.Router
|
|
|
|
Runtime runtime.Runtime
|
|
|
|
Profile profile.Profile
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
// Before and After funcs
|
2020-12-03 22:58:05 +03:00
|
|
|
BeforeStart []func(context.Context) error
|
|
|
|
BeforeStop []func(context.Context) error
|
|
|
|
AfterStart []func(context.Context) error
|
|
|
|
AfterStop []func(context.Context) error
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewOptions returns new Options filled with defaults and overrided by provided opts
|
2020-10-28 17:48:39 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2020-12-03 22:58:05 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
Server: server.DefaultServer,
|
|
|
|
Client: client.DefaultClient,
|
|
|
|
Broker: broker.DefaultBroker,
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
Router: router.DefaultRouter,
|
|
|
|
Auth: auth.DefaultAuth,
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Configs: []config.Config{config.DefaultConfig},
|
|
|
|
Store: store.DefaultStore,
|
2020-08-29 17:44:49 +03:00
|
|
|
//Runtime runtime.Runtime
|
|
|
|
//Profile profile.Profile
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2020-10-28 17:48:39 +03:00
|
|
|
o(&options)
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 17:48:39 +03:00
|
|
|
return options
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Option func
|
2020-08-28 11:27:45 +03:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
// Broker to be used for service
|
|
|
|
func Broker(b broker.Broker) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Broker = b
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Client != nil {
|
|
|
|
// Update Client and Server
|
|
|
|
o.Client.Init(client.Broker(b))
|
|
|
|
}
|
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Broker(b))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client to be used for service
|
|
|
|
func Client(c client.Client) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Client = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
func Server(s server.Server) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store sets the store to use
|
|
|
|
func Store(s store.Store) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Store = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger set the logger to use
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
// Registry sets the registry for the service
|
|
|
|
// and the underlying components
|
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Registry = r
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Router != nil {
|
|
|
|
// Update router
|
|
|
|
o.Router.Init(router.Registry(r))
|
|
|
|
}
|
|
|
|
if o.Server != nil {
|
|
|
|
// Update server
|
|
|
|
o.Server.Init(server.Registry(r))
|
|
|
|
}
|
|
|
|
if o.Broker != nil {
|
|
|
|
// Update Broker
|
|
|
|
o.Broker.Init(broker.Registry(r))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracer sets the tracer for the service
|
2020-09-10 00:06:29 +03:00
|
|
|
func Tracer(t tracer.Tracer) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
//todo client trace
|
|
|
|
o.Server.Init(server.Tracer(t))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth sets the auth for the service
|
|
|
|
func Auth(a auth.Auth) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Auth = a
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Auth(a))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 22:58:05 +03:00
|
|
|
// Configs sets the configs for the service
|
|
|
|
func Configs(c []config.Config) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
2020-12-03 22:58:05 +03:00
|
|
|
o.Configs = c
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selector sets the selector for the service client
|
|
|
|
func Selector(s selector.Selector) Option {
|
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Client != nil {
|
|
|
|
o.Client.Init(client.Selector(s))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runtime sets the runtime
|
|
|
|
func Runtime(r runtime.Runtime) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Runtime = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Router sets the router
|
|
|
|
func Router(r router.Router) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Router = r
|
|
|
|
// Update client
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Client != nil {
|
|
|
|
o.Client.Init(client.Router(r))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address sets the address of the server
|
|
|
|
func Address(addr string) Option {
|
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Address(addr))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the service
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Name(n))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version of the service
|
|
|
|
func Version(v string) Option {
|
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Version(v))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata associated with the service
|
2020-11-18 16:50:41 +03:00
|
|
|
func Metadata(md metadata.Metadata) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Metadata(md))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterTTL specifies the TTL to use when registering the service
|
|
|
|
func RegisterTTL(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.RegisterTTL(t))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterInterval specifies the interval on which to re-register
|
|
|
|
func RegisterInterval(t time.Duration) Option {
|
|
|
|
return func(o *Options) {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.RegisterInterval(t))
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapClient is a convenience method for wrapping a Client with
|
|
|
|
// some middleware component. A list of wrappers can be provided.
|
|
|
|
// 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-- {
|
|
|
|
o.Client = w[i-1](o.Client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeStart run funcs before service starts
|
2020-12-03 22:58:05 +03:00
|
|
|
func BeforeStart(fn func(context.Context) error) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeStart = append(o.BeforeStart, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeStop run funcs before service stops
|
2020-12-03 22:58:05 +03:00
|
|
|
func BeforeStop(fn func(context.Context) error) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.BeforeStop = append(o.BeforeStop, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterStart run funcs after service starts
|
2020-12-03 22:58:05 +03:00
|
|
|
func AfterStart(fn func(context.Context) error) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterStart = append(o.AfterStart, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterStop run funcs after service stops
|
2020-12-03 22:58:05 +03:00
|
|
|
func AfterStop(fn func(context.Context) error) Option {
|
2020-08-28 10:57:42 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.AfterStop = append(o.AfterStop, fn)
|
|
|
|
}
|
|
|
|
}
|