libs5-go/node/node.go

98 lines
2.0 KiB
Go
Raw Normal View History

package node
2024-01-06 11:33:46 +00:00
import (
"git.lumeweb.com/LumeWeb/libs5-go/config"
2024-01-29 19:33:40 +00:00
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
"git.lumeweb.com/LumeWeb/libs5-go/service"
2024-01-06 11:33:46 +00:00
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
)
type Node struct {
nodeConfig *config.NodeConfig
services service.Services
2024-01-07 14:02:39 +00:00
}
func (n *Node) Services() service.Services {
return n.services
2024-01-06 11:33:46 +00:00
}
func NewNode(config *config.NodeConfig, services service.Services) *Node {
return &Node{
nodeConfig: config,
services: services, // Services are passed in, not created here
2024-01-06 11:33:46 +00:00
}
}
func (n *Node) IsStarted() bool {
return n.services.IsStarted()
2024-01-06 11:33:46 +00:00
}
func (n *Node) Config() *config.NodeConfig {
2024-01-06 11:33:46 +00:00
return n.nodeConfig
}
func (n *Node) Logger() *zap.Logger {
2024-01-06 11:33:46 +00:00
if n.nodeConfig != nil {
return n.nodeConfig.Logger
}
return nil
}
func (n *Node) Db() *bolt.DB {
2024-01-06 11:33:46 +00:00
if n.nodeConfig != nil {
2024-01-06 18:15:45 +00:00
return n.nodeConfig.DB
2024-01-06 11:33:46 +00:00
}
return nil
}
func (n *Node) Start() error {
protocol.RegisterProtocols()
signed.RegisterSignedProtocols()
return n.services.Start()
}
func (n *Node) Stop() error {
return n.services.Stop()
}
func (n *Node) WaitOnConnectedPeers() {
n.services.P2P().WaitOnConnectedPeers()
}
2024-01-29 19:26:10 +00:00
func (n *Node) NetworkId() string {
return n.services.P2P().NetworkId()
}
2024-01-29 19:33:40 +00:00
func (n *Node) NodeId() *encoding.NodeId {
return n.services.P2P().NodeId()
}
func DefaultNode(config *config.NodeConfig) *Node {
params := service.ServiceParams{
Logger: config.Logger,
Config: config,
Db: config.DB,
}
// Initialize services first
p2pService := service.NewP2P(params)
registryService := service.NewRegistry(params)
httpService := service.NewHTTP(params)
2024-01-29 07:12:02 +00:00
storageService := service.NewStorage(params)
// Aggregate services
services := NewServices(ServicesParams{
P2P: p2pService,
Registry: registryService,
HTTP: httpService,
2024-01-29 07:12:02 +00:00
Storage: storageService,
})
// Now create the node with the services
return NewNode(config, services)
}