middleware

package
v0.0.0-...-c05fae0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package middleware provides some of the middlewares used in the Karman API. In part these middlewares are re-implementations of known middlewares from the chi router, mostly with the goal to provide unified error reporting via the apierror package.

Index

Constants

View Source
const (
	// PaginationLimitKey is the query parameter specifying the pagination limit.
	PaginationLimitKey = "limit"
	// PaginationOffsetKey is the query parameter specifying the pagination offset.
	PaginationOffsetKey = "offset"
)

Variables

View Source
var ContentTypeJSON = RequireContentType("application/json")

ContentTypeJSON is an instance of the RequireContentType middleware for the common application of JSON requests.

Functions

func GetUUID

func GetUUID(ctx context.Context) (p uuid.UUID, ok bool)

GetUUID returns the UUID value from the request, if any. If the request does not contain a UUID value, the second return value will be false.

func Logger

func Logger(logger *slog.Logger) func(next http.Handler) http.Handler

Logger returns a middleware that logs every request after it is done.

func MustGetUUID

func MustGetUUID(ctx context.Context) uuid.UUID

MustGetUUID returns the UUID value from the request. In contrast to GetUUID this function panics if the request does not contain a UUID value.

func Paginate

func Paginate(defaultLimit int, maxLimit int) func(next http.Handler) http.Handler

Paginate is a middleware that processes paginated queries and provides sanitized parameters via the request context. This middleware implements limit-offset pagination through two query parameters limit and offset. The limit defaults to the specified defaultLimit and is bounded by maxLimit. The offset defaults to 0 and is unbounded. Both limit and offset will be set to 0 if they are negative.

This middleware may return a 400 response of the pagination query cannot be parsed. If the pagination query could be parsed the request context will contain the sanitized data. You can get the data via GetPagination and MustGetPagination.

func Recoverer

func Recoverer(logger *slog.Logger, printTrace bool) func(next http.Handler) http.Handler

Recoverer returns a middleware that recovers from panics, logs the panic (and a backtrace), and returns an HTTP 500 (Internal Server Error) status if possible.

If printStack is true, the middleware will print the stack trace of a panic to stderr. This can be useful in debugging.

func RequireContentType

func RequireContentType(types ...string) func(next http.Handler) http.Handler

RequireContentType is a middleware that enforces the use of the Content-Type header. To create this middleware you need to pass the allowed media types. You can pass parameters for media types, but those are ignored in this middleware.

In addition to static media types like text/plain you can also use wildcard media types like text/* or the special value */* to accept all media types. Wildcard types like text/* are matched as a prefix of the request header (but parameters are ignored during matching). The special value */* allows all media type but still requires a Content-Type header to be present. In this case the syntax of the Content-Type header is not validated.

Using this middleware forces the use of the Content-Type header. A request without this header will result in an error, even if the request body is empty.

func SetPagination

func SetPagination(ctx context.Context, p Pagination) context.Context

SetPagination sets p in ctx. The value is retrievable later via GetPagination and MustGetPagination.

func SetUUID

func SetUUID(ctx context.Context, u uuid.UUID) context.Context

SetUUID sets u in ctx. The value is retrievable later via GetUUID and MustGetUUID.

func UUID

func UUID(param string) func(next http.Handler) http.Handler

UUID is a simple middleware that fetches a UUID value from a request parameter named param. The UUID will be stored in the request context and can be fetched via GetUUID.

Types

type Pagination

type Pagination struct {
	// The limit originally found in the request.
	// May exceed the maximum limit and may be negative.
	RequestLimit int

	// The sanitized pagination limit.
	// Non-negative and bounded by the maximum of the pagination middleware.
	Limit int

	// The sanitized pagination offset.
	// The Offset is non-negative but otherwise unbounded.
	Offset int64
}

Pagination is a simple data object holding information about the current pagination query.

func GetPagination

func GetPagination(ctx context.Context) (p Pagination, ok bool)

GetPagination returns the pagination value from the request, if any. If the request does not contain a pagination value, the second return value will be false.

func MustGetPagination

func MustGetPagination(ctx context.Context) Pagination

MustGetPagination returns the pagination value from the request. In contrast to GetPagination this function panics if the request does not contain a pagination value.

Jump to

Keyboard shortcuts

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