mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-28 03:57:02 +02:00
parent
26cf7c80ad
commit
50af5b2588
10
app.go
10
app.go
@ -80,6 +80,11 @@ func (a *App) Run() error {
|
||||
return srv.Start()
|
||||
})
|
||||
}
|
||||
for _, fn := range a.opts.before {
|
||||
if err := fn(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if a.opts.registrar != nil {
|
||||
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
|
||||
return err
|
||||
@ -113,6 +118,11 @@ func (a *App) Stop() error {
|
||||
if a.cancel != nil {
|
||||
a.cancel()
|
||||
}
|
||||
for _, fn := range a.opts.after {
|
||||
if err := fn(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,6 @@ func (codec) Unmarshal(data []byte, v interface{}) error {
|
||||
if rv.IsNil() {
|
||||
rv.Set(reflect.New(rv.Type().Elem()))
|
||||
v = rv.Interface()
|
||||
break
|
||||
}
|
||||
rv = rv.Elem()
|
||||
}
|
||||
|
13
options.go
13
options.go
@ -27,6 +27,9 @@ type options struct {
|
||||
registrar registry.Registrar
|
||||
|
||||
servers []transport.Server
|
||||
|
||||
before []func() error
|
||||
after []func() error
|
||||
}
|
||||
|
||||
// ID with service id.
|
||||
@ -78,3 +81,13 @@ func Registry(r registry.Registrar) Option {
|
||||
func Server(srv ...transport.Server) Option {
|
||||
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) }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user