2020-08-28 10:57:42 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-29 13:17:32 +03:00
|
|
|
"fmt"
|
2020-08-28 10:57:42 +03:00
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
"go.unistack.org/micro/v3/store"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2020-08-28 10:57:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options for micro service
|
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds external options or cancel stuff
|
|
|
|
Context context.Context
|
|
|
|
// Metadata holds service metadata
|
2021-01-29 13:17:32 +03:00
|
|
|
Metadata metadata.Metadata
|
2021-03-06 19:45:13 +03:00
|
|
|
// Version holds service version
|
|
|
|
Version string
|
|
|
|
// Name holds service name
|
|
|
|
Name string
|
|
|
|
// Brokers holds brokers
|
|
|
|
Brokers []broker.Broker
|
|
|
|
// Loggers holds loggers
|
|
|
|
Loggers []logger.Logger
|
|
|
|
// Meters holds meter
|
|
|
|
Meters []meter.Meter
|
|
|
|
// Configs holds config
|
|
|
|
Configs []config.Config
|
|
|
|
// Clients holds clients
|
|
|
|
Clients []client.Client
|
|
|
|
// Stores holds stores
|
|
|
|
Stores []store.Store
|
|
|
|
// Registers holds registers
|
2021-01-29 13:17:32 +03:00
|
|
|
Registers []register.Register
|
2021-03-06 19:45:13 +03:00
|
|
|
// Tracers holds tracers
|
|
|
|
Tracers []tracer.Tracer
|
|
|
|
// Routers holds routers
|
|
|
|
Routers []router.Router
|
|
|
|
// BeforeStart holds funcs that runs before service starts
|
2020-12-04 02:28:45 +03:00
|
|
|
BeforeStart []func(context.Context) error
|
2021-03-06 19:45:13 +03:00
|
|
|
// BeforeStop holds funcs that runs before service stops
|
|
|
|
BeforeStop []func(context.Context) error
|
|
|
|
// AfterStart holds funcs that runs after service starts
|
|
|
|
AfterStart []func(context.Context) error
|
|
|
|
// AfterStop holds funcs that runs after service stops
|
|
|
|
AfterStop []func(context.Context) error
|
|
|
|
// Servers holds servers
|
|
|
|
Servers []server.Server
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
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{
|
2021-01-29 13:17:32 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
Servers: []server.Server{server.DefaultServer},
|
|
|
|
Clients: []client.Client{client.DefaultClient},
|
|
|
|
Brokers: []broker.Broker{broker.DefaultBroker},
|
|
|
|
Registers: []register.Register{register.DefaultRegister},
|
|
|
|
Routers: []router.Router{router.DefaultRouter},
|
|
|
|
Loggers: []logger.Logger{logger.DefaultLogger},
|
|
|
|
Tracers: []tracer.Tracer{tracer.DefaultTracer},
|
|
|
|
Meters: []meter.Meter{meter.DefaultMeter},
|
|
|
|
Configs: []config.Config{config.DefaultConfig},
|
|
|
|
Stores: []store.Store{store.DefaultStore},
|
2021-04-27 08:32:47 +03:00
|
|
|
// Runtime runtime.Runtime
|
|
|
|
// Profile profile.Profile
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2021-02-13 01:46:16 +03:00
|
|
|
//nolint:errcheck
|
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
|
2021-01-29 13:17:32 +03:00
|
|
|
type Option func(*Options) error
|
|
|
|
|
|
|
|
// Broker to be used for client and server
|
|
|
|
func Broker(b broker.Broker, opts ...BrokerOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
bopts := brokerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&bopts)
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
|
|
|
}
|
|
|
|
for _, srv := range o.Servers {
|
|
|
|
for _, os := range bopts.servers {
|
|
|
|
if srv.Name() == os || all {
|
|
|
|
if err = srv.Init(server.Broker(b)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, cli := range o.Clients {
|
|
|
|
for _, oc := range bopts.clients {
|
|
|
|
if cli.Name() == oc || all {
|
|
|
|
if err = cli.Init(client.Broker(b)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
type brokerOptions struct {
|
|
|
|
servers []string
|
|
|
|
clients []string
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// BrokerOption func signature
|
2021-01-29 13:17:32 +03:00
|
|
|
type BrokerOption func(*brokerOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// BrokerClient specifies clients for broker
|
2021-01-29 13:17:32 +03:00
|
|
|
func BrokerClient(n string) BrokerOption {
|
|
|
|
return func(o *brokerOptions) {
|
|
|
|
o.clients = append(o.clients, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// BrokerServer specifies servers for broker
|
2021-01-29 13:17:32 +03:00
|
|
|
func BrokerServer(n string) BrokerOption {
|
|
|
|
return func(o *brokerOptions) {
|
|
|
|
o.servers = append(o.servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:37:12 +03:00
|
|
|
// Client to be used for service
|
|
|
|
func Client(c ...client.Client) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Clients = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Clients to be used for service
|
|
|
|
func Clients(c ...client.Client) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Clients = c
|
|
|
|
return nil
|
2020-08-28 10:57:42 +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 {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
|
|
|
// TODO: Pass context to underline stuff ?
|
2020-08-28 10:57:42 +03:00
|
|
|
o.Context = ctx
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
/*
|
2020-08-28 10:57:42 +03:00
|
|
|
// Profile to be used for debug profile
|
|
|
|
func Profile(p profile.Profile) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Profile = p
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
*/
|
2020-08-28 10:57:42 +03:00
|
|
|
|
2021-02-02 15:37:12 +03:00
|
|
|
// Server to be used for service
|
|
|
|
func Server(s ...server.Server) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Servers = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Servers to be used for service
|
|
|
|
func Servers(s ...server.Server) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Servers = s
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:37:12 +03:00
|
|
|
// Store sets the store to use
|
|
|
|
func Store(s ...store.Store) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Stores = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Stores sets the store to use
|
|
|
|
func Stores(s ...store.Store) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Stores = s
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger set the logger to use
|
2023-03-24 00:25:47 +03:00
|
|
|
//
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2021-01-29 13:17:32 +03:00
|
|
|
func Logger(l logger.Logger, opts ...LoggerOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
lopts := loggerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&lopts)
|
|
|
|
}
|
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
|
|
|
}
|
|
|
|
for _, srv := range o.Servers {
|
|
|
|
for _, os := range lopts.servers {
|
|
|
|
if srv.Name() == os || all {
|
|
|
|
if err = srv.Init(server.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, cli := range o.Clients {
|
|
|
|
for _, oc := range lopts.clients {
|
|
|
|
if cli.Name() == oc || all {
|
|
|
|
if err = cli.Init(client.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, brk := range o.Brokers {
|
|
|
|
for _, ob := range lopts.brokers {
|
|
|
|
if brk.Name() == ob || all {
|
|
|
|
if err = brk.Init(broker.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, reg := range o.Registers {
|
|
|
|
for _, or := range lopts.registers {
|
|
|
|
if reg.Name() == or || all {
|
|
|
|
if err = reg.Init(register.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, str := range o.Stores {
|
|
|
|
for _, or := range lopts.stores {
|
|
|
|
if str.Name() == or || all {
|
|
|
|
if err = str.Init(store.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 18:52:37 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, trc := range o.Tracers {
|
|
|
|
for _, ot := range lopts.tracers {
|
|
|
|
if trc.Name() == ot || all {
|
|
|
|
if err = trc.Init(tracer.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// LoggerOption func signature
|
2021-01-29 13:17:32 +03:00
|
|
|
type LoggerOption func(*loggerOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// loggerOptions
|
2021-01-29 13:17:32 +03:00
|
|
|
type loggerOptions struct {
|
|
|
|
servers []string
|
|
|
|
clients []string
|
|
|
|
brokers []string
|
|
|
|
registers []string
|
|
|
|
stores []string
|
2024-12-06 18:52:37 +03:00
|
|
|
// meters []string
|
|
|
|
tracers []string
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func LoggerServer(n string) LoggerOption {
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-02-02 15:37:12 +03:00
|
|
|
// Meter set the meter to use
|
|
|
|
func Meter(m ...meter.Meter) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Meters = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Meters set the meter to use
|
|
|
|
func Meters(m ...meter.Meter) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Meters = m
|
|
|
|
return nil
|
2021-01-22 23:32:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Register sets the register for the service
|
2020-08-28 10:57:42 +03:00
|
|
|
// and the underlying components
|
2023-03-24 00:25:47 +03:00
|
|
|
//
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2021-01-29 13:17:32 +03:00
|
|
|
func Register(r register.Register, opts ...RegisterOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
ropts := registerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&ropts)
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
|
|
|
}
|
|
|
|
for _, rtr := range o.Routers {
|
|
|
|
for _, os := range ropts.routers {
|
|
|
|
if rtr.Name() == os || all {
|
|
|
|
if err = rtr.Init(router.Register(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, srv := range o.Servers {
|
|
|
|
for _, os := range ropts.servers {
|
|
|
|
if srv.Name() == os || all {
|
|
|
|
if err = srv.Init(server.Register(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, brk := range o.Brokers {
|
|
|
|
for _, os := range ropts.brokers {
|
|
|
|
if brk.Name() == os || all {
|
|
|
|
if err = brk.Init(broker.Register(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
type registerOptions struct {
|
|
|
|
routers []string
|
|
|
|
servers []string
|
|
|
|
brokers []string
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterOption func signature
|
2021-01-29 13:17:32 +03:00
|
|
|
type RegisterOption func(*registerOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterRouter speciefies routers for register
|
2021-01-29 13:17:32 +03:00
|
|
|
func RegisterRouter(n string) RegisterOption {
|
|
|
|
return func(o *registerOptions) {
|
|
|
|
o.routers = append(o.routers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterServer specifies servers for register
|
2021-01-29 13:17:32 +03:00
|
|
|
func RegisterServer(n string) RegisterOption {
|
|
|
|
return func(o *registerOptions) {
|
|
|
|
o.servers = append(o.servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RegisterBroker specifies broker for register
|
2021-01-29 13:17:32 +03:00
|
|
|
func RegisterBroker(n string) RegisterOption {
|
|
|
|
return func(o *registerOptions) {
|
|
|
|
o.brokers = append(o.brokers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Tracer sets the tracer
|
2023-03-24 00:25:47 +03:00
|
|
|
//
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2021-01-29 13:17:32 +03:00
|
|
|
func Tracer(t tracer.Tracer, opts ...TracerOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
topts := tracerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&topts)
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
|
|
|
}
|
|
|
|
for _, srv := range o.Servers {
|
|
|
|
for _, os := range topts.servers {
|
|
|
|
if srv.Name() == os || all {
|
|
|
|
if err = srv.Init(server.Tracer(t)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, cli := range o.Clients {
|
|
|
|
for _, os := range topts.clients {
|
|
|
|
if cli.Name() == os || all {
|
|
|
|
if err = cli.Init(client.Tracer(t)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 23:32:33 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, str := range o.Stores {
|
|
|
|
for _, os := range topts.stores {
|
|
|
|
if str.Name() == os || all {
|
|
|
|
if err = str.Init(store.Tracer(t)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, brk := range o.Brokers {
|
|
|
|
for _, os := range topts.brokers {
|
|
|
|
if brk.Name() == os || all {
|
|
|
|
if err = brk.Init(broker.Tracer(t)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tracerOptions struct {
|
|
|
|
clients []string
|
|
|
|
servers []string
|
|
|
|
brokers []string
|
|
|
|
stores []string
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// TracerOption func signature
|
2021-01-29 13:17:32 +03:00
|
|
|
type TracerOption func(*tracerOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// TracerClient sets the clients for tracer
|
2021-01-29 13:17:32 +03:00
|
|
|
func TracerClient(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.clients = append(o.clients, n)
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// TracerServer sets the servers for tracer
|
2021-01-29 13:17:32 +03:00
|
|
|
func TracerServer(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.servers = append(o.servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// TracerBroker sets the broker for tracer
|
2021-01-29 13:17:32 +03:00
|
|
|
func TracerBroker(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.brokers = append(o.brokers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// TracerStore sets the store for tracer
|
2021-01-29 13:17:32 +03:00
|
|
|
func TracerStore(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.stores = append(o.stores, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:37:12 +03:00
|
|
|
// Config sets the config for the service
|
|
|
|
func Config(c ...config.Config) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
o.Configs = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-04 02:28:45 +03:00
|
|
|
// Configs sets the configs for the service
|
2020-12-13 18:56:54 +03:00
|
|
|
func Configs(c ...config.Config) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-12-04 02:28:45 +03:00
|
|
|
o.Configs = c
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
/*
|
2020-08-28 10:57:42 +03:00
|
|
|
// Selector sets the selector for the service client
|
|
|
|
func Selector(s selector.Selector) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Client != nil {
|
|
|
|
o.Client.Init(client.Selector(s))
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
*/
|
|
|
|
/*
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
*/
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
// Router sets the router
|
2021-01-29 13:17:32 +03:00
|
|
|
func Router(r router.Router, opts ...RouterOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
ropts := routerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&ropts)
|
|
|
|
}
|
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
|
|
|
}
|
|
|
|
for _, cli := range o.Clients {
|
|
|
|
for _, os := range ropts.clients {
|
|
|
|
if cli.Name() == os || all {
|
|
|
|
if err = cli.Init(client.Router(r)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type routerOptions struct {
|
|
|
|
clients []string
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RouterOption func signature
|
2021-01-29 13:17:32 +03:00
|
|
|
type RouterOption func(*routerOptions)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// RouterClient sets the clients for router
|
2021-01-29 13:17:32 +03:00
|
|
|
func RouterClient(n string) RouterOption {
|
|
|
|
return func(o *routerOptions) {
|
|
|
|
o.clients = append(o.clients, n)
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address sets the address of the server
|
|
|
|
func Address(addr string) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
|
|
|
switch len(o.Servers) {
|
|
|
|
case 0:
|
|
|
|
return fmt.Errorf("cant set address on nil server")
|
|
|
|
case 1:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("cant set same address for multiple servers")
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
return o.Servers[0].Init(server.Address(addr))
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name of the service
|
|
|
|
func Name(n string) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
|
|
|
o.Name = n
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version of the service
|
|
|
|
func Version(v string) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
|
|
|
o.Version = v
|
|
|
|
return nil
|
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 {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
|
|
|
o.Metadata = metadata.Copy(md)
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterTTL specifies the TTL to use when registering the service
|
2021-01-29 13:17:32 +03:00
|
|
|
func RegisterTTL(td time.Duration, opts ...RegisterOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
ropts := registerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&ropts)
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
2020-08-29 17:44:49 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, srv := range o.Servers {
|
|
|
|
for _, os := range ropts.servers {
|
|
|
|
if srv.Name() == os || all {
|
|
|
|
if err = srv.Init(server.RegisterTTL(td)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// RegisterInterval specifies the interval on which to re-register
|
|
|
|
func RegisterInterval(td time.Duration, opts ...RegisterOption) Option {
|
|
|
|
return func(o *Options) error {
|
|
|
|
var err error
|
|
|
|
ropts := registerOptions{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&ropts)
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
all := false
|
|
|
|
if len(opts) == 0 {
|
|
|
|
all = true
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, srv := range o.Servers {
|
|
|
|
for _, os := range ropts.servers {
|
|
|
|
if srv.Name() == os || all {
|
|
|
|
if err = srv.Init(server.RegisterInterval(td)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeStart run funcs before service starts
|
2020-12-04 02:28:45 +03:00
|
|
|
func BeforeStart(fn func(context.Context) error) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-08-28 10:57:42 +03:00
|
|
|
o.BeforeStart = append(o.BeforeStart, fn)
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeforeStop run funcs before service stops
|
2020-12-04 02:28:45 +03:00
|
|
|
func BeforeStop(fn func(context.Context) error) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-08-28 10:57:42 +03:00
|
|
|
o.BeforeStop = append(o.BeforeStop, fn)
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterStart run funcs after service starts
|
2020-12-04 02:28:45 +03:00
|
|
|
func AfterStart(fn func(context.Context) error) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-08-28 10:57:42 +03:00
|
|
|
o.AfterStart = append(o.AfterStart, fn)
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AfterStop run funcs after service stops
|
2020-12-04 02:28:45 +03:00
|
|
|
func AfterStop(fn func(context.Context) error) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-08-28 10:57:42 +03:00
|
|
|
o.AfterStop = append(o.AfterStop, fn)
|
2021-01-29 13:17:32 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|