knife

package
v0.0.0-...-f70f7d2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2017 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SupressError bool

SupressError sets if http error should be sent

Functions

func HTTPRouterWrapHandler

func HTTPRouterWrapHandler(h http.Handler) httprouter.Handle

HTTPRouterWrapHandler wraps the http.Handler with a httprouter.Handle The httprouter.Handle supports better parse of URL params

func IsUnMarshalError

func IsUnMarshalError(err error) bool

IsUnMarshalError verifies if error is an UnMarshalError

func JSONContentTypeMiddleware

func JSONContentTypeMiddleware(next http.Handler) http.Handler

JSONContentTypeMiddleware forces application/json Content-Type

func MarshalJSON

func MarshalJSON(v interface{}) ([]byte, error)

MarshalJSON returns the JSON encoding of v

func PanicRecoverMiddleware

func PanicRecoverMiddleware(next http.Handler) http.Handler

PanicRecoverMiddleware recovers a panic error

func UnMarshalJSON

func UnMarshalJSON(data []byte, v interface{}) error

UnMarshalJSON parses the JSON-encoded data and stores in v interface

Types

type Body

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

Body represents the request body

func NewBody

func NewBody(r io.Reader) Body

NewBody creates a Body instance

func (Body) UnMarshalJSON

func (b Body) UnMarshalJSON(v interface{}) error

UnMarshalJSON parses the JSON into the body

type ErrorHandler

type ErrorHandler func(HandlerFunc) HandlerFunc

ErrorHandler represents the global error handler

type ErrorMessage

type ErrorMessage struct {
	Message []string `json:"errors"`
}

ErrorMessage represents errors sent as response

func NewErrorMessage

func NewErrorMessage(message ...string) ErrorMessage

NewErrorMessage creates a ErrorMessages instance

type HTTPMethod

type HTTPMethod func(string, httprouter.Handle)

HTTPMethod represents a http method

type Handler

type Handler interface {
	ServeHTTP(Response, *Request) (Response, error)
}

Handler defines an interface to be used by structs

type HandlerFunc

type HandlerFunc func(Response, *Request) (Response, error)

HandlerFunc represents the routes created from a function

type Middleware

type Middleware struct {
	Token       string
	Constructor alice.Constructor
	Silent      bool
}

Middleware represents a middleware

type MiddlewareManager

type MiddlewareManager struct {
	Middlewares []Middleware
}

MiddlewareManager represents the middleware manager. It can stores built-in middlewares or external middlewares. The middlewares are supported via alice package.

func NewMiddlewareManager

func NewMiddlewareManager() *MiddlewareManager

NewMiddlewareManager creates an instance of MiddlewareManager

func (*MiddlewareManager) Add

func (m *MiddlewareManager) Add(token string, constructor alice.Constructor)

Add adds a middleware to the middlewares chain

func (*MiddlewareManager) AddSilent

func (m *MiddlewareManager) AddSilent(token string, constructor alice.Constructor)

AddSilent adds a silent middleware to the middlewares chain

type MiddlewareMapper

type MiddlewareMapper struct {
	MiddlewaresMap MiddlewaresMap
}

MiddlewareMapper represents the mapper of middlewares and routes

func NewMiddlewareMapper

func NewMiddlewareMapper() *MiddlewareMapper

NewMiddlewareMapper creates an instance of NewMiddlewareMapper

func (*MiddlewareMapper) Map

func (mapper *MiddlewareMapper) Map(routeToken string, middlewareTokens ...string)

Map maps middlewares for routes. A route can have specifics middlewares and not to use the globals.

type MiddlewaresMap

type MiddlewaresMap map[string][]string

MiddlewaresMap represents the middlewares map.

type Request

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

Request represents the server request

func NewRequest

func NewRequest(r *http.Request) *Request

NewRequest creates an instance of Request

func (Request) Body

func (r Request) Body() Body

Body gets the request body

func (Request) Params

func (r Request) Params() RouteParams

Params gets the request params

func (Request) Target

func (r Request) Target() *http.Request

Target gests the pointer of *http.Request

func (Request) URL

func (r Request) URL() *url.URL

URL gets the request URL

type Response

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

Response represents the server response

func BadRequest

func BadRequest(err error) (Response, error)

BadRequest creates a BadRequest response

func NewResponse

func NewResponse(w http.ResponseWriter) Response

NewResponse creates an instance of Response

func (Response) Bytes

func (r Response) Bytes() []byte

Bytes gets the response bytes

