refactor: add All method so that we can range over services dynamically

This commit is contained in:
Derrick Hammer 2024-01-10 06:33:21 -05:00
parent 6bf557346d
commit 7578665ba4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 18 additions and 17 deletions

View File

@ -11,4 +11,5 @@ type Service interface {
type Services interface { type Services interface {
P2P() P2PService P2P() P2PService
Registry() RegistryService Registry() RegistryService
All() []Service
} }

View File

@ -94,25 +94,17 @@ func (n *NodeImpl) Start() error {
n.started = true n.started = true
err = n.Services().P2P().Init() for _, svc := range n.services.All() {
if err != nil { err := svc.Init()
return err if err != nil {
} return err
}
err = n.Services().P2P().Start() err = svc.Start()
if err != nil { if err != nil {
return err return err
}
} }
err = n.Services().Registry().Init()
if err != nil {
return err
}
err = n.Services().Registry().Start()
if err != nil {
return err
}
n.started = true n.started = true
return nil return nil
} }

View File

@ -11,6 +11,14 @@ type ServicesImpl struct {
registry interfaces.RegistryService registry interfaces.RegistryService
} }
func (s *ServicesImpl) All() []interfaces.Service {
services := make([]interfaces.Service, 0)
services = append(services, s.p2p)
services = append(services, s.registry)
return services
}
func (s *ServicesImpl) Registry() interfaces.RegistryService { func (s *ServicesImpl) Registry() interfaces.RegistryService {
return s.registry return s.registry
} }