Documentation ¶
Overview ¶
Package middleware implements the Middleware type.
Index ¶
- Constants
- Variables
- func DecodeBody[A any](c *Connection) data.Result[*A]
- func DecodeQuery[A any](c *Connection) data.Result[*A]
- func GetOrElse[A any](ma data.Result[A], onError func(error) A) A
- func ToHandlerFunc[A any](ma Middleware[A], onError func(error, *Connection)) http.HandlerFunc
- type Connection
- type Middleware
- func Ap[A, B any](fab Middleware[func(A) B], fa Middleware[A]) Middleware[B]
- func ApFirst[A, B any](fa Middleware[A], fb Middleware[B]) Middleware[A]
- func ApSecond[A, B any](fa Middleware[A], fb Middleware[B]) Middleware[B]
- func Chain[A, B any](ma Middleware[A], f func(A) Middleware[B]) Middleware[B]
- func ChainFirst[A, B any](ma Middleware[A], f func(A) Middleware[B]) Middleware[A]
- func ContentType[A any](contentType string) Middleware[A]
- func DecodeHeader(name string, rules ...validation.Rule) Middleware[string]
- func DecodeMethod[A any](f func(string) data.Result[A]) Middleware[A]
- func FilterOrElse[A any](ma Middleware[A], predicate data.Predicate[A], onFalse func(A) error) Middleware[A]
- func FromRequest[A any](f func(c *http.Request) data.Result[A]) Middleware[A]
- func FromResult[A any](ra data.Result[A]) Middleware[A]
- func HTML(html string) Middleware[any]
- func Header(name, value string) Middleware[any]
- func JSON[A any](d A) Middleware[any]
- func Map[A, B any](fa Middleware[A], f func(A) B) Middleware[B]
- func MapError[A any](fa Middleware[A], f func(error) error) Middleware[A]
- func ModifyResponse[A any](f func(w http.ResponseWriter)) Middleware[A]
- func OrElse[A any](ma Middleware[A], onError func(error) Middleware[A]) Middleware[A]
- func PlainText(text string) Middleware[any]
- func Redirect(url string, code int) Middleware[any]
- func Status(status int) Middleware[any]
- func Write(body []byte) Middleware[any]
Constants ¶
const ( MediaTypeApplicationXML = "application/xml" MediaTypeApplicationJSON = "application/json" MediaTypeFormURLEncoded = "application/x-www-form-urlencoded" MediaTypeImageGIF = "image/gif" MediaTypeImageJPEG = "image/jpeg" MediaTypeImagePNG = "image/png" MediaTypeApplicationOctetStream = "application/octet-stream" MediaTypeTextHTML = "text/html" MediaTypeTextXML = "text/xml" MediaTypeTextCSV = "text/csv" MediaTypeTextPlain = "text/plain" MediaTypeMultipartFormData = "multipart/form-data" )
MIME types.
Variables ¶
var ErrUnknownContentType = errors.New("unknown content-type")
ErrUnknownContentType is thrown by DecodeBody when a Content-Type is not one of "application/x-www-form-urlencoded" or "application/json".
Functions ¶
func DecodeBody ¶
func DecodeBody[A any](c *Connection) data.Result[*A]
DecodeBody middleware decodes (and optionally validates) a request payload into a value of type A.
func DecodeQuery ¶
func DecodeQuery[A any](c *Connection) data.Result[*A]
DecodeQuery middleware decodes (and optionally validates) a value of type A from the query string.
func GetOrElse ¶ added in v0.0.3
GetOrElse creates a middleware which can be used to recover from a failing middleware with a new value.
func ToHandlerFunc ¶
func ToHandlerFunc[A any]( ma Middleware[A], onError func(error, *Connection), ) http.HandlerFunc
ToHandlerFunc turns a middleware into a standard http handler function.
Types ¶
type Connection ¶
type Connection struct { R *http.Request W http.ResponseWriter // contains filtered or unexported fields }
A Connection represents the connection between an HTTP server and a user agent.
type Middleware ¶
type Middleware[A any] func(*Connection) data.Result[A]
A Middleware represents a computation which modifies a HTTP connection or reads from it, producing either a value of type A or an error for the next middleware in the pipeline.
func Ap ¶
func Ap[A, B any](fab Middleware[func(A) B], fa Middleware[A]) Middleware[B]
Ap creates a middleware by applying a function contained in the first middleware on the value contained in the second middleware.
func ApFirst ¶
func ApFirst[A, B any](fa Middleware[A], fb Middleware[B]) Middleware[A]
ApFirst creates a middleware by combining two effectful computations on a connection, keeping only the result of the first.
func ApSecond ¶
func ApSecond[A, B any](fa Middleware[A], fb Middleware[B]) Middleware[B]
ApSecond creates a middleware by combining two effectful computations on a connection, keeping only the result of the second.
func Chain ¶
func Chain[A, B any](ma Middleware[A], f func(A) Middleware[B]) Middleware[B]
Chain creates a middleware which combines two results in sequence, using the return value of one middleware to determine the next one.
func ChainFirst ¶
func ChainFirst[A, B any](ma Middleware[A], f func(A) Middleware[B]) Middleware[A]
ChainFirst composes two middlewares in sequence, using the return value of one to determine the next one, keeping only the result of the first one.
func ContentType ¶
func ContentType[A any](contentType string) Middleware[A]
ContentType creates a middleware which sets the Content-Type header on a response.
func DecodeHeader ¶
func DecodeHeader(name string, rules ...validation.Rule) Middleware[string]
DecodeHeader creates a middleware by validating a string value from a header.
func DecodeMethod ¶
func DecodeMethod[A any](f func(string) data.Result[A]) Middleware[A]
DecodeMethod creates a Middleware by applying a function on a request method.
func FilterOrElse ¶
func FilterOrElse[A any](ma Middleware[A], predicate data.Predicate[A], onFalse func(A) error) Middleware[A]
FilterOrElse creates a middleware which can be used to fail with an error unless a predicate holds on a succeeding result.
func FromRequest ¶
FromRequest creates a middleware for reading a request by applying a function on a connection request that either yields a value of type A, or fails with an error.
func FromResult ¶
func FromResult[A any](ra data.Result[A]) Middleware[A]
FromResult converts a function, which takes no parameters and returns a value of type A along with an error, into a Middleware.
func HTML ¶
func HTML(html string) Middleware[any]
HTML creates a middleware that sends a string as HTML response.
func Header ¶
func Header(name, value string) Middleware[any]
Header creates a middleware that sets a header on the response. Note that, changing a header after a call to Status has no effect.
func Map ¶
func Map[A, B any](fa Middleware[A], f func(A) B) Middleware[B]
Map creates a middleware by applying a function on a succeeding middleware.
func MapError ¶
func MapError[A any](fa Middleware[A], f func(error) error) Middleware[A]
MapError creates a middleware by applying a function on a failing middleware.
func ModifyResponse ¶
func ModifyResponse[A any](f func(w http.ResponseWriter)) Middleware[A]
ModifyResponse creates a middleware for writing a response.
func OrElse ¶
func OrElse[A any](ma Middleware[A], onError func(error) Middleware[A]) Middleware[A]
OrElse creates a middleware which can be used to recover from a failing middleware by switching to a new middleware.
func PlainText ¶
func PlainText(text string) Middleware[any]
PlainText creates a middleware that sends a plain text as response.
func Redirect ¶
func Redirect(url string, code int) Middleware[any]
Redirect creates a middleware for redirecting a request to the given URL with the given 3xx code.
func Status ¶
func Status(status int) Middleware[any]
Status creates a middleware that sets a response status code.
func Write ¶
func Write(body []byte) Middleware[any]
Write creates a middleware for sending the given byte array response without specifying the Content-Type.