portal/api/api.go

86 lines
1.8 KiB
Go
Raw Normal View History

2024-01-12 00:11:53 +00:00
package api
import (
"context"
"slices"
"git.lumeweb.com/LumeWeb/portal/api/middleware"
2024-02-14 00:07:24 +00:00
"git.lumeweb.com/LumeWeb/portal/api/account"
"git.lumeweb.com/LumeWeb/portal/api/registry"
"git.lumeweb.com/LumeWeb/portal/api/s5"
"github.com/spf13/viper"
"go.uber.org/fx"
)
2024-01-12 00:11:53 +00:00
func RegisterApis() {
registry.Register(registry.APIEntry{
Key: "s5",
Module: s5.Module,
})
2024-02-14 00:07:24 +00:00
registry.Register(registry.APIEntry{
Key: "account",
Module: account.Module,
2024-02-14 00:07:24 +00:00
})
2024-01-12 00:11:53 +00:00
}
func BuildApis(config *viper.Viper) fx.Option {
var options []fx.Option
enabledProtocols := config.GetStringSlice("core.protocols")
for _, entry := range registry.GetRegistry() {
if slices.Contains(enabledProtocols, entry.Key) {
options = append(options, entry.Module)
}
}
options = append(options, fx.Invoke(func(protocols []registry.API) error {
for _, protocol := range protocols {
err := protocol.Init()
if err != nil {
return err
}
}
return nil
}))
options = append(options, fx.Invoke(func(protocols []registry.API) error {
for _, protocol := range protocols {
routes, err := protocol.Routes()
if err != nil {
return err
}
middleware.RegisterProtocolSubdomain(config, routes, protocol.Name())
}
return nil
}))
return fx.Module("api", fx.Options(options...))
}
2024-01-28 09:45:34 +00:00
type LifecyclesParams struct {
fx.In
Protocols []registry.API `group:"protocol"`
}
func SetupLifecycles(lifecycle fx.Lifecycle, params LifecyclesParams) error {
for _, entry := range registry.GetRegistry() {
2024-01-28 09:45:34 +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
}