fix: issues
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/ThreeDotsLabs/watermill"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var _ contracts.EventHandler = (*UserRegister)(nil)
|
||||
|
||||
type Event struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
type ProcessedEvent struct {
|
||||
ProcessedID int `json:"processed_id"`
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
// @provider(event)
|
||||
type UserRegister struct {
|
||||
log *logrus.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (u *UserRegister) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handler implements contracts.EventHandler.
|
||||
func (u *UserRegister) Handler(msg *message.Message) ([]*message.Message, error) {
|
||||
consumedPayload := Event{}
|
||||
err := json.Unmarshal(msg.Payload, &consumedPayload)
|
||||
if err != nil {
|
||||
// When a handler returns an error, the default behavior is to send a Nack (negative-acknowledgement).
|
||||
// The message will be processed again.
|
||||
//
|
||||
// You can change the default behaviour by using middlewares, like Retry or PoisonQueue.
|
||||
// You can also implement your own middleware.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("received event %+v\n", consumedPayload)
|
||||
|
||||
newPayload, err := json.Marshal(ProcessedEvent{
|
||||
ProcessedID: consumedPayload.ID,
|
||||
Time: time.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newMessage := message.NewMessage(watermill.NewUUID(), newPayload)
|
||||
|
||||
return nil, nil
|
||||
return []*message.Message{newMessage}, nil
|
||||
}
|
||||
|
||||
// PublishToTopic implements contracts.EventHandler.
|
||||
func (u *UserRegister) PublishToTopic() string {
|
||||
return "event:processed"
|
||||
}
|
||||
|
||||
// Topic implements contracts.EventHandler.
|
||||
func (u *UserRegister) Topic() string {
|
||||
return "event:user-register"
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"backend/providers/events"
|
||||
"backend/providers/event"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
__event *events.PubSub,
|
||||
__event *event.PubSub,
|
||||
) (contracts.Initial, error) {
|
||||
obj := &UserRegister{}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
|
||||
174
backend/app/events/topics.gen.go
Normal file
174
backend/app/events/topics.gen.go
Normal file
@@ -0,0 +1,174 @@
|
||||
// Code generated by go-enum DO NOT EDIT.
|
||||
// Version: -
|
||||
// Revision: -
|
||||
// Build Date: -
|
||||
// Built By: -
|
||||
|
||||
package events
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// TopicProcessed is a Topic of type Processed.
|
||||
TopicProcessed Topic = "event:processed"
|
||||
// TopicUserRegister is a Topic of type UserRegister.
|
||||
TopicUserRegister Topic = "user:register"
|
||||
)
|
||||
|
||||
var ErrInvalidTopic = fmt.Errorf("not a valid Topic, try [%s]", strings.Join(_TopicNames, ", "))
|
||||
|
||||
var _TopicNames = []string{
|
||||
string(TopicProcessed),
|
||||
string(TopicUserRegister),
|
||||
}
|
||||
|
||||
// TopicNames returns a list of possible string values of Topic.
|
||||
func TopicNames() []string {
|
||||
tmp := make([]string, len(_TopicNames))
|
||||
copy(tmp, _TopicNames)
|
||||
return tmp
|
||||
}
|
||||
|
||||
// TopicValues returns a list of the values for Topic
|
||||
func TopicValues() []Topic {
|
||||
return []Topic{
|
||||
TopicProcessed,
|
||||
TopicUserRegister,
|
||||
}
|
||||
}
|
||||
|
||||
// String implements the Stringer interface.
|
||||
func (x Topic) String() string {
|
||||
return string(x)
|
||||
}
|
||||
|
||||
// IsValid provides a quick way to determine if the typed value is
|
||||
// part of the allowed enumerated values
|
||||
func (x Topic) IsValid() bool {
|
||||
_, err := ParseTopic(string(x))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
var _TopicValue = map[string]Topic{
|
||||
"event:processed": TopicProcessed,
|
||||
"user:register": TopicUserRegister,
|
||||
}
|
||||
|
||||
// ParseTopic attempts to convert a string to a Topic.
|
||||
func ParseTopic(name string) (Topic, error) {
|
||||
if x, ok := _TopicValue[name]; ok {
|
||||
return x, nil
|
||||
}
|
||||
return Topic(""), fmt.Errorf("%s is %w", name, ErrInvalidTopic)
|
||||
}
|
||||
|
||||
var errTopicNilPtr = errors.New("value pointer is nil") // one per type for package clashes
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (x *Topic) Scan(value interface{}) (err error) {
|
||||
if value == nil {
|
||||
*x = Topic("")
|
||||
return
|
||||
}
|
||||
|
||||
// A wider range of scannable types.
|
||||
// driver.Value values at the top of the list for expediency
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
*x, err = ParseTopic(v)
|
||||
case []byte:
|
||||
*x, err = ParseTopic(string(v))
|
||||
case Topic:
|
||||
*x = v
|
||||
case *Topic:
|
||||
if v == nil {
|
||||
return errTopicNilPtr
|
||||
}
|
||||
*x = *v
|
||||
case *string:
|
||||
if v == nil {
|
||||
return errTopicNilPtr
|
||||
}
|
||||
*x, err = ParseTopic(*v)
|
||||
default:
|
||||
return errors.New("invalid type for Topic")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (x Topic) Value() (driver.Value, error) {
|
||||
return x.String(), nil
|
||||
}
|
||||
|
||||
// Set implements the Golang flag.Value interface func.
|
||||
func (x *Topic) Set(val string) error {
|
||||
v, err := ParseTopic(val)
|
||||
*x = v
|
||||
return err
|
||||
}
|
||||
|
||||
// Get implements the Golang flag.Getter interface func.
|
||||
func (x *Topic) Get() interface{} {
|
||||
return *x
|
||||
}
|
||||
|
||||
// Type implements the github.com/spf13/pFlag Value interface.
|
||||
func (x *Topic) Type() string {
|
||||
return "Topic"
|
||||
}
|
||||
|
||||
type NullTopic struct {
|
||||
Topic Topic
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func NewNullTopic(val interface{}) (x NullTopic) {
|
||||
err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
|
||||
_ = err // make any errcheck linters happy
|
||||
return
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (x *NullTopic) Scan(value interface{}) (err error) {
|
||||
if value == nil {
|
||||
x.Topic, x.Valid = Topic(""), false
|
||||
return
|
||||
}
|
||||
|
||||
err = x.Topic.Scan(value)
|
||||
x.Valid = (err == nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (x NullTopic) Value() (driver.Value, error) {
|
||||
if !x.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
// driver.Value accepts int64 for int values.
|
||||
return string(x.Topic), nil
|
||||
}
|
||||
|
||||
type NullTopicStr struct {
|
||||
NullTopic
|
||||
}
|
||||
|
||||
func NewNullTopicStr(val interface{}) (x NullTopicStr) {
|
||||
x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
|
||||
return
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (x NullTopicStr) Value() (driver.Value, error) {
|
||||
if !x.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return x.Topic.String(), nil
|
||||
}
|
||||
10
backend/app/events/topics.go
Normal file
10
backend/app/events/topics.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package events
|
||||
|
||||
// swagger:enum Topic
|
||||
// ENUM(
|
||||
//
|
||||
// Processed = "event:processed"
|
||||
// UserRegister = "user:register"
|
||||
//
|
||||
// )
|
||||
type Topic string
|
||||
52
backend/app/events/user_register.go
Normal file
52
backend/app/events/user_register.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
_ contracts.EventHandler = (*UserRegister)(nil)
|
||||
_ contracts.EventPublisher = (*UserRegister)(nil)
|
||||
)
|
||||
|
||||
// @provider(event)
|
||||
type UserRegister struct {
|
||||
log *logrus.Entry `inject:"false" json:"-"`
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
func (e *UserRegister) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Marshal implements contracts.EventPublisher.
|
||||
func (e *UserRegister) Marshal() ([]byte, error) {
|
||||
return json.Marshal(e)
|
||||
}
|
||||
|
||||
// PublishToTopic implements contracts.EventHandler.
|
||||
func (e *UserRegister) PublishToTopic() string {
|
||||
return TopicProcessed.String()
|
||||
}
|
||||
|
||||
// Topic implements contracts.EventHandler.
|
||||
func (e *UserRegister) Topic() string {
|
||||
return TopicUserRegister.String()
|
||||
}
|
||||
|
||||
// Handler implements contracts.EventHandler.
|
||||
func (e *UserRegister) Handler(msg *message.Message) ([]*message.Message, error) {
|
||||
var payload UserRegister
|
||||
err := json.Unmarshal(msg.Payload, &payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e.log.Infof("received event %+v\n", payload)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
Reference in New Issue
Block a user