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
- Variables
- func GetUUID(ctx context.Context) (p uuid.UUID, ok bool)
- func Logger(logger *slog.Logger) func(next http.Handler) http.Handler
- func MustGetUUID(ctx context.Context) uuid.UUID
- func Paginate(defaultLimit int, maxLimit int) func(next http.Handler) http.Handler
- func Recoverer(logger *slog.Logger, printTrace bool) func(next http.Handler) http.Handler
- func RequireContentType(types ...string) func(next http.Handler) http.Handler
- func SetPagination(ctx context.Context, p Pagination) context.Context
- func SetUUID(ctx context.Context, u uuid.UUID) context.Context
- func UUID(param string) func(next http.Handler) http.Handler
- type Pagination
Constants ¶
const ( // PaginationLimitKey is the query parameter specifying the pagination limit. PaginationLimitKey = "limit" // PaginationOffsetKey is the query parameter specifying the pagination offset. PaginationOffsetKey = "offset" )
Variables ¶
var ContentTypeJSON = RequireContentType("application/json")
ContentTypeJSON is an instance of the RequireContentType middleware for the common application of JSON requests.
Functions ¶
func GetUUID ¶
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 MustGetUUID ¶
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 ¶
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 ¶
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 ¶
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.
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.