events: implement package with memory & nats streams (#1942)

This commit is contained in:
ben-toogood
2020-08-18 16:19:53 +01:00
committed by GitHub
parent 19ef225b2f
commit 21cca297c0
10 changed files with 1008 additions and 13 deletions

46
events/events.go Normal file
View File

@@ -0,0 +1,46 @@
// Package events contains interfaces for managing events within distributed systems
package events
import (
"encoding/json"
"errors"
"time"
)
var (
// ErrMissingTopic is returned if a blank topic was provided to publish
ErrMissingTopic = errors.New("Missing topic")
// ErrEncodingMessage is returned from publish if there was an error encoding the message option
ErrEncodingMessage = errors.New("Error encoding message")
)
// Stream of events
type Stream interface {
Publish(topic string, opts ...PublishOption) error
Subscribe(opts ...SubscribeOption) (<-chan Event, error)
}
// Store of events
type Store interface {
Read(opts ...ReadOption) ([]*Event, error)
Write(event *Event, opts ...WriteOption) error
}
// Event is the object returned by the broker when you subscribe to a topic
type Event struct {
// ID to uniquely identify the event
ID string
// Topic of event, e.g. "registry.service.created"
Topic string
// Timestamp of the event
Timestamp time.Time
// Metadata contains the encoded event was indexed by
Metadata map[string]string
// Payload contains the encoded message
Payload []byte
}
// Unmarshal the events message into an object
func (e *Event) Unmarshal(v interface{}) error {
return json.Unmarshal(e.Payload, v)
}