func (Response) ContentType

func (r Response) ContentType() string

ContentType gets the response content type

func (Response) JSON

func (r Response) JSON(v interface{}) (Response, error)

JSON creates a JSON response from v

func (Response) NoContent

func (r Response) NoContent() (Response, error)

NoContent creates a NoContent response

func (Response) NotFound

func (r Response) NotFound() (Response, error)

NotFound creates a NotFound response

func (Response) Ok

func (r Response) Ok(bytes []byte) (Response, error)

Ok creates an ok response

func (Response) ServerError

func (r Response) ServerError(err error) (Response, error)

ServerError creates an InternalServerError response

func (*Response) SetBytes

func (r *Response) SetBytes(b []byte)

SetBytes sets the response bytes

func (*Response) SetContentType

func (r *Response) SetContentType(s string)

SetContentType sets the response content type

func (*Response) SetStatus

func (r *Response) SetStatus(s int)

SetStatus sets the response status

func (Response) Status

func (r Response) Status() int

Status gets the response status

func (Response) Writer

func (r Response) Writer() http.ResponseWriter

Writer gets the http response writer

type Route

type Route struct {
	Token   string
	Method  HTTPMethod
	Path    string
	Handler HandlerFunc
}

Route represents a route

func NewRoute

func NewRoute(token string, method HTTPMethod, path string, handler Handler) *Route

NewRoute creates an instance of *Route using a struct

func NewRouteFunc

func NewRouteFunc(token string, method HTTPMethod, path string, handler HandlerFunc) *Route

NewRouteFunc creates an instance of *Route using a func

type RouteParams

type RouteParams struct {
	Params httprouter.Params
}

RouteParams represents the params for a route

func (RouteParams) AsInt

func (p RouteParams) AsInt(name string) int

AsInt gets the param as an Int

func (RouteParams) AsString

func (p RouteParams) AsString(name string) string

AsString gets the param as an String

type Router

type Router struct {
	*httprouter.Router
	// contains filtered or unexported fields
}

Router represents the router

func NewRouter

func NewRouter() *Router

NewRouter creates an instance of the *Router

func (*Router) AddRoutes

func (r *Router) AddRoutes(g string, newRoutes ...*Route)

AddRoutes adds one or more routes for the routes

func (*Router) DELETE

func (r *Router) DELETE(token string, path string, h Handler) *Route

DELETE creates a new route for HTTP DELETE

func (*Router) GET

func (r *Router) GET(token string, path string, h Handler) *Route

GET creates a new route for HTTP GET

func (*Router) HEAD

func (r *Router) HEAD(token string, path string, h Handler) *Route

HEAD creates a new route for HTTP HEAD

func (*Router) OPTIONS

func (r *Router) OPTIONS(token string, path string, h Handler) *Route

OPTIONS creates a new route for HTTP OPTIONS

func (*Router) PATCH

func (r *Router) PATCH(token string, path string, h Handler) *Route

PATCH creates a new route for HTTP PATCH

func (*Router) POST

func (r *Router) POST(token string, path string, h Handler) *Route

POST creates a new route for HTTP POST

func (*Router) PUT

func (r *Router) PUT(token string, path string, h Handler) *Route

PUT creates a new route for HTTP PUT

func (*Router) SetErrorHandler

func (r *Router) SetErrorHandler(e ErrorHandler)

SetErrorHandler defines the error handler for the router

func (*Router) SetMiddlewares

func (r *Router) SetMiddlewares(m []Middleware)

SetMiddlewares defines the middlewares for the router

func (*Router) SetMiddlewaresMap

func (r *Router) SetMiddlewaresMap(m MiddlewaresMap)

SetMiddlewaresMap defines the middlewares map fot the router

func (*Router) Start

func (r *Router) Start() *Router

Start configures all necessary steps for each route. The router starts the middlewares chain with the context.ClearHandler. It is responsible to clear all data in the request context. After, it configures specific middlewares for a route or adds all. So, the router adds the error handle as the last handler in the chain.

type Routes

type Routes map[string][]*Route

Routes represents the map de routes

type UnMarshalError

type UnMarshalError struct {
	Msg string
}

UnMarshalError represents an unmarshal error

func NewUnMarshalError

func NewUnMarshalError(msg string) UnMarshalError

NewUnMarshalError an instance of UnMarshalError

func (UnMarshalError) Error

func (v UnMarshalError) Error() string

Error gets the error message

Jump to

Keyboard shortcuts

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