mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-16 09:48:28 +02:00
remove unused options (#767)
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/go-kratos/kratos/v2/log"
|
"github.com/go-kratos/kratos/v2/log"
|
||||||
"github.com/go-kratos/kratos/v2/registry"
|
"github.com/go-kratos/kratos/v2/registry"
|
||||||
"github.com/go-kratos/kratos/v2/transport"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
@@ -42,26 +41,11 @@ func New(opts ...Option) *App {
|
|||||||
opts: options,
|
opts: options,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
instance: serviceInstance(options),
|
instance: buildInstance(options),
|
||||||
log: log.NewHelper("app", options.logger),
|
log: log.NewHelper("app", options.logger),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logger returns logger.
|
|
||||||
func (a *App) Logger() log.Logger {
|
|
||||||
return a.opts.logger
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server returns transport servers.
|
|
||||||
func (a *App) Server() []transport.Server {
|
|
||||||
return a.opts.servers
|
|
||||||
}
|
|
||||||
|
|
||||||
// Registry returns registry.
|
|
||||||
func (a *App) Registry() registry.Registrar {
|
|
||||||
return a.opts.registrar
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run executes all OnStart hooks registered with the application's Lifecycle.
|
// Run executes all OnStart hooks registered with the application's Lifecycle.
|
||||||
func (a *App) Run() error {
|
func (a *App) Run() error {
|
||||||
a.log.Infow(
|
a.log.Infow(
|
||||||
@@ -80,11 +64,6 @@ func (a *App) Run() error {
|
|||||||
return srv.Start()
|
return srv.Start()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, fn := range a.opts.before {
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if a.opts.registrar != nil {
|
if a.opts.registrar != nil {
|
||||||
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
|
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -118,15 +97,10 @@ func (a *App) Stop() error {
|
|||||||
if a.cancel != nil {
|
if a.cancel != nil {
|
||||||
a.cancel()
|
a.cancel()
|
||||||
}
|
}
|
||||||
for _, fn := range a.opts.after {
|
|
||||||
if err := fn(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func serviceInstance(o options) *registry.ServiceInstance {
|
func buildInstance(o options) *registry.ServiceInstance {
|
||||||
if len(o.endpoints) == 0 {
|
if len(o.endpoints) == 0 {
|
||||||
for _, srv := range o.servers {
|
for _, srv := range o.servers {
|
||||||
if e, err := srv.Endpoint(); err == nil {
|
if e, err := srv.Endpoint(); err == nil {
|
||||||
|
|||||||
-13
@@ -27,9 +27,6 @@ type options struct {
|
|||||||
registrar registry.Registrar
|
registrar registry.Registrar
|
||||||
|
|
||||||
servers []transport.Server
|
servers []transport.Server
|
||||||
|
|
||||||
before []func() error
|
|
||||||
after []func() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ID with service id.
|
// ID with service id.
|
||||||
@@ -81,13 +78,3 @@ func Registrar(r registry.Registrar) Option {
|
|||||||
func Server(srv ...transport.Server) Option {
|
func Server(srv ...transport.Server) Option {
|
||||||
return func(o *options) { o.servers = srv }
|
return func(o *options) { o.servers = srv }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Before before service starts.
|
|
||||||
func Before(fn func() error) Option {
|
|
||||||
return func(o *options) { o.before = append(o.before, fn) }
|
|
||||||
}
|
|
||||||
|
|
||||||
// After after services stops.
|
|
||||||
func After(fn func() error) Option {
|
|
||||||
return func(o *options) { o.after = append(o.after, fn) }
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user