fin

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2020 License: Apache-2.0 Imports: 26 Imported by: 0

README

fin

write fabric2's chainCode like write a web server

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CC

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

func NewCC

func NewCC(engine *Engine) *CC

func (*CC) Init

func (c *CC) Init(stub cc.ChaincodeStubInterface) peer.Response

func (*CC) Invoke

func (c *CC) Invoke(stub cc.ChaincodeStubInterface) peer.Response

type Context

type Context struct {
	context.Context

	Stub   cc.ChaincodeStubInterface
	Writer shim.ResponseWriter

	// Keys is a key/value pair exclusively for the context of each request.
	Keys map[string]interface{}

	Error error

	RoutePath string

	Params Params
	Init   bool
	// contains filtered or unexported fields
}

Context is the most important part. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.

func (*Context) Abort

func (c *Context) Abort()

Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails (ex: the password does not match), call Abort to ensure the remaining handlers for this request are not called.

func (*Context) AbortWithStatus

func (c *Context) AbortWithStatus(code int)

AbortWithStatus calls `Abort()` and writes the headers with the specified status code. For example, a failed attempt to authenticate a request could use: context.AbortWithStatus(401).

func (*Context) Bind

func (c *Context) Bind(obj interface{}) error

func (*Context) BindWith

func (c *Context) BindWith(obj interface{}, b binding.Binding) error

BindWith bind req arg with parser.

func (*Context) Bytes

func (c *Context) Bytes(code int, data ...[]byte)

Bytes writes some data into the body stream and updates the HTTP code.

func (*Context) Get

func (c *Context) Get(key string) (value interface{}, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)

func (*Context) GetBool

func (c *Context) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*Context) GetFloat64

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*Context) GetInt

func (c *Context) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*Context) GetInt64

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*Context) GetString

func (c *Context) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*Context) GetUint

func (c *Context) GetUint(key string) (ui uint)

GetUint returns the value associated with the key as an unsigned integer.

func (*Context) GetUint64

func (c *Context) GetUint64(key string) (ui64 uint64)

GetUint64 returns the value associated with the key as an unsigned integer.

func (*Context) IsAborted

func (c *Context) IsAborted() bool

IsAborted returns true if the current context was aborted.

func (*Context) JSON

func (c *Context) JSON(data interface{}, err error)

JSON serializes the given struct as JSON into the response body.

func (*Context) JSONMap

func (c *Context) JSONMap(data map[string]interface{}, err error)

JSONMap serializes the given map as map JSON into the response body.

func (*Context) Next

func (c *Context) Next()

Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler. See example in godoc.

func (*Context) Protobuf

func (c *Context) Protobuf(data proto.Message, err error)

Protobuf serializes the given struct as PB into the response body.

func (*Context) Render

func (c *Context) Render(code int, r render.Render)

Render pb response with shim code by a render instance.

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set is used to store a new key/value pair exclusively for this context. It also lazy initializes c.Keys if it was not used previously.

func (*Context) Status

func (c *Context) Status(code int)

Status sets the pb response code.

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

String writes the given string into the response body.

func (*Context) XML

func (c *Context) XML(data interface{}, err error)

XML serializes the given struct as XML into the response body.

type Engine

type Engine struct {
	RouterGroup

	// If enabled, the url.RawPath will be used to find parameters.
	UseRawPath bool

	// If true, the path value will be unescaped.
	// If UseRawPath is false (by default), the UnescapePathValues effectively is true,
	// as url.Path gonna be used, which is already unescaped.
	UnescapePathValues bool

	// If enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool
	// contains filtered or unexported fields
}

Engine is the framework's instance, it contains the muxer, middleware and configuration settings. Create an instance of Engine, by using New() or Default()

func DefaultServer

func DefaultServer(conf *ServerConfig) *Engine

DefaultServer returns an Engine instance with the Recovery and Logger middleware already attached.

func NewServer

func NewServer(conf *ServerConfig) *Engine

NewServer returns a new blank Engine instance without any middleware attached.

func (*Engine) InitMethod

func (engine *Engine) InitMethod(handler ...HandlerFunc)

