middleware

package
v2.1.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2018 License: MIT Imports: 15 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAPIDefault = errors.New("looks like something went wrong")

ErrAPIDefault default error message response when something happens.

Functions

func APIError

func APIError(opts ...func(*apiErrCfg)) func(http.Handler) http.Handler

APIError intercept responses to verify their status and handle the error. It gets the response code and if it's >= 500 handlers the error with a default error message and without disclosure internal information. The real error keeps logged.

func APIErrorDefault500

func APIErrorDefault500(err error) func(*apiErrCfg)

APIErrorDefault500 set default error message to be sent

func APIErrorLoggerOutput

func APIErrorLoggerOutput(w io.Writer) func(*apiErrCfg)

APIErrorLoggerOutput set the output for the logger

func Filter

func Filter(criteria ...filtering.FilterDecoder) func(*listingConfig)

Filter set criteria to filter

func GetListing

func GetListing(ctx context.Context) (*listing.Listing, error)

GetListing will return the listing reference assigned to the context, or nil if there is any error or there isn't a Listing instance.

func Limit

func Limit(limit int) func(*listingConfig)

Limit set the paging limit default.

func Listing

func Listing(opts ...func(*listingConfig)) func(http.Handler) http.Handler

Listing is a middleware that parses the url from a request and stores a listing.Listing on the context, it can be accessed through middleware.GetListing.

Sample usage.. for the url: `/repositories/1?limit=10&offset=25`

 func routes() http.Handler {
   r := chi.NewRouter()
   r.Use(middleware.Listing())
   r.Get("/repositories/{id}", ListRepositories)
   return r
 }

 func ListRepositories(w http.ResponseWriter, r *http.Request) {
	  list, _ := middleware.GetListing(r.Context())

	  // do something with listing
}

func MaxAllowedLimit

func MaxAllowedLimit(maxAllowed int) func(*listingConfig)

MaxAllowedLimit set the max allowed limit default.

func Recovery

func Recovery(opts ...func(*recoveryCfg)) func(http.Handler) http.Handler

Recovery is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recovery prints a request ID if one is provided.

func RecoveryLoggerOutput

func RecoveryLoggerOutput(w io.Writer) func(*recoveryCfg)

RecoveryLoggerOutput set the output for the logger

func Sort

func Sort(criteria ...sorting.Sort) func(*listingConfig)

Sort set criteria to sort

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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