Documentation
¶
Index ¶
- func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
- func EncodeJSONResponse[UseCaseResult any](_ context.Context, w http.ResponseWriter, response UseCaseResult) error
- type CreateRequestFunc
- type DecodeRequestFunc
- type DecodeResponseFunc
- type EncodeRequestFunc
- type EncodeResponseFunc
- type ErrorEncoder
- type Headerer
- type SimpleRoute
- type StatusCoder
- type UseCase
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorEncoder ¶
func DefaultErrorEncoder(_ context.Context, err error, w http.ResponseWriter)
DefaultErrorEncoder writes the error to the ResponseWriter, by default a content type of text/plain, a body of the plain text of the error, and a status code of 500. If the error implements Headerer, the provided headers will be applied to the response. If the error implements json.Marshaler, and the marshaling succeeds, a content type of application/json and the JSON encoded form of the error will be used. If the error implements StatusCoder, the provided StatusCode will be used instead of 500.
func EncodeJSONResponse ¶
func EncodeJSONResponse[UseCaseResult any](_ context.Context, w http.ResponseWriter, response UseCaseResult) error
EncodeJSONResponse is a EncodeResponseFunc that serializes the response as a JSON object to the ResponseWriter. Many JSON-over-HTTP services can use it as a sensible default. If the response implements Headerer, the provided headers will be applied to the response. If the response implements StatusCoder, the provided StatusCode will be used instead of 200.
Types ¶
type CreateRequestFunc ¶
CreateRequestFunc creates an outgoing HTTP request based on the passed request object. It's designed to be used in HTTP clients, for client-side endpoints. It's a more powerful version of EncodeRequestFunc, and can be used if more fine-grained control of the HTTP request is required.
type DecodeRequestFunc ¶
type DecodeRequestFunc[UseCaseCommand any] func(context.Context, *http.Request) (cmd *UseCaseCommand, err error)
DecodeRequestFunc extracts a user-domain request object from an HTTP request object. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward DecodeRequestFunc could be something that JSON decodes from the request body to the concrete request type.
type DecodeResponseFunc ¶
DecodeResponseFunc extracts a user-domain response object from an HTTP response object. It's designed to be used in HTTP clients, for client-side endpoints. One straightforward DecodeResponseFunc could be something that JSON decodes from the response body to the concrete response type.
type EncodeRequestFunc ¶
EncodeRequestFunc encodes the passed request object into the HTTP request object. It's designed to be used in HTTP clients, for client-side endpoints. One straightforward EncodeRequestFunc could be something that JSON encodes the object directly to the request body.
type EncodeResponseFunc ¶
type EncodeResponseFunc[UseCaseResult any] func(context.Context, http.ResponseWriter, UseCaseResult) error
EncodeResponseFunc encodes the passed response object to the HTTP response writer. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward EncodeResponseFunc could be something that JSON encodes the object directly to the response body.
type ErrorEncoder ¶
type ErrorEncoder func(ctx context.Context, err error, w http.ResponseWriter)
ErrorEncoder is responsible for encoding an error to the ResponseWriter. Users are encouraged to use custom ErrorEncoders to encode HTTP errors to their clients, and will likely want to pass and check for their own error types. See the example shipping/handling service.
type Headerer ¶
Headerer is checked by DefaultErrorEncoder. If an error value implements Headerer, the provided headers will be applied to the response writer, after the Content-Type is set.
type SimpleRoute ¶
type SimpleRoute[UseCaseCommand, UseCaseResult any] struct { // contains filtered or unexported fields }
SimpleRoute wraps an endpoint and implements http.HttpHandler.
func NewServer ¶
func NewServer[UseCaseCommand, UseCaseResult any]( e UseCase[UseCaseCommand, UseCaseResult], dec DecodeRequestFunc[UseCaseCommand], enc EncodeResponseFunc[UseCaseResult], ee ErrorEncoder, ) *SimpleRoute[UseCaseCommand, UseCaseResult]
NewServer constructs a new server, which implements http.HttpHandler and wraps the provided endpoint.
func NewServerWithDefaults ¶
func NewServerWithDefaults[UseCaseCommand, UseCaseResult any]( e UseCase[UseCaseCommand, UseCaseResult], dec DecodeRequestFunc[UseCaseCommand], ) *SimpleRoute[UseCaseCommand, UseCaseResult]
NewServerWithDefaults constructs a new server, which implements http.HttpHandler and wraps the provided endpoint.
func (*SimpleRoute[UseCaseCommand, UseCaseResult]) ServeHTTP ¶
func (sr *SimpleRoute[UseCaseCommand, UseCaseResult]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.HttpHandler.
type StatusCoder ¶
type StatusCoder interface {
StatusCode() int
}
StatusCoder is checked by DefaultErrorEncoder. If an error value implements StatusCoder, the StatusCode will be used when encoding the error. By default, StatusInternalServerError (500) is used.