event
Last updated
Last updated
This directory contains the Event Handling Framework for Go applications, designed to streamline event-driven workflows in backend services. It provides a generic and modular way to handle event messages with flexible payloads and versioning support.
The Event Package provides a robust framework for handling event messages in Go applications. It allows for:
Defining and processing event messages with flexible, user-defined payload types.
Integration with HTTP frameworks (like Gin) via a generic EventHandler interface.
Separation of concerns: event message parsing, business logic handling, and HTTP integration are modular and distinct.
This package promotes reusable patterns across services to maintain consistency in event handling across your projects.
User-Defined Payload Types: Utilizes Go generics to allow flexible payload structures for event messages.
Generic Interface for Event Processing: Provides a flexible EventHandler
interface for defining how events are processed.
Modular Design: Separates event message logic, event handler logic, and HTTP integration for clean and maintainable code.
HTTP Integration: Includes helper functions for integrating with popular web frameworks like Gin.
To define an event, the framework expects a JSON structure with an event type, timestamp, and a flexible payload. The payload
section of the event can be a user-defined structure, providing the flexibility to handle different kinds of events.
example
event_type
: A string indicating the type of event.
timestamp
: An RFC3339 formatted timestamp.
payload
: An object containing the event data. This is where generics come into play, allowing for flexible payload types.
metadata
: An object containing metadata about the event, including the version
.
Using Go's generics, you can define your own payload type to represent the data for each event:
The event framework uses a generic EventHandler
interface to process events.
Define your business logic function that processes the event payload.
If you are using a web framework like Gin, you can integrate the event framework with the NewGinEventHandler
, which creates a gin.HandlerFunc
. This handler can then be injected into your Gin routes. The handler requires an EventHandler
instance to process the event.
The event framework is designed to be extensible. You can create custom event handlers to handle specific scenarios, such as events with callbacks, or complex retry mechanisms. For examples of custom handlers, see:
You can find a complete working example in the repository under .