66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package event
|
|
|
|
import (
|
|
"context"
|
|
|
|
"quyun/v2/app/commands"
|
|
"quyun/v2/app/errorx"
|
|
"quyun/v2/app/events/subscribers"
|
|
"quyun/v2/providers/app"
|
|
"quyun/v2/providers/event"
|
|
"quyun/v2/providers/postgres"
|
|
|
|
"go.ipao.vip/atom"
|
|
"go.ipao.vip/atom/container"
|
|
"go.ipao.vip/atom/contracts"
|
|
|
|
logrus "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"go.uber.org/dig"
|
|
)
|
|
|
|
func defaultProviders() container.Providers {
|
|
return commands.Default(container.Providers{
|
|
postgres.DefaultProvider(),
|
|
}...)
|
|
}
|
|
|
|
func Command() atom.Option {
|
|
return atom.Command(
|
|
atom.Name("event"),
|
|
atom.Short("start event processor"),
|
|
atom.RunE(Serve),
|
|
atom.Providers(
|
|
defaultProviders().
|
|
With(
|
|
subscribers.Provide,
|
|
),
|
|
),
|
|
)
|
|
}
|
|
|
|
type Service struct {
|
|
dig.In
|
|
|
|
App *app.Config
|
|
PubSub *event.PubSub
|
|
Initials []contracts.Initial `group:"initials"`
|
|
}
|
|
|
|
func Serve(_ *cobra.Command, _ []string) error {
|
|
err := container.Container.Invoke(func(ctx context.Context, svc Service) error {
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
if svc.App.IsDevMode() {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
}
|
|
|
|
return svc.PubSub.Serve(ctx)
|
|
})
|
|
if err != nil {
|
|
return errorx.ErrOperationFailed.WithCause(err)
|
|
}
|
|
|
|
return nil
|
|
}
|