portal/protocols/protocols.go

67 lines
1.4 KiB
Go
Raw Normal View History

2024-01-12 00:11:53 +00:00
package protocols
import (
"context"
"git.lumeweb.com/LumeWeb/portal/protocols/registry"
"git.lumeweb.com/LumeWeb/portal/protocols/s5"
"github.com/samber/lo"
"github.com/spf13/viper"
"go.uber.org/fx"
)
2024-01-16 01:10:15 +00:00
func RegisterProtocols() {
registry.Register(registry.ProtocolEntry{
Key: "s5",
Module: s5.ProtocolModule,
})
}
func BuildProtocols(config *viper.Viper) fx.Option {
var options []fx.Option
enabledProtocols := config.GetStringSlice("core.protocols")
for _, entry := range registry.GetRegistry() {
if lo.Contains(enabledProtocols, entry.Key) {
options = append(options, entry.Module)
}
}
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...))
}
2024-01-28 09:44:33 +00:00
type LifecyclesParams struct {
fx.In
Protocols []registry.Protocol `group:"protocol"`
}
func SetupLifecycles(lifecycle fx.Lifecycle, params LifecyclesParams) error {
for _, entry := range registry.GetRegistry() {
2024-01-28 09:44:33 +00:00
for _, protocol := range params.Protocols {
if protocol.Name() == entry.Key {
lifecycle.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return protocol.Start(ctx)
},
OnStop: func(ctx context.Context) error {
return protocol.Stop(ctx)
},
})
}
}
}
return nil
2024-01-12 00:11:53 +00:00
}