router

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2021 License: Apache-2.0 Imports: 4 Imported by: 2

README

lambda-router

Lightweight wrapper for routing lambda events to handling functions.

Usage

Initialize a router and register handlers, then pass to lamda.Start.

router := New()
router.Route("Do", handleDo)
lambda.Start(router.Handle)
Custom errors

If your error values contain rich content which you want to make available to calling lambda functions, provide your own error marshaling function.

router := New(MarshalErrorsWith(func(error) ([]byte, error) {
	// Marshal your error.
	return marshaled, nil
}))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler interface {
	Handle(context.Context, json.RawMessage) (json.RawMessage, error)
}

Handler implementations will be called by the Router to handle requests made to the associated procedure.

type HandlerFunc

type HandlerFunc func(context.Context, json.RawMessage) (json.RawMessage, error)

The HandlerFunc type is an adapter to allow the use of ordinary functions as Lambda handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, body json.RawMessage) (json.RawMessage, error)

Handle calls f(ctx, body).

type Option

type Option func(*Router)

Option implementations can mutate the Router to configure how events should be handled.

func MarshalErrorsWith

func MarshalErrorsWith(f func(error) (json.RawMessage, error)) Option

MarshalErrorsWith configures the Router to use the passed function to marshal errors returned from a Handler. This allows streaming additional content which may be included in your error value. If a custom marshaling function isn't provided only the error message will be propagated to the caller. If your marshaling function fails it should return an error; the Router will propagate the original err.Error() in this case.

type Request

type Request struct {
	Procedure string          `json:"procedure"`
	Body      json.RawMessage `json:"body"`
}

Request is the expected structure of an event which can be routed. It contains a Procedure field which names the Handler which should handle the request, and the body to be passed to the handler.

type Response

type Response struct {
	Body  json.RawMessage `json:"body,omitempty"`
	Error json.RawMessage `json:"error,omitempty"`
}

Response is returned from the Router.Handle function and is the form of the event which will returned from the lambda invoke function. NOTE: Errors returned from a Handler won't be propagated, instead they're marshaled to json and streamed as part of the response.

type Router

type Router struct {
	Routes map[string]Handler
	// contains filtered or unexported fields
}

Router exposes a 'Handle' method which should be passed to 'lambda.Start'. It does the work of unwrapping the request event and passing it to the relevant Handler, and wrapping the response, or error returned.

func New

func New(opts ...Option) *Router

New initializes a Router instance with the options passed.

func (*Router) Handle

func (r *Router) Handle(ctx context.Context, req Request) (*Response, error)

Handle should be passed to 'lambda.Start' to handle inbound requests.

func (*Router) Route

func (r *Router) Route(procedure string, handler Handler)

Route registers the passed Handler to the procedure name. NOTE: If multiple Handlers are registered to the same procedure, only the last registered will be called.

Jump to

Keyboard shortcuts

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