go get github.com/edwin-luijten/go_command_bus
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: "[email protected]",
Password: "secret",
})
}
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)
}