commandbus

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2019 License: MIT Imports: 4 Imported by: 0

README

GO CommandBus

Build Status Codecov branch GoDoc Go Report Card License

A slight and pluggable command-bus for Go.

Install

Use go get.

$ go get github.com/lana/go-commandbus

Then import the package into your own code:

import "github.com/lana/go-commandbus"

Usage

package main

import (
	"context"
	"log"

	"github.com/lana/go-commandbus"
)

type CreateUser struct {
	Name string
}

func CreateHandler(ctx context.Context, cmd *CreateUser) error {
	log.Printf("user %s created", cmd.Name)

	return nil
}

func main() {
	bus := commandbus.New()

	err := bus.Register(&CreateUser{}, CreateHandler)

	if err != nil {
		log.Fatal(err)
	}

	err = bus.Execute(context.Background(), &CreateUser{"go-commandbus"})

	if err != nil {
		log.Fatal(err)
	}
}

License

This project is released under the MIT licence. See LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommandBus

type CommandBus interface {
	// Register assign a command to a command handle for future executions.
	Register(interface{}, interface{}) error

	// Handlers returns all registered handlers.
	Handlers() handlers

	// Use adds middleware to the chain.
	Use(...MiddlewareFunc)

	// Execute send a given Command to its assigned command handler.
	Execute(context.Context, interface{}) error
}

CommandBus is the definition of how command should be handled

func New

func New() CommandBus

type HandlerFunc

type HandlerFunc func(context.Context, interface{}) error

HandlerFunc defines a function to execute the command handler. This function type is only used by `MiddlewareFunc`.

type MiddlewareFunc

type MiddlewareFunc func(h HandlerFunc) HandlerFunc

MiddlewareFunc defines a function to process middleware. it receives the next Handler must return another handler

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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