capture

package
v2.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package capture is a middleware that captures requests/responses size, and status.

For another middleware to get those attributes of a request/response, this middleware should be added before in the middleware chain.

chain := alice.New().
     Append(capture.Wrap).
     Append(myOtherMiddleware).
     then(...)

As this middleware stores those data in the request's context, the data can be retrieved at anytime after the ServerHTTP.

func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request, next http.Handler) {
	capt, err := capture.FromContext(req.Context())
	if err != nil {
	...
	}

	fmt.Println(capt.Status())
	fmt.Println(capt.ResponseSize())
	fmt.Println(capt.RequestSize())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap added in v2.9.2

func Wrap(next http.Handler) (http.Handler, error)

Wrap returns a new handler that inserts a Capture into the given handler for each incoming request. It satisfies the alice.Constructor type.

Types

type Capture

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

Capture is the object populated by the capture middleware, holding probes that allow to gather information about the request and response.

func FromContext

func FromContext(ctx context.Context) (Capture, error)

FromContext returns the Capture value found in ctx, or an empty Capture otherwise.

func (*Capture) NeedsReset added in v2.9.2

func (c *Capture) NeedsReset(rw http.ResponseWriter) bool

NeedsReset returns whether the given http.ResponseWriter is the capture's probe.

func (*Capture) RequestSize

func (c *Capture) RequestSize() int64

RequestSize returns the size of the request's body if it applies, zero otherwise.

func (*Capture) Reset added in v2.9.2

func (c *Capture) Reset(next http.Handler) http.Handler

Reset returns a new handler that renews the Capture's probes, and inserts them when deferring to next.

func (*Capture) ResponseSize

func (c *Capture) ResponseSize() int64

func (*Capture) StatusCode

func (c *Capture) StatusCode() int

Jump to

Keyboard shortcuts

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