Documentation ¶
Index ¶
- Variables
- func Filter(criteria ...filtering.FilterDecoder) func(*listingConfig)
- func GetListing(ctx context.Context) (*listing.Listing, error)
- func InternalErrLoggerOutput(w io.Writer) func(*internalErr)
- func InternalErrMsg(err error) func(*internalErr)
- func InternalError(opts ...func(*internalErr)) func(http.Handler) http.Handler
- func Limit(limit int) func(*listingConfig)
- func Listing(opts ...func(*listingConfig)) func(http.Handler) http.Handler
- func Logger(opts ...LoggerOpt) func(http.Handler) http.Handler
- func MaxAllowedLimit(maxAllowed int) func(*listingConfig)
- func Recovery(opts ...func(*recoveryCfg)) func(http.Handler) http.Handler
- func RecoveryLoggerOutput(w io.Writer) func(*recoveryCfg)
- func Sort(criteria ...sorting.Sort) func(*listingConfig)
- type LoggerOpt
- func AttachLogger(log zerolog.Logger) LoggerOpt
- func DisableLogDuration() LoggerOpt
- func DisableLogMethod() LoggerOpt
- func DisableLogRequestID() LoggerOpt
- func DisableLogSize() LoggerOpt
- func DisableLogStatus() LoggerOpt
- func DisableLogURL() LoggerOpt
- func EnableLogReferer() LoggerOpt
- func EnableLogReqIP() LoggerOpt
- func EnableLogUserAgent() LoggerOpt
Constants ¶
This section is empty.
Variables ¶
var (
// ListingCtxKey is the context.Context key to store the Listing for a request.
ListingCtxKey = &contextKey{"Listing"}
)
Functions ¶
func Filter ¶
func Filter(criteria ...filtering.FilterDecoder) func(*listingConfig)
Filter set criteria to filter
func GetListing ¶
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 InternalErrLoggerOutput ¶
InternalErrLoggerOutput set the output for the logger
func InternalErrMsg ¶
func InternalErrMsg(err error) func(*internalErr)
InternalErrMsg set default error message to be sent
func InternalError ¶
InternalError intercept responses to verify their status and handle the error. It gets the response code and if it's >= 500 handles the error with a default error message without disclosure internal information. The real error keeps logged.
func Listing ¶
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 Logger ¶
Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return.
Alternatively, look at https://github.com/rs/zerolog#integration-with-nethttp.
func MaxAllowedLimit ¶
func MaxAllowedLimit(maxAllowed int) func(*listingConfig)
MaxAllowedLimit set the max allowed limit default.
func Recovery ¶
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 ¶
RecoveryLoggerOutput set the output for the logger
Types ¶
type LoggerOpt ¶
type LoggerOpt func(*loggerCfg)
func AttachLogger ¶
AttachLogger chain the logger with the middleware.
func DisableLogDuration ¶
func DisableLogDuration() LoggerOpt
DisableLogStatus hide the request duration.
func DisableLogRequestID ¶
func DisableLogRequestID() LoggerOpt
DisableLogStatus hide the request id.
func EnableLogReferer ¶
func EnableLogReferer() LoggerOpt
EnableLogReferer show referer of the request.
func EnableLogUserAgent ¶
func EnableLogUserAgent() LoggerOpt
EnableLogUserAgent show the user agent of the request.