feat: new event

This commit is contained in:
Rogee
2025-01-15 19:30:01 +08:00
parent b5d6593fa4
commit 57f8e95d50
7 changed files with 185 additions and 60 deletions

View File

@@ -0,0 +1,48 @@
package subscribers
import (
"encoding/json"
"{{.ModuleName}}/app/events"
"{{.ModuleName}}/app/events/publishers"
"git.ipao.vip/rogeecn/atom/contracts"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/sirupsen/logrus"
)
var _ contracts.EventHandler = (*{{.Name}}Subscriber)(nil)
// @provider(event)
type {{.Name}}Subscriber struct {
log *logrus.Entry `inject:"false"`
}
func (e *{{.Name}}Subscriber) Prepare() error {
e.log = logrus.WithField("module", "events.subscribers.{{.Name}}Subscriber")
return nil
}
// PublishToTopic implements contracts.EventHandler.
func (e *{{.Name}}Subscriber) PublishToTopic() string {
return events.TopicProcessed
}
// Topic implements contracts.EventHandler.
func (e *{{.Name}}Subscriber) Topic() string {
return events.Topic{{.Name}}
}
// Handler implements contracts.EventHandler.
func (e *{{.Name}}Subscriber) Handler(msg *message.Message) ([]*message.Message, error) {
var payload publishers.{{.Name}}Event
err := json.Unmarshal(msg.Payload, &payload)
if err != nil {
return nil, err
}
e.log.Infof("received event %s", msg.Payload)
// TODO: handle post deletion
return nil, nil
}