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"
|
2024-02-16 13:39:55 +00:00
|
|
|
"slices"
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/config"
|
|
|
|
|
2024-02-16 13:39:55 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/api/middleware"
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/api/registry"
|
|
|
|
"go.uber.org/fx"
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
2024-01-12 00:11:53 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
var alwaysEnabled = []string{"account"}
|
|
|
|
|
|
|
|
func BuildApis(cm *config.Manager) fx.Option {
|
2024-01-28 07:20:59 +00:00
|
|
|
var options []fx.Option
|
2024-02-22 07:09:31 +00:00
|
|
|
enabledProtocols := cm.Viper().GetStringSlice("core.protocols")
|
2024-01-28 07:20:59 +00:00
|
|
|
for _, entry := range registry.GetRegistry() {
|
2024-02-22 07:09:31 +00:00
|
|
|
if slices.Contains(enabledProtocols, entry.Key) || slices.Contains(alwaysEnabled, entry.Key) {
|
2024-01-28 07:20:59 +00:00
|
|
|
options = append(options, entry.Module)
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-02-17 10:23:33 +00:00
|
|
|
type initParams struct {
|
|
|
|
fx.In
|
|
|
|
Protocols []registry.API `group:"api"`
|
|
|
|
}
|
|
|
|
|
|
|
|
options = append(options, fx.Invoke(func(params initParams) error {
|
|
|
|
for _, protocol := range params.Protocols {
|
2024-02-16 13:39:55 +00:00
|
|
|
err := protocol.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2024-02-17 10:23:33 +00:00
|
|
|
options = append(options, fx.Invoke(func(params initParams) error {
|
|
|
|
for _, protocol := range params.Protocols {
|
2024-02-17 08:04:15 +00:00
|
|
|
routes, err := protocol.Routes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-22 07:09:31 +00:00
|
|
|
middleware.RegisterProtocolSubdomain(cm, routes, protocol.Name())
|
2024-02-16 13:39:55 +00:00
|
|
|
}
|
2024-02-17 08:04:15 +00:00
|
|
|
|
|
|
|
return nil
|
2024-02-16 13:39:55 +00:00
|
|
|
}))
|
|
|
|
|
2024-01-28 10:22:11 +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 09:45:34 +00:00
|
|
|
type LifecyclesParams struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
Protocols []registry.API `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:45:34 +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 04:13:10 +00:00
|
|
|
}
|