libs5-go/node/services.go

121 lines
2.0 KiB
Go
Raw Normal View History

2024-01-07 09:15:28 +00:00
package node
import (
2024-02-27 08:28:25 +00:00
"context"
"git.lumeweb.com/LumeWeb/libs5-go/service"
)
2024-01-07 09:15:28 +00:00
var (
_ service.Services = (*ServicesImpl)(nil)
2024-01-07 09:15:28 +00:00
)
type ServicesParams struct {
P2P service.P2PService
Registry service.RegistryService
HTTP service.HTTPService
Storage service.StorageService
}
2024-01-07 09:15:28 +00:00
type ServicesImpl struct {
p2p service.P2PService
registry service.RegistryService
http service.HTTPService
storage service.StorageService
started bool
2024-02-27 09:07:12 +00:00
starting bool
}
func (s *ServicesImpl) HTTP() service.HTTPService {
return s.http
2024-01-10 11:21:03 +00:00
}
func (s *ServicesImpl) Storage() service.StorageService {
return s.storage
}
func (s *ServicesImpl) All() []service.Service {
services := make([]service.Service, 0)
services = append(services, s.p2p)
services = append(services, s.registry)
services = append(services, s.http)
services = append(services, s.storage)
return services
}
func (s *ServicesImpl) Registry() service.RegistryService {
2024-01-10 11:21:03 +00:00
return s.registry
2024-01-07 09:15:28 +00:00
}
func NewServices(params ServicesParams) service.Services {
sc := &ServicesImpl{
p2p: params.P2P,
registry: params.Registry,
http: params.HTTP,
storage: params.Storage,
started: false,
}
for _, svc := range sc.All() {
svc.SetServices(sc)
}
return sc
2024-01-07 09:15:28 +00:00
}
func (s *ServicesImpl) P2P() service.P2PService {
2024-01-07 09:15:28 +00:00
return s.p2p
}
func (s *ServicesImpl) IsStarted() bool {
return s.started
}
2024-02-27 09:07:12 +00:00
func (s *ServicesImpl) IsStarting() bool {
return s.starting
}
2024-02-27 08:28:25 +00:00
func (s *ServicesImpl) Init(ctx context.Context) error {
err := s.p2p.Config().DB.Open()
if err != nil {
return err
}
2024-01-30 20:46:00 +00:00
for _, svc := range s.All() {
2024-02-27 08:28:25 +00:00
err := svc.Init(ctx)
2024-01-30 20:46:00 +00:00
if err != nil {
return err
}
}
return nil
}
2024-02-27 08:28:25 +00:00
func (s *ServicesImpl) Start(ctx context.Context) error {
2024-02-27 09:07:12 +00:00
s.starting = true
for _, svc := range s.All() {
2024-02-27 08:28:25 +00:00
err := svc.Start(ctx)
if err != nil {
return err
}
}
s.started = true
2024-02-27 09:07:12 +00:00
s.starting = false
return nil
}
2024-02-27 08:28:25 +00:00
func (s *ServicesImpl) Stop(ctx context.Context) error {
for _, svc := range s.All() {
2024-02-27 08:28:25 +00:00
err := svc.Stop(ctx)
if err != nil {
return err
}
}
s.started = false
return nil
}