2024-01-12 00:11:53 +00:00
|
|
|
package api
|
|
|
|
|
2024-01-12 04:13:10 +00:00
|
|
|
import (
|
2024-01-28 07:20:59 +00:00
|
|
|
"context"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/api/registry"
|
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-12 04:13:10 +00:00
|
|
|
)
|
2024-01-12 00:11:53 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func RegisterApis() {
|
|
|
|
registry.Register(registry.APIEntry{
|
2024-01-28 07:51:35 +00:00
|
|
|
Key: "s5",
|
|
|
|
Module: S5Module,
|
|
|
|
InitFunc: InitS5Api,
|
2024-01-28 07:20:59 +00:00
|
|
|
})
|
2024-01-12 00:11:53 +00:00
|
|
|
}
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func getModulesBasedOnConfig() []fx.Option {
|
|
|
|
var modules []fx.Option
|
|
|
|
for _, entry := range registry.GetRegistry() {
|
|
|
|
if viper.GetBool("protocols." + entry.Key + ".enabled") {
|
|
|
|
modules = append(modules, entry.Module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return modules
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildApis(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)
|
|
|
|
if entry.InitFunc != nil {
|
|
|
|
options = append(options, fx.Invoke(entry.InitFunc))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-01-28 08:03:04 +00:00
|
|
|
return fx.Module("api", fx.Options(options...))
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func SetupLifecycles(lifecycle fx.Lifecycle, protocols []registry.API) {
|
|
|
|
for _, entry := range registry.GetRegistry() {
|
|
|
|
for _, protocol := range 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)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|