Skip to content

Edwin-Luijten/go_command_bus

Repository files navigation

A Command Bus for GO

GitHub go.mod Go version Go Reference Build Status Maintainability Test Coverage

Installation

go get github.com/edwin-luijten/go_command_bus

Usage

Commands

import (
    commandbus "github.com/edwin-luijten/go_command_bus"
)

func main() {
    bus := commandbus.New()
    
    bus.RegisterHandler(&RegisterUserCommand{}, func(command interface{}) {
        cmd := command.(*RegisterUserCommand)
        
        fmt.Sprintf("Registered %s", cmd.Username)
    })
    
    bus.Handle(&RegisterUserCommand{
    	Email: "jj@email.com",
    	Password: "secret",
    })
}

Middleware's

A middleware has a handler and a priority.
Where priority of 0 is the least amount of priority.

import (
    commandbus "github.com/edwin-luijten/go_command_bus"
)

func main() {
    bus := commandbus.New()
    
    bus.RegisterMiddleware(func(command interface{}, next HandlerFunc) {
        
    	// your logic here
    
        next(command)
    
        // or here
    }, 1) 
}