refactor: add PreInit and Node setter to store s5 node

This commit is contained in:
Derrick Hammer 2024-02-17 05:16:52 -05:00
parent 997e362d90
commit 263473db41
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 12 additions and 12 deletions

View File

@ -37,6 +37,7 @@ type S5Protocol struct {
identity ed25519.PrivateKey identity ed25519.PrivateKey
node *s5node.Node node *s5node.Node
tusHandler *TusHandler tusHandler *TusHandler
store *S5ProviderStore
} }
type S5ProtocolParams struct { type S5ProtocolParams struct {
@ -75,6 +76,10 @@ var ProtocolModule = fx.Module("s5_api",
s5fx.Module, s5fx.Module,
) )
var PreInit = func(protocol *S5Protocol, node *s5node.Node) {
protocol.SetNode(node)
}
func NewS5Protocol( func NewS5Protocol(
params S5ProtocolParams, params S5ProtocolParams,
) (S5ProtocolResult, error) { ) (S5ProtocolResult, error) {
@ -84,6 +89,7 @@ func NewS5Protocol(
storage: params.Storage, storage: params.Storage,
identity: params.Identity, identity: params.Identity,
tusHandler: params.TusHandler, tusHandler: params.TusHandler,
store: params.ProviderStore,
} }
cfg, err := ConfigureS5Protocol(params) cfg, err := ConfigureS5Protocol(params)
@ -162,18 +168,8 @@ func NewS5ProviderStore(params S5ProviderStoreParams) *S5ProviderStore {
} }
} }
func (s *S5Protocol) Init(args ...any) error { func (s *S5Protocol) Init() error {
if node, ok := args[0].(*s5node.Node); !ok { s.node.Services().Storage().SetProviderStore(s.store)
s.logger.Fatal("Node is not a s5 node")
} else {
s.node = node
}
if store, ok := args[1].(*S5ProviderStore); !ok {
s.logger.Fatal("Store is not a s5 store")
} else {
s.node.Services().Storage().SetProviderStore(store)
}
err := s.node.Init() err := s.node.Init()
if err != nil { if err != nil {
@ -218,6 +214,10 @@ func (s *S5Protocol) Stop(ctx context.Context) error {
return nil return nil
} }
func (s *S5Protocol) SetNode(node *s5node.Node) {
s.node = node
}
type S5ProviderStore struct { type S5ProviderStore struct {
config *viper.Viper config *viper.Viper
logger *zap.Logger logger *zap.Logger