libs5-go/node/node.go

102 lines
2.0 KiB
Go

package node
import (
"git.lumeweb.com/LumeWeb/libs5-go/config"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/service"
_default "git.lumeweb.com/LumeWeb/libs5-go/service/default"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
)
type Node struct {
nodeConfig *config.NodeConfig
services service.Services
}
func (n *Node) Services() service.Services {
return n.services
}
func NewNode(config *config.NodeConfig, services service.Services) *Node {
return &Node{
nodeConfig: config,
services: services, // Services are passed in, not created here
}
}
func (n *Node) IsStarted() bool {
return n.services.IsStarted()
}
func (n *Node) Config() *config.NodeConfig {
return n.nodeConfig
}
func (n *Node) Logger() *zap.Logger {
if n.nodeConfig != nil {
return n.nodeConfig.Logger
}
return nil
}
func (n *Node) Db() *bolt.DB {
if n.nodeConfig != nil {
return n.nodeConfig.DB
}
return nil
}
func (n *Node) Start() error {
protocol.RegisterProtocols()
protocol.RegisterSignedProtocols()
return n.services.Start()
}
func (n *Node) Init() error {
return n.services.Init()
}
func (n *Node) Stop() error {
return n.services.Stop()
}
func (n *Node) WaitOnConnectedPeers() {
n.services.P2P().WaitOnConnectedPeers()
}
func (n *Node) NetworkId() string {
return n.services.P2P().NetworkId()
}
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 := _default.NewP2P(params)
registryService := _default.NewRegistry(params)
httpService := _default.NewHTTP(params)
storageService := _default.NewStorage(params)
// Aggregate services
services := NewServices(ServicesParams{
P2P: p2pService,
Registry: registryService,
HTTP: httpService,
Storage: storageService,
})
// Now create the node with the services
return NewNode(config, services)
}