2024-01-12 00:11:53 +00:00
|
|
|
package protocols
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2024-02-17 08:24:44 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/protocols/registry"
|
2024-01-29 20:11:57 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/protocols/s5"
|
2024-01-28 07:58:33 +00:00
|
|
|
"github.com/samber/lo"
|
2024-01-28 07:20:59 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/fx"
|
|
|
|
)
|
2024-01-16 01:10:15 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func RegisterProtocols() {
|
|
|
|
registry.Register(registry.ProtocolEntry{
|
2024-02-17 08:24:44 +00:00
|
|
|
Key: "s5",
|
|
|
|
Module: s5.ProtocolModule,
|
2024-01-28 07:20:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildProtocols(config *viper.Viper) fx.Option {
|
|
|
|
var options []fx.Option
|
2024-01-28 07:58:33 +00:00
|
|
|
enabledProtocols := config.GetStringSlice("core.protocols")
|
2024-01-28 07:20:59 +00:00
|
|
|
for _, entry := range registry.GetRegistry() {
|
2024-01-28 07:58:33 +00:00
|
|
|
if lo.Contains(enabledProtocols, entry.Key) {
|
2024-01-28 07:20:59 +00:00
|
|
|
options = append(options, entry.Module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-17 08:24:44 +00:00
|
|
|
options = append(options, fx.Invoke(func(protocols []registry.Protocol) error {
|
|
|
|
for _, protocol := range protocols {
|
|
|
|
err := protocol.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2024-01-28 10:22:11 +00:00
|
|
|
return fx.Module("protocols", fx.Options(options...))
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 09:44:33 +00:00
|
|
|
type LifecyclesParams struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
Protocols []registry.Protocol `group:"protocol"`
|
|
|
|
}
|
|
|
|
|
2024-01-30 19:53:50 +00:00
|
|
|
func SetupLifecycles(lifecycle fx.Lifecycle, params LifecyclesParams) error {
|
2024-01-28 07:20:59 +00:00
|
|
|
for _, entry := range registry.GetRegistry() {
|
2024-01-28 09:44:33 +00:00
|
|
|
for _, protocol := range params.Protocols {
|
2024-01-28 07:20:59 +00:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 19:53:50 +00:00
|
|
|
|
|
|
|
return nil
|
2024-01-12 00:11:53 +00:00
|
|
|
}
|