Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Encoder ¶
type Encoder interface { NewWriter(io.Writer) io.WriteCloser Encoding() string }
Encoder is an interface that wraps the methods returning the information necessary for the compress middleware to work.
`NewWriter` returns any `io.WriteCloser`, allowing the middleware to support any compression algorithm.
`Encoding` returns the name of the compression algorithm. Using the returned value, the middleware:
- detects the client's preferred encoding with the `Accept-Encoding` request header
- replaces the response writer with the writer returned by `NewWriter`
- sets the `Content-Encoding` response header
type Gzip ¶
type Gzip struct {
Level int
}
Gzip encoder for the gzip format using Go's standard `compress/gzip` package.
Takes a compression level as parameter. Accepted values are defined by constants in the standard `compress/gzip` package.
type Middleware ¶
type Middleware struct { goyave.Component Encoders []Encoder }
Middleware compresses HTTP responses.
This middleware supports multiple algorithms thanks to the `Encoders` slice. The encoder will be chosen depending on the request's `Accept-Encoding` header, and the value returned by the `Encoder`'s `Encoding()` method. Quality values in the headers are taken into account.
If the header's value is `*`, the first element of the slice is used. If none of the accepted encodings are available in the `Encoders` slice, then the response will not be compressed and the middleware immediately passes.
If the middleware successfully replaces the response writer, the `Accept-Encoding` header is removed from the request to avoid potential clashes with potential other encoding middleware.
If not set at the first call of `Write()`, the middleware will automatically detect and set the `Content-Type` header using `http.DetectContentType()`.
The middleware ignores hijacked responses or requests containing the `Upgrade` header.
**Example:**
compressMiddleware := &compress.Middleware{ Encoders: []compress.Encoder{ &compress.Gzip{Level: gzip.BestCompression}, }, }
func (*Middleware) Handle ¶
func (m *Middleware) Handle(next goyave.Handler) goyave.Handler
Handle implementation of `goyave.Middleware`.