Documentation ¶
Index ¶
- Variables
- func CopyWriterHook(w io.Writer) ...
- func Filter(criteria ...filtering.FilterDecoder) func(*listingConfig)
- func GetListing(ctx context.Context) (*listing.Listing, error)
- func HijackWriteHook(w io.Writer) ...
- 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)
- func WriteHeaderHook(...) func(*wrapWriterOpts)
- func WriteHook(...) func(*wrapWriterOpts)
- 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
- type WriterMetricsCollector
Constants ¶
This section is empty.
Variables ¶
var ( // CollectHeaderHook capture the response code into a WriterMetricsCollector and forward the execution to the main // WriteHeader method. It's the default hook for WriteHeader when WrapWriter is used. CollectHeaderHook = func(collector *WriterMetricsCollector) func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc { return func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc { return func(code int) { next(code) collector.locker.Lock() defer collector.locker.Unlock() if !collector.wroteHeader { collector.Code = code collector.wroteHeader = true } } } } // HijackWriteHeaderHook capture the response code into a WriterMetricsCollector. Warning it'll not forward to the // main WriteHeader method execution. HijackWriteHeaderHook = func(collector *WriterMetricsCollector) func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc { return func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc { return func(code int) { collector.locker.Lock() defer collector.locker.Unlock() if !collector.wroteHeader { collector.Code = code collector.wroteHeader = true } } } } // CollectBytesHook capture the amount of bytes into a WriterMetricsCollector and forward the execution to the main // Write method. It's the default hook for Write when WrapWriter is used. CollectBytesHook = func(collector *WriterMetricsCollector) func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc { return func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc { return func(p []byte) (int, error) { n, err := next(p) collector.locker.Lock() defer collector.locker.Unlock() collector.Bytes += int64(n) collector.wroteHeader = true return n, err } } } )
var (
// ListingCtxKey is the context.Context key to store the Listing for a request.
ListingCtxKey = &contextKey{"Listing"}
)
Functions ¶
func CopyWriterHook ¶
func CopyWriterHook(w io.Writer) func(collector *WriterMetricsCollector) func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc
CopyWriterHook makes a copy of the bytes into a io.Writer and forward the execution to the main Write method. It'll save the amount of bytes into a WriterMetricsCollector.
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 HijackWriteHook ¶
func HijackWriteHook(w io.Writer) func(collector *WriterMetricsCollector) func(next httpsnoop.WriteFunc) httpsnoop.WriteFunc
HijackWriteHook write the response bytes into a io.Writer. It'll save the amount of bytes into a WriterMetricsCollector. Warning it'll not forward to the main Write method execution.
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
func WriteHeaderHook ¶
func WriteHeaderHook(hook func(*WriterMetricsCollector) func(next httpsnoop.WriteHeaderFunc) httpsnoop.WriteHeaderFunc) func(*wrapWriterOpts)
WriteHeaderHook define the method interceptor when WriteHeader is called.
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.
type WriterMetricsCollector ¶
type WriterMetricsCollector struct { // Code is the first http response code passed to the WriteHeader func of // the ResponseWriter. If no such call is made, a default code of 200 is // assumed instead. Code int // bytes is the number of bytes successfully written by the Write or // ReadFrom function of the ResponseWriter. ResponseWriters may also write // data to their underlying connection directly (e.g. headers), but those // are not tracked. Therefor the number of Written bytes will usually match // the size of the response body. Bytes int64 // contains filtered or unexported fields }
WriterMetricsCollector holds metrics captured from writer.
func WrapResponseWriter ¶
func WrapResponseWriter(w http.ResponseWriter, opts ...func(*wrapWriterOpts)) (*WriterMetricsCollector, http.ResponseWriter)
WrapResponseWriter defines a set of method interceptors for methods included in http.ResponseWriter as well as some others. You can think of them as middleware for the function calls they target. It response with a wrapped ResponseWriter and WriterMetricsCollector with some useful metrics.