rocha

package module
v0.0.0-...-c8ec2d3 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 3 Imported by: 2

README

Rocha

Build Status codecov Go Report Card GoDoc

Rocha is Hyperledger Fabric Chaincode Router with Middleware capabilities

Currently in development

Usage

r := rocha.NewRouter().
    // Route method `QueryUsers` to QueryUsers function
    Handle("QueryUsers", QueryUsers)
    // Route method `SaveUser` receiving a string and integer parameters
    Handle("SaveUser", SaveUser,
        argsmw.Arguments(
            argsmw.String("name"),
            argsmw.Int("age"))).

func SaveUser(c rocha.Context) pb.Response {
    name := c.String("name")
    age := c.Int("age")

    if age < 18 {
        return shim.Error("can't register user")
    }

    stub := c.Stub()

    // ...
}

Documentation

Overview

Package rocha is a Hyperledger Fabric Chaincode Router with Middleware support

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotFoundHandler

func NotFoundHandler(c Context) pb.Response

NotFoundHandler is a simple handler which returns a not found message with the method's name

Types

type Context

type Context interface {
	// Method returns the method's name
	Method() (method string)

	// Args returns the sent arguments
	Args() (args []string)

	// Stub returns the internal ChaincodeStub for this context
	Stub() shim.ChaincodeStubInterface

	// Set stores a value in the current context
	Set(key string, value interface{})

	// Get returns a value in the current context
	Get(key string) (value interface{}, exists bool)

	// Value returns a value stored in the context like Get, but does no
	// checking if the value actually exists
	Value(key string) (value interface{})

	// String returns a value stored in the context casted to a string.
	// if no value is found, a empty string is returned
	String(key string) (value string)

	// Int returns a value stored in the context casted to a int. If no
	// value is found, a zero int is returned
	Int(key string) (value int)
}

Context contains all necessary functions for interacting with the underlying blockchain infrastructure and a simple key-value store for middleware/application internal usage

func NewContext

func NewContext(stub shim.ChaincodeStubInterface, method string, args []string) Context

NewContext creates a new context with the middlewares and handlers to be called

type Handler

type Handler func(c Context) pb.Response

Handler is a function called to process a chaincode request

func Chain

func Chain(h Handler, middlewares ...Middleware) Handler

Chain chains a series of middlewares to a new Handler. Middlewares are chained in the reverse order. If this function is called with

rocha.Chain(h, m4, m3, m2, m1) == m1(m2(m3(m4(h))))

type Middleware

type Middleware func(next Handler) Handler

Middleware is function which wraps a Handler with some functionality, returning a new Handler with wrapped code

type Router

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

Router register routes to be invoked according to their method names

func NewRouter

func NewRouter() *Router

NewRouter creates a new Router

func (*Router) Handle

func (r *Router) Handle(method string, handler Handler, middlewares ...Middleware) *Router

Handle adds a new route to the router, overwriting a previous router by this name

func (*Router) Invoke

func (r *Router) Invoke(stub shim.ChaincodeStubInterface, method string, args []string) pb.Response

Invoke invokes the handler

func (*Router) NotFoundHandler

func (r *Router) NotFoundHandler(handler Handler) *Router

NotFoundHandler sets a not found handler for this router

func (*Router) Use

func (r *Router) Use(middlewares ...Middleware) *Router

Use adds a new middleware

Directories

Path Synopsis
Package argsmw is a chaincode argument parser and validator middleware
Package argsmw is a chaincode argument parser and validator middleware

Jump to

Keyboard shortcuts

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