middleware

package
v0.24.2 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2021 License: MIT Imports: 21 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCacheHeader added in v0.24.1

func AddCacheHeader(h http.Header) http.Header

AddCacheHeader adds a response header to indicate the resopnse is cached.

func CacheKey added in v0.24.1

func CacheKey(prefix string, r *http.Request) string

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

func ContextFromRequest(r *http.Request) context.Context

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.

func Etag

func Etag(next http.Handler) http.Handler

Etag middleware sets the ETag header.

func Recover

func Recover(next http.Handler) http.Handler

Recover middleware recovers from panics, and logs the error.

func SetHeader added in v0.23.0

func SetHeader(key, value string) func(next http.Handler) http.Handler

SetHeader adds header key/values to the response.

Types

type AppHeadersOptions added in v0.23.0

type AppHeadersOptions struct {
	GitCommit string
	Name      string
	Region    string
	Version   string
}

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 CachedResponse struct {
	http.Header

	// Body is the HTTP response.
	Body []byte

	// StatusCode is the HTTP response status code.
	StatusCode int

	// Expiration is the time when the cached data should expire.
	Expiration int64
}

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.

func (*EtagWriter) Write added in v0.1.1

func (e *EtagWriter) Write(p []byte) (int, error)

Write overrides the wrapped http.ResponseWriter's Write method, to provide etag-specific headers.

type LoggerOptions added in v0.23.0

type LoggerOptions struct {
	GitCommit string
	Name      string
	Version   string
}

LoggerOptions holds all logger middleware options.

type Middleware

type Middleware = func(http.Handler) http.Handler

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 Secure

func Secure(opts secure.Options) Middleware

Secure creates the security middleware

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.

Jump to

Keyboard shortcuts

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