Documentation ¶
Overview ¶
Package middleware provides multiple middlewares, useful for any HTTP service. These middlewares are not specific to any application, and are made to be as reusable and idiomatic as possible. Compatibility is guaranteed for `net/http`, however these middlewares should be compatible with any third-party package which conforms to the standard library's APIs.
Index ¶
- func AddCacheHeader(h http.Header) http.Header
- func CacheKey(prefix string, r *http.Request) string
- func Compose(h http.Handler, m ...Middleware) http.Handler
- func ContextFromRequest(r *http.Request) context.Context
- func Etag(next http.Handler) http.Handler
- func Recover(next http.Handler) http.Handler
- func SetHeader(key, value string) func(next http.Handler) http.Handler
- type AppHeadersOptions
- type AuthOptions
- type CacheOptions
- type CacheWriter
- type CachedResponse
- type EtagWriter
- type LoggerOptions
- type Middleware
- func AppHeaders(opts AppHeadersOptions) Middleware
- func Auth(opts AuthOptions) Middleware
- func Cache(c cache.Cacher, opts CacheOptions) Middleware
- func Logger(opts LoggerOptions) Middleware
- func Secure(opts secure.Options) Middleware
- func SecureDefault() Middleware
- func Timeout(d time.Duration) Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCacheHeader ¶ added in v0.24.1
AddCacheHeader adds a response header to indicate the resopnse is cached.
func Compose ¶
func Compose(h http.Handler, m ...Middleware) http.Handler
Compose adds middleware handlers to a given handler. Middleware handlers are ordered from first to last.
func ContextFromRequest ¶ added in v0.24.2
ContextFromRequest returns a new context, which may or may not add a "no cache" flag. Contexts created from this can be used with cache.UseCache to determine if the cache can be used.
Types ¶
type AppHeadersOptions ¶ added in v0.23.0
AppHeaders holds app specific header values.
type AuthOptions ¶ added in v0.23.0
type AuthOptions struct { WhiteList []string APIKeyName string SecretKey string ErrorMessage string }
AuthOptions holds the necessary values for the authentication middleware
type CacheOptions ¶ added in v0.24.1
type CacheOptions struct { // AllowedMethods is the slice of allowed HTTP request methods // which can be cached. // Defaults are: GET, HEAD. AllowedMethods []string // AllowedStatuses is the slice of allowed HTTP response status // codes which can be cached. // Default is: 200. AllowedStatuses []int // KeyPrefix is the optional cache key prefix. KeyPrefix string // TTL is the time-to-live for the cached data. // Default is: 15 minutes. TTL time.Duration // UseStale, when true, allows stale cache data to be used in the // case of internal server errors. UseStale bool // StaleStatuses is the slice of status codes allowed to use stale // cache data as a fall-back. // Default is: 500. StaleStatuses []int // StaleTTL is the time-to-live for stale cached data, before // being automaticlly purged. StaleTTL time.Duration }
CacheOptions are the configurable options for the cache middleware. These determine how exactly responses are cached.
type CacheWriter ¶ added in v0.24.1
type CacheWriter struct { http.ResponseWriter // contains filtered or unexported fields }
CacheWriter is an enhanced http.ResponseWriter. Write operations are duplicated to a data buffer, which is used to cache the response.
func NewCacheWriter ¶ added in v0.24.1
func NewCacheWriter(w http.ResponseWriter, useStale bool, staleStatuses []int) *CacheWriter
NewCacheWriter creates a new CacheWriter.
func (*CacheWriter) ReadAll ¶ added in v0.24.1
func (cw *CacheWriter) ReadAll() ([]byte, error)
ReadAll reads all of the data in the cache buffer.
func (*CacheWriter) Write ¶ added in v0.24.1
func (cw *CacheWriter) Write(p []byte) (int, error)
Write writes data to both a ResponseWriter and a cache data buffer. If stale cache data may be used, only the buffer writes data; stale data is written later, using the underlying ResponseWriter directly.
func (*CacheWriter) WriteHeader ¶ added in v0.24.1
func (cw *CacheWriter) WriteHeader(statusCode int)
type CachedResponse ¶ added in v0.24.1
type EtagWriter ¶ added in v0.1.1
type EtagWriter struct { http.ResponseWriter // contains filtered or unexported fields }
EtagWriter provides a ResponseWriter which holds info needed for verifying etags.
func NewEtagWriter ¶ added in v0.23.0
func NewEtagWriter(w http.ResponseWriter) *EtagWriter
NewEtagWriter returns an EtagWriter with a wrapped ResponseWriter.
func (*EtagWriter) ClientEtag ¶ added in v0.23.0
func (e *EtagWriter) ClientEtag(t string) *EtagWriter
ClientEtag sets the requested ETag.
type LoggerOptions ¶ added in v0.23.0
LoggerOptions holds all logger middleware options.
type Middleware ¶
Middleware aliases the function type for all middleware.
func AppHeaders ¶
func AppHeaders(opts AppHeadersOptions) Middleware
AppHeaders adds more app-specific headers to the response.
func Auth ¶
func Auth(opts AuthOptions) Middleware
Auth handles authenticating requests. Authentication is driven by an API key query parameter.
func Cache ¶ added in v0.24.1
func Cache(c cache.Cacher, opts CacheOptions) Middleware
Cache middleware creator offers caching of responses. Unlike caching driven by cache-control headers, responses are cached by an external Cacher, so even first-time requesters can benefit from cached responses. Optionally, stale cache data can be returned in cases of internal server errors, to protect against downtime.
func Logger ¶
func Logger(opts LoggerOptions) Middleware
Logger outputs general information about requests.
func SecureDefault ¶ added in v0.23.0
func SecureDefault() Middleware
SecureDefault is similar to Secure, but provides sane security defaults, without requiring options to be provided.
func Timeout ¶ added in v0.24.2
func Timeout(d time.Duration) Middleware
Timeout adds a timeout to each request's context. If the duration is zero, Timeout defaults to fifteen seconds.