From 66e1cba39bb05ad76c02086dfe140a80c9b3938b Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 15 Jan 2024 20:07:08 -0500 Subject: [PATCH] refactor: move init and startup process to a functional approach for readability --- cmd/portal/init.go | 117 ++++++++++++++++++++++++++++++++++++++++++ cmd/portal/portal.go | 118 +++++-------------------------------------- cmd/portal/start.go | 22 ++++++++ interfaces/portal.go | 4 ++ 4 files changed, 156 insertions(+), 105 deletions(-) create mode 100644 cmd/portal/init.go create mode 100644 cmd/portal/start.go diff --git a/cmd/portal/init.go b/cmd/portal/init.go new file mode 100644 index 0000000..7e32681 --- /dev/null +++ b/cmd/portal/init.go @@ -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, + } +} diff --git a/cmd/portal/portal.go b/cmd/portal/portal.go index 08a9270..120770a 100644 --- a/cmd/portal/portal.go +++ b/cmd/portal/portal.go @@ -3,13 +3,10 @@ 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" "git.lumeweb.com/LumeWeb/portal/storage" "github.com/spf13/viper" - "go.sia.tech/core/wallet" "go.uber.org/zap" "gorm.io/gorm" "net/http" @@ -43,8 +40,8 @@ func NewPortal() interfaces.Portal { } func (p *PortalImpl) Initialize() error { - for _, initFunc := range p.getInitFuncs() { - if err := initFunc(); err != nil { + for _, initFunc := range getInitList() { + if err := initFunc(p); err != nil { return err } } @@ -52,8 +49,8 @@ func (p *PortalImpl) Initialize() error { return nil } func (p *PortalImpl) Run() { - for _, initFunc := range p.getStartFuncs() { - if err := initFunc(); err != nil { + for _, initFunc := range getStartList() { + if err := initFunc(p); err != nil { 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 { 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 { return p.storage } -func (p *PortalImpl) getStartFuncs() []func() error { - return []func() error{ - func() error { - for _, _func := range p.protocolRegistry.All() { - err := _func.Start() - if err != nil { - return err - } - } - - return nil - }, - } +func (p *PortalImpl) SetIdentity(identity ed25519.PrivateKey) { + p.identity = identity +} + +func (p *PortalImpl) SetLogger(logger *zap.Logger) { + p.logger = logger +} +func (p *PortalImpl) ProtocolRegistry() protocols.ProtocolRegistry { + return p.protocolRegistry } diff --git a/cmd/portal/start.go b/cmd/portal/start.go new file mode 100644 index 0000000..5823f5d --- /dev/null +++ b/cmd/portal/start.go @@ -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, + } +} diff --git a/interfaces/portal.go b/interfaces/portal.go index 8b1bc4a..3fd3a73 100644 --- a/interfaces/portal.go +++ b/interfaces/portal.go @@ -2,6 +2,7 @@ package interfaces import ( "crypto/ed25519" + "git.lumeweb.com/LumeWeb/portal/protocols" "github.com/spf13/viper" "go.uber.org/zap" "gorm.io/gorm" @@ -14,6 +15,9 @@ type Portal interface { Logger() *zap.Logger Db() *gorm.DB ApiRegistry() APIRegistry + ProtocolRegistry() protocols.ProtocolRegistry Identity() ed25519.PrivateKey Storage() StorageService + SetIdentity(identity ed25519.PrivateKey) + SetLogger(logger *zap.Logger) }