Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DecodeFunc ¶
DecodeFunc is the function signature for decoding a request into an object.
type Decoder ¶
type Decoder struct {
// DecodeBody is the function that will be used to decode the
// request body into a target object.
DecodeBody DecodeFunc
// Header is the function used to get the string value of a header.
Header func(*http.Request, string) (string, error)
// Segment is the function used to get the string value of a path
// parameter.
Segment func(*http.Request, string) (string, error)
// Queries is the function used to get the string values of a query
// parameter.
Queries func(*http.Request, string) ([]string, error)
// Cookie is the function used to get the value of a cookie from a
// request.
Cookie func(*http.Request, string) (string, error)
}
Decoder is a struct that allows for the decoding of http requests into arbitrary objects.
func NewDecoder ¶
func NewDecoder() *Decoder
NewDecoder returns a new decoder with sensible defaults for the DecodeBody, Header and Query functions. By default, it uses a JSON decoder on the request body.
func (*Decoder) Decode ¶
Decode will (by default), given a struct definition:
type Request struct {
AuthString string `http:"header=Authorization"`
Limit int `http:"query=limit"`
Resource string `http:"segment=resource"`
UserCookie float64 `http:"cookie=user_cookie"`
Extra map[string]int `json:"extra"`
}
The Authorization header will be parsed into the field Token of the request struct.
The Limit field will come from the query string.
The Resource field will come from the resource value of the path (e.g: /api/pets/{resource}).
The Extra field will come from deserializing the request body from JSON encoding.
type HTTPError ¶
type HTTPError interface {
error
HTTPResponse
}
func NewNoopError ¶
func NewNoopError() HTTPError
NewNoopError returns an HTTPError that will completely bypass the deserialization logic. This can be used when the endpoint or middleware operates directly on the native http.ResponseWriter.
type HTTPResponse ¶
HTTPResponse is used by the StandardResponseWriter to construct the response body according to the StatusCode() and WriteBody() functions. If the StatusCode() function returns `0`, the StandardResponseWriter will assume that WriteHeader has already been called on the http.ResponseWriter object.
func NewJSONResponse ¶
func NewJSONResponse(code int, body any) HTTPResponse
type RequestReader ¶
RequestReader is the function signature for unmarshalling a *http.Request into an object.
func StandardRequestReader ¶
func StandardRequestReader() RequestReader
The StandardRequestReader decodes the request using the following:
- Cookies
- Query Params
- Request Headers
- Request Path Segment (e.g: /api/pets/{id})
- JSON Decoding of the http request body
type ResponseWriter ¶
ResponseWriter is the function signature for marshalling a structured response into a standard http.ResponseWriter
func StandardResponseWriter ¶
func StandardResponseWriter() ResponseWriter
StandardResponseWriter will try to cast the error and response objects to the HTTPResponse interface and use them to send the response to the client. By default, it will send a 200 OK and encode the response object as JSON. If the HTTPResponse has a `0` StatusCode, WriteHeader will not be called. If the error is not an HTTPResponse, a 500 status code will be returned with the body being exactly the error's string.
type Wrapper ¶
type Wrapper struct {
// contains filtered or unexported fields
}
Wrapper implements the http.Handler interface, wrapping the handlers that are passed in.
func New ¶
func New() Wrapper
New creates a new Wrapper object. This wrapper object will not interact in any way with the http request and response writer.
func NewStandardWrapper ¶
func NewStandardWrapper() Wrapper
NewStandardWrapper returns a new wrapper using the StandardRequestReader and the StandardResponseWriter.
func (Wrapper) Before ¶
Before adds a new function that will execute before the main handler. The chain of befores will end if a before returns a non-nil error value.
func (Wrapper) Finally ¶
Finally sets the last function that will execute during a request. This function gets invoked with the response object and the possible error returned from the main endpoint function.
func (Wrapper) WithRequestReader ¶
func (w Wrapper) WithRequestReader(cons RequestReader) Wrapper
WithRequestReader returns a new wrapper with the given RequestReader function.