middlewares

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package middlewares contains a set of tools used to handle input requests.

In order to add middlewares, it's recommended to use funcs that returns func (*Context), given consumer the possibility of using it later into Exec.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseBodyJson

func ParseBodyJson(body interface{}) func(c *Context)

ParseBodyJson will return a func that will decode request's body into the given interface. Its return is a func (c *Context) to be used into Context.Exec if needed.

func SendJSON

func SendJSON(c *Context)

SendJSON will write context's status and response as json format into the provided ResponseWriter

Types

type Context

type Context struct {
	Err      error
	Status   int
	Response interface{}

	Body interface{}
	// contains filtered or unexported fields
}

Context will store data cross handlers

func NewContext

func NewContext(
	req *http.Request,
	rw http.ResponseWriter,
	method string,
	headers map[string]string,
) (ctx *Context)

NewContext generates the context and validates if method and headers providad in requests match agains route's expected

func (*Context) Exec

func (c *Context) Exec(m func(*Context))

Exec will run the passed func only if no previous errors are detected into the context (to prevent useless or risky calls)

func (*Context) Recover

func (c *Context) Recover()

Recover should be run as defered and will wrap all panics that may happen and store into context's error

Jump to

Keyboard shortcuts

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