Documentation ¶
Index ¶
- Constants
- Variables
- func Bootstrap(h web.Handler) web.Handler
- func Compress(level int, types ...string) func(next web.Handler) web.Handler
- func DefaultLogFields(h web.Handler) web.Handler
- func LogRequest(h web.Handler) web.Handler
- func RealIP(h web.Handler) web.Handler
- func Recoverer(h web.Handler) web.Handler
- func RequestID(h web.Handler) web.Handler
- func VersionHeader(version string) func(web.Handler) web.Handler
- func WithField(name string, value interface{}) func(web.Handler) web.Handler
- func WithTimeout(timeout time.Duration) func(web.Handler) web.Handler
- type Compressor
- type EncoderFunc
Constants ¶
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 ¶
var WithDefaultTimeout = WithTimeout(15 * time.Second)
WithDefaultTimeout wraps WithTimeout with a timeout of 15 seconds.
Functions ¶
func Bootstrap ¶
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 ¶
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 ¶
DefaultLogFields attaches a set of default log fields to the logger that's passed through the request.
func LogRequest ¶
LogRequest adds logging about how long the request took to execute.
func RealIP ¶
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 RequestID ¶
RequestID determines whether a request ID should be created or gleaned from the request, then sets it on the context.
func WithField ¶
WithField attaches a log field with the provided name and value to the logger that's passed through the request.
func WithTimeout ¶
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) })