middleware

package
v0.0.0-...-d6b3ade Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RequestIDKey is the context key for a given request ID.
	RequestIDKey ctxKey = iota

	// StartTimeKey is a context key for storing when the request starts.
	StartTimeKey
)

Variables

View Source
var WithDefaultTimeout = WithTimeout(15 * time.Second)

WithDefaultTimeout wraps WithTimeout with a timeout of 15 seconds.

Functions

func Bootstrap

func Bootstrap(h web.Handler) web.Handler

Bootstrap creates an initial context and log entry, and is therefore the first middleware that should be applied. You may choose to use your own app-specific Bootstrap implementation to attach a custom logger or context.

func Compress

func Compress(level int, types ...string) func(next web.Handler) web.Handler

Compress is a middleware that compresses response body of a given content types to a data format based on Accept-Encoding request header. It uses a given compression level.

NOTE: make sure to set the Content-Type header on your response otherwise this middleware will not compress the response body. For ex, in your handler you should set w.Header().Set("Content-Type", http.DetectContentType(yourBody)) or set it manually.

Passing a compression level of 5 is sensible value

func DefaultLogFields

func DefaultLogFields(h web.Handler) web.Handler

DefaultLogFields attaches a set of default log fields to the logger that's passed through the request.

func LogRequest

func LogRequest(h web.Handler) web.Handler

LogRequest adds logging about how long the request took to execute.

func RealIP

func RealIP(h web.Handler) web.Handler

RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Real-IP header or the X-Forwarded-For header (in that order).

This middleware should be inserted fairly early in the middleware stack to ensure that subsequent layers (e.g., request loggers) which examine the RemoteAddr will see the intended value.

You should only use this middleware if you can trust the headers passed to you (in particular, the two headers this middleware uses), for example because you have placed a reverse proxy like HAProxy or nginx in front of chi. If your reverse proxies are configured to pass along arbitrary header values from the client, or if you use this middleware without a reverse proxy, malicious clients will be able to make you very sad (or, depending on how you're using RemoteAddr, vulnerable to an attack of some sort).

func Recoverer

func Recoverer(h web.Handler) web.Handler

Recoverer catches any panics that the wrapped Handler might cause.

func RequestID

func RequestID(h web.Handler) web.Handler

RequestID determines whether a request ID should be created or gleaned from the request, then sets it on the context.

func VersionHeader

func VersionHeader(version string) func(web.Handler) web.Handler

func WithField

func WithField(name string, value interface{}) func(web.Handler) web.Handler

WithField attaches a log field with the provided name and value to the logger that's passed through the request.

func WithTimeout

func WithTimeout(timeout time.Duration) func(web.Handler) web.Handler

WithTimeout ensures that the provided handler completes within a set amount of time. If it doesn't, it'll write a 503 to the client. It *does not* prevent the handler from completing, but silently swallows any additional output.

Types

type Compressor

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

Compressor represents a set of encoding configurations.

func NewCompressor

func NewCompressor(level int, types ...string) *Compressor

NewCompressor creates a new Compressor that will handle encoding responses.

The level should be one of the ones defined in the flate package. The types are the content types that are allowed to be compressed.

func (*Compressor) Handler

func (c *Compressor) Handler(next web.Handler) web.Handler

Handler returns a new middleware that will compress the response based on the current Compressor.

func (*Compressor) SetEncoder

func (c *Compressor) SetEncoder(encoding string, fn EncoderFunc)

SetEncoder can be used to set the implementation of a compression algorithm.

The encoding should be a standardised identifier. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding

For example, add the Brotli algortithm:

import brotli_enc "gopkg.in/kothar/brotli-go.v0/enc"

compressor := middleware.NewCompressor(5, "text/html")
compressor.SetEncoder("br", func(w http.ResponseWriter, level int) io.Writer {
  params := brotli_enc.NewBrotliParams()
  params.SetQuality(level)
  return brotli_enc.NewBrotliWriter(params, w)
})

type EncoderFunc

type EncoderFunc func(w io.Writer, level int) io.Writer

An EncoderFunc is a function that wraps the provided io.Writer with a streaming compression algorithm and returns it.

In case of failure, the function should return nil.

Directories

Path Synopsis
Package cors is ported from github.com/go-chi/cors to implement CORS middleware for web.Handler.
Package cors is ported from github.com/go-chi/cors to implement CORS middleware for web.Handler.

Jump to

Keyboard shortcuts

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