eventsio

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 4 Imported by: 0

README

EventsIO

EventsIO is a simple and flexible library designed to handle events within G3deon. Its main goal is to centralize event logic in the application, providing a unified interface for different messaging implementations.

Features

  • Simple Interface: Intuitive API for publishing and subscribing to events
  • Multiple Implementations: Support for different messaging backends
    • RabbitMQ (implemented)
    • Kafka (coming soon)
    • Memory (for testing)
  • Flexibility: Allows advanced options when needed
  • Thread-Safe: Concurrency-safe implementation
  • JSON Serialization: Automatic event serialization/deserialization

Installation

go get github.com/g3deon/aventsio

Getting Started

Pre-commit Hook Setup

To ensure code quality, the project includes a pre-commit hook that runs:

  • Go code formatting (go fmt)
  • Linting with golangci-lint
  • Unit tests (go test)

To set up the hook:

# Configure hooks directory
git config core.hooksPath githooks

# Give execution permissions to the script
git add githooks/pre-commit.sh
git update-index --chmod=+x githooks/pre-commit.sh

Now, every time you try to commit, these checks will run automatically.

Basic Usage
import (
    "github.com/g3deon/aventsio"
    "github.com/g3deon/aventsio/adapters/memory"
)

// Create in-memory event bus
bus := memory.NewEventBus()

// Publish event
event := eventsio.NewEvent("event-id")
bus.Publish("users", event)

// Subscribe to events
bus.Subscribe("users", func(event eventsio.Event) {
    fmt.Printf("Received event: %s\n", event.GetID())
})

Advanced Options

With Retries
// Publish with automatic retry
bus.Publish("users", event, eventsio.WithRetry())
// Retry count its increment every time the event its re-delivered
bus.Subscribe("users", func(event eventsio.Event) {
    fmt.Printf(
      "Received event: %s\n, with re-try: %d", 
      event.GetID(), 
      event.GetRetryCount(),
    )
})

More comming soon...

Event Structure

Each event contains:

  • Unique ID
  • Send timestamp
  • Customizable headers
  • Event-specific data

Contributing

Contributions are welcome. Please ensure to:

  1. Follow existing code conventions
  2. Add tests for new functionality
  3. Update documentation as needed

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseEvent

type BaseEvent struct {
	ID      string         `json:"id"`
	SendAt  time.Time      `json:"sendAt"`
	Headers map[string]any `json:"headers"`
	// contains filtered or unexported fields
}

func NewEvent

func NewEvent(id string) *BaseEvent

func (*BaseEvent) GetHeader

func (e *BaseEvent) GetHeader(key string) any

func (*BaseEvent) GetHeaders

func (e *BaseEvent) GetHeaders() map[string]any

func (*BaseEvent) GetID

func (e *BaseEvent) GetID() string

func (*BaseEvent) GetSendAt

func (e *BaseEvent) GetSendAt() time.Time

func (*BaseEvent) Marshal

func (e *BaseEvent) Marshal() ([]byte, error)

func (*BaseEvent) SetHeader

func (e *BaseEvent) SetHeader(key string, value any)

func (*BaseEvent) SetID

func (e *BaseEvent) SetID(id string)

func (*BaseEvent) SetSendAt

func (e *BaseEvent) SetSendAt(sendAt time.Time)

func (*BaseEvent) String

func (e *BaseEvent) String() string

func (*BaseEvent) Unmarshal

func (e *BaseEvent) Unmarshal(data []byte) error

type Event

type Event interface {
	GetID() string
	GetSendAt() time.Time
	GetHeader(key string) any
	SetHeader(key string, value any)
	GetHeaders() map[string]any
	Marshal() ([]byte, error)
	Unmarshal([]byte) error
}

type EventBus

type EventBus interface {
	Publish(topic string, event Event) error
	Subscribe(topic string, handler func(event Event)) error
}

type PublishOption

type PublishOption func(*PublishOptions)

func WithRetry

func WithRetry() PublishOption

type PublishOptions

type PublishOptions struct {
	// contains filtered or unexported fields
}

func (*PublishOptions) IsRetry

func (o *PublishOptions) IsRetry() bool

Directories

Path Synopsis
adapters

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL