Documentation ¶
Overview ¶
Package middleware provides you multiple generic middlewares to use to enhance your http handlers.
It also provides you a core to use them all at once and customize them easily.
Index ¶
- Variables
- type BodyDecoder
- func (m BodyDecoder) AddDecoder(contentType string, decoder Decoder) BodyDecoder
- func (m BodyDecoder) Doc(builder *openapi.DocBuilder) error
- func (m BodyDecoder) SetDecoders(decoders map[string]Decoder) BodyDecoder
- func (m BodyDecoder) SetDefaultContentType(contentType string) BodyDecoder
- func (m BodyDecoder) SetForcedContentType(contentType string) BodyDecoder
- func (m BodyDecoder) Wrap(h handler.Handler) handler.Handler
- type BodyValidation
- type Decoder
- type Encoder
- type FuncAsDecoder
- type FuncAsEncoder
- type Log
- type PathParameters
- type QueryParameters
- type Recover
- type ResponseWriter
- func (m ResponseWriter) AddEncoder(contentType string, encoder Encoder) ResponseWriter
- func (m ResponseWriter) SetDefaultContentType(contentType string) ResponseWriter
- func (m ResponseWriter) SetEncoders(encoders map[string]Encoder) ResponseWriter
- func (m ResponseWriter) SetForcedContentType(contentType string) ResponseWriter
- func (m ResponseWriter) StatusCodeFromHTTPServeResult(resp interface{}, err error) int
- func (m ResponseWriter) Wrap(h handler.Handler) handler.Handler
- type WithStatusCode
Constants ¶
This section is empty.
Variables ¶
var DecoderByContentType = map[string]Decoder{ "application/json": FuncAsDecoder(json.Unmarshal), "application/xml": FuncAsDecoder(xml.Unmarshal), }
DecoderByContentType contain all built in decoders
var Defaults = []handler.Middleware{ MakeJSONResponseWriter(), Log{}, Recover{}, }
Defaults includes all default middlewares. You can update this list if you want to change middleware configs for all you handlers.
var EncoderByContentType = map[string]Encoder{ "application/json": FuncAsEncoder(json.Marshal), "application/xml": FuncAsEncoder(xml.Marshal), }
EncoderByContentType contain all built in decoders
Functions ¶
This section is empty.
Types ¶
type BodyDecoder ¶
type BodyDecoder struct { // BodyPtr is a pointer to the variable you want to unmarshal your request body into. BodyPtr interface{} // Decoders is the list the available Decoders by content-type. Decoders map[string]Decoder // DefaultContentType is the default content-type if the request don't have any. DefaultContentType string // SkipValidation indicates whether you want to skip body validation or not. SkipValidation bool // ForcedContentType will always decode a body with this content-type. ForcedContentType string }
BodyDecoder is a middleware that will Unmarshal the incoming request body into the Body field.
func Body ¶
func Body(bodyPtr interface{}) BodyDecoder
Body return a preconfigured BodyDecoder with :
- BodyDecoder.DefaultContentType = `application/json`
- BodyDecoder.Decoders with all referenced Decoder
func JsonBody ¶
func JsonBody(bodyPtr interface{}) BodyDecoder
JsonBody return a preconfigured BodyDecoder with :
- BodyDecoder.ForcedContentType = `application/json`
func (BodyDecoder) AddDecoder ¶
func (m BodyDecoder) AddDecoder(contentType string, decoder Decoder) BodyDecoder
AddDecoder add a Decoder in field Decoders and return current instance
func (BodyDecoder) Doc ¶
func (m BodyDecoder) Doc(builder *openapi.DocBuilder) error
Doc implements the openapi.Documented interface
func (BodyDecoder) SetDecoders ¶
func (m BodyDecoder) SetDecoders(decoders map[string]Decoder) BodyDecoder
SetDecoders set Decoders field and return current instance
func (BodyDecoder) SetDefaultContentType ¶
func (m BodyDecoder) SetDefaultContentType(contentType string) BodyDecoder
SetDefaultContentType set DefaultContentType and return current instance
func (BodyDecoder) SetForcedContentType ¶
func (m BodyDecoder) SetForcedContentType(contentType string) BodyDecoder
SetForcedContentType set ForcedContentType and return current instance
type BodyValidation ¶
type BodyValidation interface {
Validate() error
}
BodyValidation interface can be implemented to trigger an auto validation by BodyDecoder middleware.
type FuncAsDecoder ¶
FuncAsDecoder is function type that implements Decoder interface.
func (FuncAsDecoder) Unmarshal ¶
func (f FuncAsDecoder) Unmarshal(b []byte, v interface{}) error
Unmarshal implements the Decoder interface.
type FuncAsEncoder ¶
FuncAsEncoder is function type that implements Encoder interface.
func (FuncAsEncoder) Marshal ¶
func (f FuncAsEncoder) Marshal(v interface{}) ([]byte, error)
Marshal implements the Encoder interface.
type Log ¶
type Log struct{}
Log is a middleware that will: - set the given logger into the request's context. - log any error returned by the next handler
type PathParameters ¶
type PathParameters struct {
Parameters interface{}
}
PathParameters is a middleware that will set the request path parameters into the Parameters field.
func (PathParameters) Doc ¶
func (m PathParameters) Doc(builder *openapi.DocBuilder) error
Doc implements the openapi.Documented interface
type QueryParameters ¶
type QueryParameters struct {
Parameters interface{}
}
QueryParameters is a middleware that will set the request query parameters into the Parameters field.
func (QueryParameters) Doc ¶
func (m QueryParameters) Doc(builder *openapi.DocBuilder) error
Doc implements the openapi.Documented interface
type Recover ¶
type Recover struct{}
Recover middleware will recover from panics and return the panic details as error.
type ResponseWriter ¶
type ResponseWriter struct { // Encoders is the list of available Marshaller by content type. Encoders map[string]Encoder // DefaultContentType is the default content-type if the request don't have any. DefaultContentType string // ForcedContentType will always return a response serialized with this content-type. ForcedContentType string // StatusCode stores response status code. StatusCode int }
ResponseWriter is a middleware that will take the response from the next handler and write it into the response. It will choose the content type based on the request Accept header.
func MakeJSONResponseWriter ¶
func MakeJSONResponseWriter() ResponseWriter
MakeJSONResponseWriter return an initialized ResponseWriter with all supported encoders (see EncoderByContentType) and ResponseWriter.ForcedContentType set to application/json
func MakeResponseWriter ¶
func MakeResponseWriter() ResponseWriter
MakeResponseWriter return an initialized ResponseWriter with all supported encoders (see EncoderByContentType) and ResponseWriter.DefaultContentType set to application/json
func (ResponseWriter) AddEncoder ¶
func (m ResponseWriter) AddEncoder(contentType string, encoder Encoder) ResponseWriter
AddEncoder add an Encoder in field Encoders and return current instance
func (ResponseWriter) SetDefaultContentType ¶
func (m ResponseWriter) SetDefaultContentType(contentType string) ResponseWriter
SetDefaultContentType set DefaultContentType and return current instance
func (ResponseWriter) SetEncoders ¶
func (m ResponseWriter) SetEncoders(encoders map[string]Encoder) ResponseWriter
SetEncoders set Encoders and return current instance
func (ResponseWriter) SetForcedContentType ¶
func (m ResponseWriter) SetForcedContentType(contentType string) ResponseWriter
SetForcedContentType set ForcedContentType and return current instance
func (ResponseWriter) StatusCodeFromHTTPServeResult ¶ added in v0.1.1
func (m ResponseWriter) StatusCodeFromHTTPServeResult(resp interface{}, err error) int
StatusCodeFromHTTPServeResult returns response http status code from http.Serve result
type WithStatusCode ¶
type WithStatusCode interface {
StatusCode() int
}
WithStatusCode is able to return its http status code.