refactor: replicate what we did in api and remove the need for an InitFunc

This commit is contained in:
Derrick Hammer 2024-02-17 03:24:44 -05:00
parent 12a5f3f631
commit afc0b7a343
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 17 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package protocols
import ( import (
"context" "context"
"git.lumeweb.com/LumeWeb/portal/protocols/registry" "git.lumeweb.com/LumeWeb/portal/protocols/registry"
"git.lumeweb.com/LumeWeb/portal/protocols/s5" "git.lumeweb.com/LumeWeb/portal/protocols/s5"
"github.com/samber/lo" "github.com/samber/lo"
@ -11,9 +12,8 @@ import (
func RegisterProtocols() { func RegisterProtocols() {
registry.Register(registry.ProtocolEntry{ registry.Register(registry.ProtocolEntry{
Key: "s5", Key: "s5",
Module: s5.ProtocolModule, Module: s5.ProtocolModule,
InitFunc: s5.InitProtocol,
}) })
} }
@ -23,12 +23,20 @@ func BuildProtocols(config *viper.Viper) fx.Option {
for _, entry := range registry.GetRegistry() { for _, entry := range registry.GetRegistry() {
if lo.Contains(enabledProtocols, entry.Key) { if lo.Contains(enabledProtocols, entry.Key) {
options = append(options, entry.Module) options = append(options, entry.Module)
if entry.InitFunc != nil {
options = append(options, fx.Invoke(entry.InitFunc))
}
} }
} }
options = append(options, fx.Invoke(func(protocols []registry.Protocol) error {
for _, protocol := range protocols {
err := protocol.Init()
if err != nil {
return err
}
}
return nil
}))
return fx.Module("protocols", fx.Options(options...)) return fx.Module("protocols", fx.Options(options...))
} }

View File

@ -2,6 +2,7 @@ package registry
import ( import (
"context" "context"
"go.uber.org/fx" "go.uber.org/fx"
) )
@ -15,9 +16,8 @@ type Protocol interface {
} }
type ProtocolEntry struct { type ProtocolEntry struct {
Key string Key string
Module fx.Option Module fx.Option
InitFunc interface{}
} }
var protocolEntry []ProtocolEntry var protocolEntry []ProtocolEntry

View File

@ -162,9 +162,6 @@ func NewS5ProviderStore(params S5ProviderStoreParams) *S5ProviderStore {
} }
} }
func InitProtocol(s5 *S5Protocol, node *s5node.Node, store *S5ProviderStore) error {
return s5.Init(node, store)
}
func (s *S5Protocol) Init(args ...any) error { func (s *S5Protocol) Init(args ...any) error {
if node, ok := args[0].(*s5node.Node); !ok { if node, ok := args[0].(*s5node.Node); !ok {
s.logger.Fatal("Node is not a s5 node") s.logger.Fatal("Node is not a s5 node")