Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrWrappedNil = errors.New("cannot wrap a nil request") ErrNoHandlers = errors.New("no handlers") )
Listing of errors that can be returned by the midl library specifically.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface { http.Handler // EmptyHandler registers a handler for empty response // bodies. // // Defaults to an empty byte array EmptyHandler(EmptyHandler) Adapter // Content-Type sets the default content type header. ContentType(string) Adapter // ErrorSerializer registers a handler for serializing // errors. ErrorSerializer(ErrorSerializer) Adapter // Serializer registers the default body serializer. Serializer(Serializer) Adapter // AddHandlers appends handlers to the list of Middleware // handlers. AddHandlers(...Middleware) Adapter // SetHandlers set and/or overwrite the current list of // Middleware handlers. SetHandlers(...Middleware) Adapter }
Adapter for conversion between Middleware and Golang http.Handlers.
handler := JSONAdapter(NewInputValidator(), ..., NewResponder()) http.Handle("/", handler) log.Fatal(http.ListenAndServe(":8080", nil))
func EmptyAdapter ¶
func EmptyAdapter() Adapter
EmptyAdapter creates an Adapter with no default settings or serializers
func JSONAdapter ¶
func JSONAdapter(handlers ...Middleware) Adapter
JSONAdapter creates a new Adapter defaulted for JSON responses
func NewAdapter ¶
func NewAdapter( content string, serial Serializer, error ErrorSerializer, next ...Middleware, ) Adapter
NewAdapter creates a new Adapter instance with the provided settings
func XMLAdapter ¶
func XMLAdapter(handlers ...Middleware) Adapter
XMLAdapter creates a new Adapter defaulted for XML responses
type BodyProcessor ¶
type BodyProcessor interface { // Process is used to handle some work which requires a // request body. This function is meant to be used with // Request.ProcessBody() which allows chaining various // operations, such as validation, deserialization, etc... Process([]byte) error }
BodyProcessor defines a service which can be run against a Request body.
type BodyProcessorFunc ¶
BodyProcessorFunc is a convenience wrapper which allows the use of a function as a BodyProcessor implementation.
func (BodyProcessorFunc) Process ¶
func (b BodyProcessorFunc) Process(in []byte) error
Process is a simple passthrough for the wrapped function.
type EmptyHandler ¶
type EmptyHandler interface { // Handle will be called in the event that a response body // is nil. Handle(Request, Response) []byte }
An EmptyHandler provides a chance to set default response output in the event of an empty body from middleware.
func DefaultEmptyHandler ¶
func DefaultEmptyHandler() EmptyHandler
DefaultEmptyHandler returns a no op empty handler
type EmptyHandlerFunc ¶
EmptyHandlerFunc provides a function wrapper for simple EmptyHandlers.
type ErrorSerializer ¶
type ErrorSerializer interface { // Serialize is used to serialize the given error into an // array of bytes. The HTTP request and response are // provided for context and to allow overriding/setting a // response status code or headers. Serialize(error, Request, Response) []byte }
ErrorSerializer defines a service which can be used to serialize a given error into a byte array.
func DefaultJSONErrorSerializer ¶
func DefaultJSONErrorSerializer() ErrorSerializer
DefaultJSONErrorSerializer returns an ErrorSerializer implementation which converts errors into a JSON byte array.
The given error will be printed in a JSON string matching this pattern:
`{"error":"%s"}`
The input error message is run through strconv.Quote for escaping special characters.
func DefaultXMLErrorSerializer ¶
func DefaultXMLErrorSerializer() ErrorSerializer
DefaultXMLErrorSerializer returns an ErrorSerializer implementation which converts errors into an XML byte array.
The given error will be printed in an XML string matching this pattern:
`<error>%s</error>`
type ErrorSerializerFunc ¶
ErrorSerializerFunc defines a convenience wrapper for using a function as an ErrorSerializer implementation.
type Middleware ¶
type Middleware interface { // Handle handles an HTTP request. // // If Handle returns a non-nil value, this signals to the // midl.Adapter that this request has been processed. Handle(Request) Response }
Middleware defines a service that can be called by a midl.Adapter with a midl.Response to get a midl.Response.
type MiddlewareFunc ¶
MiddlewareFunc is a convenience wrapper to allow the use of an arbitrary function as an instance of Middleware.
handler := MiddlewareFunc(func(Request) Response { return NewResponse().SetCode(http.StatusNoContent) })
func (MiddlewareFunc) Handle ¶
func (e MiddlewareFunc) Handle(r Request) Response
Handle will call the wrapped function.
type Request ¶
type Request interface { // Header gets the first header stored at the given key. Header(key string) (value string, ok bool) // Headers gets all headers stored at the given key. Headers(key string) (values []string, ok bool) // Body retrieves the request body bytes. // // In the event of a read error, returns nil; the error // will be available by calling the Error method. // // Multiple calls will only read the http.Request.Body // reader once. Body() []byte // Host retrieves the host string from the request. Host() string // Parameter get the query parameter stored at a given // key. Parameter(key string) (value string, ok bool) // Parameters gets all query parameters stored at a given // key. Parameters(key string) (values []string, ok bool) // RawRequest retrieves the raw Go standard library // request. // // Warning: Modifying the raw http.Request may impact the // output of other Request methods. RawRequest() *http.Request // Error retrieves the last error (if any) encountered by // method calls to Request. // If no error is present, returns nil. Error() error // ProcessBody runs the given BodyProcessor against the // body bytes of this request. // // Errors emitted by the body processor will be // retrievable from the Error method. // // Calls to this method will do nothing if an error has // been previously encountered. ProcessBody(BodyProcessor) Request // AdditionalContext returns a map for use in assigning // additional arbitrary context data to a request. AdditionalContext() map[interface{}]interface{} }
Request defines a wrapper/accessor for the default Golang http.Request struct.
type Response ¶
type Response interface { // Body returns the body (if any) stored on the HTTP // response. Body() interface{} // Code returns the HTTP status code from the HTTP // response. Code() int // Error returns the error (if any) stored on the HTTP // response. Error() error // Header retrieves a header stored header value by key. Header(key string) string // Headers retrieves a slice of stored headers values by // key. Headers(key string) []string // SetBody stores the given value as the body value for // this response. SetBody(any interface{}) Response // SetCode stores the given value as the HTTP status code // for this response. SetCode(code int) Response // SetError stores the given value as the error value for // this response. SetError(error) Response // AddHeader creates or appends to the header values for // this response. AddHeader(key, value string) Response // AddedHeaders creates or appends a list of headers to // this response. AddHeaders(key string, value []string) Response // SetHeader creates or overwrites a header on this // response. SetHeader(key, value string) Response // SetHeaders creates or overwrites a list of headers on // this response. SetHeaders(key string, values []string) Response // RawHeaders grants access to the internal http.Header // map. RawHeaders() http.Header // Callback allows providing a request completion callback // function that will be called asynchronously on request // completion. Callback(func()) Response // Callbacks returns the list of set callbacks on the // Response object. Callbacks() []func() }
Response defines a builder that can be used to build an HTTP response.
func MakeErrorResponse ¶
MakeErrorResponse creates a Response instance with the given error and status code.
func MakeResponse ¶
MakeResponse creates a Response instance with the given body and status code values.
func NewResponse ¶
func NewResponse() Response
NewResponse creates a new Response instance. Default status code is 200 (OK).
type Serializer ¶
type Serializer interface { // Serialize is used to serialize the response body passed // as its input parameter into a byte array. If the // serialization fails the returned error will be passed // to the Adapter's ErrorSerializer. Serialize(interface{}) ([]byte, error) }
Serializer defines a service which can be used to serialize a response body into an array of bytes.
type SerializerFunc ¶
SerializerFunc defines a convenience wrapper for using a function as a Serializer implementation.
func (SerializerFunc) Serialize ¶
func (s SerializerFunc) Serialize(in interface{}) ([]byte, error)
Serialize is a simple passthrough to the wrapped function.