middleware

package
v0.2.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 19 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
var DecoderByContentType = map[string]Decoder{
	"application/json": FuncAsDecoder(json.Unmarshal),
	"application/xml":  FuncAsDecoder(xml.Unmarshal),
}

DecoderByContentType contain all built in decoders

Defaults includes all default middlewares. You can update this list if you want to change middleware configs for all you handlers.

View Source
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

func (BodyDecoder) Wrap

Wrap implements the request.Middleware interface

type BodyValidation

type BodyValidation interface {
	Validate() error
}

BodyValidation interface can be implemented to trigger an auto validation by BodyDecoder middleware.

type Decoder

type Decoder interface {
	Unmarshal(b []byte, v interface{}) error
}

Decoder is able to unmarshal the body into a value.

type Encoder

type Encoder interface {
	Marshal(v interface{}) ([]byte, error)
}

Encoder is able to marshal.

type FuncAsDecoder

type FuncAsDecoder func(b []byte, v interface{}) error

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

type FuncAsEncoder func(v interface{}) ([]byte, error)

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

func (Log) Wrap

func (m Log) Wrap(h handler.Handler) handler.Handler

Wrap implements the request.Middleware interface

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

func (PathParameters) Wrap

Wrap implements the request.Middleware 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

func (QueryParameters) Wrap

Wrap implements the request.Middleware interface

type Recover

type Recover struct{}

Recover middleware will recover from panics and return the panic details as error.

func (Recover) Wrap

Wrap implements the request.Middleware interface

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

func (ResponseWriter) Wrap

Wrap implements the request.Middleware interface

type WithStatusCode

type WithStatusCode interface {
	StatusCode() int
}

WithStatusCode is able to return its http status code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL