refactor: move init and startup process to a functional approach for readability

This commit is contained in:
Derrick Hammer 2024-01-15 20:07:08 -05:00
parent 8ceb8c1eb9
commit 66e1cba39b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 156 additions and 105 deletions

117
cmd/portal/init.go Normal file
View File

@ -0,0 +1,117 @@
package main
import (
"crypto/ed25519"
"git.lumeweb.com/LumeWeb/portal/api"
"git.lumeweb.com/LumeWeb/portal/config"
"git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/logger"
"git.lumeweb.com/LumeWeb/portal/protocols"
"go.sia.tech/core/wallet"
)
type initFunc func(p interfaces.Portal) error
func initConfig(p interfaces.Portal) error {
return config.Init()
}
func initIdentity(p interfaces.Portal) error {
var seed [32]byte
identitySeed := p.Config().GetString("core.identity")
if identitySeed == "" {
p.Logger().Info("Generating new identity seed")
identitySeed = wallet.NewSeedPhrase()
p.Config().Set("core.identity", identitySeed)
err := p.Config().WriteConfig()
if err != nil {
return err
}
}
err := wallet.SeedFromPhrase(&seed, identitySeed)
if err != nil {
return err
}
p.SetIdentity(ed25519.PrivateKey(wallet.KeyFromSeed(&seed, 0)))
return nil
}
func initCheckRequiredConfig(p interfaces.Portal) error {
required := []string{
"core.domain",
"core.port",
"core.sia.url",
"core.sia.key",
}
for _, key := range required {
if !p.Config().IsSet(key) {
p.Logger().Fatal(key + " is required")
}
}
return nil
}
func initLogger(p interfaces.Portal) error {
p.SetLogger(logger.Init(p.Config()))
return nil
}
func initProtocols(p interfaces.Portal) error {
return protocols.Init(p.ProtocolRegistry())
}
func initStorage(p interfaces.Portal) error {
p.Storage().Init()
return nil
}
func initAPI(p interfaces.Portal) error {
return api.Init(p.ApiRegistry())
}
func initializeProtocolRegistry(p interfaces.Portal) error {
for _, _func := range p.ProtocolRegistry().All() {
err := _func.Initialize(p)
if err != nil {
return err
}
}
return nil
}
func initializeAPIRegistry(p interfaces.Portal) error {
for protoName, _func := range p.ApiRegistry().All() {
proto, err := p.ProtocolRegistry().Get(protoName)
if err != nil {
return err
}
err = _func.Initialize(p, proto)
if err != nil {
return err
}
}
return nil
}
func getInitList() []initFunc {
return []initFunc{
initConfig,
initIdentity,
initCheckRequiredConfig,
initLogger,
initProtocols,
initStorage,
initAPI,
initializeProtocolRegistry,
initializeAPIRegistry,
}
}

View File

