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"
|
|
|
|
|
|
|
|
"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"
|
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"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
2021-01-29 13:17:32 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-08-28 10:57:42 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
"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"
|
2021-01-29 13:17:32 +03:00
|
|
|
// "github.com/unistack-org/micro/v3/debug/profile"
|
|
|
|
// "github.com/unistack-org/micro/v3/runtime"
|
2020-08-28 10:57:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options for micro service
|
|
|
|
type Options struct {
|
2021-01-29 13:17:32 +03:00
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Metadata metadata.Metadata
|
|
|
|
|
|
|
|
Auths []auth.Auth
|
|
|
|
Brokers []broker.Broker
|
|
|
|
Loggers []logger.Logger
|
|
|
|
Meters []meter.Meter
|
|
|
|
Configs []config.Config
|
|
|
|
Clients []client.Client
|
|
|
|
Servers []server.Server
|
|
|
|
Stores []store.Store
|
|
|
|
Registers []register.Register
|
|
|
|
Tracers []tracer.Tracer
|
|
|
|
Routers []router.Router
|
|
|
|
// Runtime runtime.Runtime
|
|
|
|
// Profile profile.Profile
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
// Before and After funcs
|
2020-12-04 02:28:45 +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{
|
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},
|
|
|
|
Auths: []auth.Auth{auth.DefaultAuth},
|
|
|
|
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},
|
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
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type BrokerOption func(*brokerOptions)
|
|
|
|
|
|
|
|
func BrokerClient(n string) BrokerOption {
|
|
|
|
return func(o *brokerOptions) {
|
|
|
|
o.clients = append(o.clients, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BrokerServer(n string) BrokerOption {
|
|
|
|
return func(o *brokerOptions) {
|
|
|
|
o.servers = append(o.servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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-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
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, mtr := range o.Meters {
|
|
|
|
for _, or := range lopts.meters {
|
|
|
|
if mtr.Name() == or || all {
|
|
|
|
if err = mtr.Init(meter.Logger(l)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-01-29 13:17:32 +03:00
|
|
|
type LoggerOption func(*loggerOptions)
|
|
|
|
|
|
|
|
type loggerOptions struct {
|
|
|
|
servers []string
|
|
|
|
clients []string
|
|
|
|
brokers []string
|
|
|
|
registers []string
|
|
|
|
stores []string
|
|
|
|
meters []string
|
|
|
|
tracers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
func LoggerServer(n string) LoggerOption {
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegisterOption func(*registerOptions)
|
|
|
|
|
|
|
|
func RegisterRouter(n string) RegisterOption {
|
|
|
|
return func(o *registerOptions) {
|
|
|
|
o.routers = append(o.routers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterServer(n string) RegisterOption {
|
|
|
|
return func(o *registerOptions) {
|
|
|
|
o.servers = append(o.servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterBroker(n string) RegisterOption {
|
|
|
|
return func(o *registerOptions) {
|
|
|
|
o.brokers = append(o.brokers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type TracerOption func(*tracerOptions)
|
|
|
|
|
|
|
|
func TracerClient(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.clients = append(o.clients, n)
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func TracerServer(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.servers = append(o.servers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TracerBroker(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.brokers = append(o.brokers, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TracerStore(n string) TracerOption {
|
|
|
|
return func(o *tracerOptions) {
|
|
|
|
o.stores = append(o.stores, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-08-28 10:57:42 +03:00
|
|
|
// Auth sets the auth for the service
|
|
|
|
func Auth(a auth.Auth) Option {
|
2021-01-29 13:17:32 +03:00
|
|
|
return func(o *Options) error {
|
2020-08-28 10:57:42 +03:00
|
|
|
o.Auth = a
|
2020-08-29 17:44:49 +03:00
|
|
|
if o.Server != nil {
|
|
|
|
o.Server.Init(server.Auth(a))
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouterOption func(*routerOptions)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|