InitMethod sets the handlers called when chaincode init.

func (*Engine) Inject

func (engine *Engine) Inject(pattern string, handlers ...HandlerFunc)

Inject is

func (*Engine) NoMethod

func (engine *Engine) NoMethod(handlers ...HandlerFunc)

NoMethod sets the handlers called when... TODO.

func (*Engine) NoRoute

func (engine *Engine) NoRoute(handlers ...HandlerFunc)

NoRoute adds handlers for NoRoute. It return a 404 code by default.

func (*Engine) Run

func (engine *Engine) Run() (err error)

func (*Engine) RunServer

func (engine *Engine) RunServer(srv *cc.ChaincodeServer) (err error)

func (*Engine) Serve

func (engine *Engine) Serve(stub cc.ChaincodeStubInterface) *Context

func (*Engine) SetConfig

func (engine *Engine) SetConfig(conf *ServerConfig) (err error)

SetConfig is used to set the engine configuration. Only the valid config will be loaded.

func (*Engine) SetMethodConfig

func (engine *Engine) SetMethodConfig(path string, mc *MethodConfig)

SetMethodConfig is used to set config on specified path

func (*Engine) Start

func (engine *Engine) Start() error

Start listen and serve fin engine by given flag.

func (*Engine) Use

func (engine *Engine) Use(middleware ...Handler) IRoutes

Use attachs a global middleware to the router. ie. the middleware attached though Use() will be included in the handlers chain for every single request. Even 404, 405, static files... For example, this is the right place for a logger or error management middleware.

func (*Engine) UseFunc

func (engine *Engine) UseFunc(middleware ...HandlerFunc) IRoutes

UseFunc attachs a global middleware to the router. ie. the middleware attached though UseFunc() will be included in the handlers chain for every single request. Even 404, 405, static files... For example, this is the right place for a logger or error management middleware.

type H

type H map[string]interface{}

type Handler

type Handler interface {
	Serve(c *Context)
}

Handler responds to a request.

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc request handler function.

func Logger

func Logger() HandlerFunc

Logger is logger middleware

func Recovery

func Recovery() HandlerFunc

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.

func (HandlerFunc) Serve

func (h HandlerFunc) Serve(c *Context)

type IRouter

type IRouter interface {
	IRoutes
	Group(string, ...HandlerFunc) *RouterGroup
}

IRouter http router framework interface.

type IRoutes

type IRoutes interface {
	UseFunc(...HandlerFunc) IRoutes
	Use(...Handler) IRoutes

	Handle(string, ...HandlerFunc) IRoutes
}

IRoutes http router interface.

type MethodConfig

type MethodConfig struct {
	Timeout time.Duration
}

MethodConfig is the fin server's method config model

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) (va string)

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

func (Params) Get

func (ps Params) Get(name string) (string, bool)

Get returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

type RouterGroup

type RouterGroup struct {
	Handlers []HandlerFunc
	// contains filtered or unexported fields
}

RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix and an array of handlers (middleware).

func (*RouterGroup) BasePath

func (group *RouterGroup) BasePath() string

BasePath router group base path.

func (*RouterGroup) Group

func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup

Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix. For example, all the routes that use a common middlware for authorization could be grouped.

func (*RouterGroup) Handle

func (group *RouterGroup) Handle(relativePath string, handlers ...HandlerFunc) IRoutes

Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in doc.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*RouterGroup) Use

func (group *RouterGroup) Use(middleware ...Handler) IRoutes

Use adds middleware to the group, see example code in doc.

func (*RouterGroup) UseFunc

func (group *RouterGroup) UseFunc(middleware ...HandlerFunc) IRoutes

UseFunc adds middleware to the group, see example code in doc.

type ServerConfig

type ServerConfig struct {
	CCID                  string
	Address               string
	TLSProps              cc.TLSProperties
	MaxConnectionIdle     time.Duration
	MaxConnectionAge      time.Duration
	MaxConnectionAgeGrace time.Duration
	Time                  time.Duration
	Timeout               time.Duration
}

ServerConfig is the fin server config model

Directories

Path Synopsis
Package render is a generated protocol buffer package.
Package render is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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