@ -3,13 +3,10 @@ package main
import ( import (
"crypto/ed25519" "crypto/ed25519"
"git.lumeweb.com/LumeWeb/portal/api" "git.lumeweb.com/LumeWeb/portal/api"
"git.lumeweb.com/LumeWeb/portal/config"
"git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/logger"
"git.lumeweb.com/LumeWeb/portal/protocols" "git.lumeweb.com/LumeWeb/portal/protocols"
"git.lumeweb.com/LumeWeb/portal/storage" "git.lumeweb.com/LumeWeb/portal/storage"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.sia.tech/core/wallet"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
"net/http" "net/http"
@ -43,8 +40,8 @@ func NewPortal() interfaces.Portal {
} }
func (p *PortalImpl) Initialize() error { func (p *PortalImpl) Initialize() error {
for _, initFunc := range p.getInitFuncs() { for _, initFunc := range getInitList() {
if err := initFunc(); err != nil { if err := initFunc(p); err != nil {
return err return err
} }
} }
@ -52,8 +49,8 @@ func (p *PortalImpl) Initialize() error {
return nil return nil
} }
func (p *PortalImpl) Run() { func (p *PortalImpl) Run() {
for _, initFunc := range p.getStartFuncs() { for _, initFunc := range getStartList() {
if err := initFunc(); err != nil { if err := initFunc(p); err != nil {
p.logger.Fatal("Failed to start", zap.Error(err)) p.logger.Fatal("Failed to start", zap.Error(err))
} }
} }
@ -78,106 +75,17 @@ func (p *PortalImpl) ApiRegistry() interfaces.APIRegistry {
func (p *PortalImpl) Identity() ed25519.PrivateKey { func (p *PortalImpl) Identity() ed25519.PrivateKey {
return p.identity return p.identity
} }
func (p *PortalImpl) getInitFuncs() []func() error {
return []func() error{
func() error {
return config.Init()
},
func() error {
var seed [32]byte
identitySeed := p.Config().GetString("core.identity")
if identitySeed == "" {
p.Logger().Info("Generating new identity seed")
identitySeed = wallet.NewSeedPhrase()
p.Config().Set("core.identity", identitySeed)
err := p.Config().WriteConfig()
if err != nil {
return err
}
}
err := wallet.SeedFromPhrase(&seed, identitySeed)
if err != nil {
return err
}
p.identity = ed25519.PrivateKey(wallet.KeyFromSeed(&seed, 0))
return nil
},
func() error {
required := []string{
"core.domain",
"core.port",
"core.sia.url",
"core.sia.key",
}
for _, key := range required {
if !p.Config().IsSet(key) {
p.logger.Fatal(key + " is required")
}
}
return nil
},
func() error {
p.logger = logger.Init(p.Config())
return nil
},
func() error {
return protocols.Init(p.protocolRegistry)
},
func() error {
p.storage.Init()
return nil
},
func() error {
return api.Init(p.apiRegistry)
},
func() error {
for _, _func := range p.protocolRegistry.All() {
err := _func.Initialize(p)
if err != nil {
return err
}
}
return nil
}, func() error {
for protoName, _func := range p.apiRegistry.All() {
proto, err := p.protocolRegistry.Get(protoName)
if err != nil {
return err
}
err = _func.Initialize(p, proto)
if err != nil {
return err
}
}
return nil
},
}
}
func (p *PortalImpl) Storage() interfaces.StorageService { func (p *PortalImpl) Storage() interfaces.StorageService {
return p.storage return p.storage
} }
func (p *PortalImpl) getStartFuncs() []func() error { func (p *PortalImpl) SetIdentity(identity ed25519.PrivateKey) {
return []func() error{ p.identity = identity
func() error {
for _, _func := range p.protocolRegistry.All() {
err := _func.Start()
if err != nil {
return err
}
} }
return nil func (p *PortalImpl) SetLogger(logger *zap.Logger) {
}, p.logger = logger
} }
func (p *PortalImpl) ProtocolRegistry() protocols.ProtocolRegistry {
return p.protocolRegistry
} }

22
cmd/portal/start.go Normal file
View File

@ -0,0 +1,22 @@
package main
import "git.lumeweb.com/LumeWeb/portal/interfaces"
type startFunc func(p interfaces.Portal) error
func initProtocolRegistry(p interfaces.Portal) error {
for _, _func := range p.ProtocolRegistry().All() {
err := _func.Start()
if err != nil {
return err
}
}
return nil
}
func getStartList() []startFunc {
return []startFunc{
initProtocolRegistry,
}
}

View File

@ -2,6 +2,7 @@ package interfaces
import ( import (
"crypto/ed25519" "crypto/ed25519"
"git.lumeweb.com/LumeWeb/portal/protocols"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
"gorm.io/gorm" "gorm.io/gorm"
@ -14,6 +15,9 @@ type Portal interface {
Logger() *zap.Logger Logger() *zap.Logger
Db() *gorm.DB Db() *gorm.DB
ApiRegistry() APIRegistry ApiRegistry() APIRegistry
ProtocolRegistry() protocols.ProtocolRegistry
Identity() ed25519.PrivateKey Identity() ed25519.PrivateKey
Storage() StorageService Storage() StorageService
SetIdentity(identity ed25519.PrivateKey)
SetLogger(logger *zap.Logger)
} }