Documentation ¶
Index ¶
- Variables
- func Respond(rw http.ResponseWriter, request *http.Request, body any, opts ...RespondOption)
- func RespondError(rw http.ResponseWriter, request *http.Request, err error, ...)
- func RespondStream(rw http.ResponseWriter, request *http.Request, contentType string, ...)
- func WrapHTTPClient(client *http.Client, opts ...RoundTripOption) *http.Client
- type BeforeRespondErrorFunc
- type BeforeRespondFunc
- type BeforeRespondStreamFunc
- type BodyTransformFunc
- type BodyTransformer
- type BufferedReadWriter
- type BufferedReader
- type ClonedBody
- type Compressor
- type ContentEncodingHint
- type ContentTypeHint
- type Encoder
- type ErrorBodyTransformer
- type FilterRequest
- type FilterRespond
- type GzipCompression
- type JSONEncoder
- type Middleware
- type NoCompression
- type NoopBodyTransform
- type NoopCloneBody
- type RespondBody
- type RespondContext
- type RespondErrorBody
- type RespondErrorHookContext
- type RespondHook
- type RespondHookContext
- type RespondHookList
- type RespondHookOption
- type RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) BeforeRespond(before BeforeRespondFunc) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) BeforeRespondError(before BeforeRespondErrorFunc) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) BeforeRespondStream(before BeforeRespondStreamFunc) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) FilterRequest(filter FilterRequest) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) FilterRespondStream(filter FilterRespond) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) OnRespond(on ResponseHookFunc) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) OnRespondError(on ResponseErrorHookFunc) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) OnRespondStream(on ResponseStreamHookFunc) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) ReadRequestBodyLimit(limit int) RespondHookOptionBuilder
- func (h RespondHookOptionBuilder) ReadRespondBodyStreamLimit(limit int) RespondHookOptionBuilder
- type RespondOption
- type RespondOptionBuilder
- func (r RespondOptionBuilder) AddCallerSkip(i int) RespondOptionBuilder
- func (r RespondOptionBuilder) Apply(o *RespondContext)
- func (r RespondOptionBuilder) CallerSkip(i int) RespondOptionBuilder
- func (r RespondOptionBuilder) Compressor(compressor Compressor) RespondOptionBuilder
- func (r RespondOptionBuilder) Encoder(encoder Encoder) RespondOptionBuilder
- func (r RespondOptionBuilder) ErrorTransformer(transformer ErrorBodyTransformer) RespondOptionBuilder
- func (r RespondOptionBuilder) StatusCode(code int) RespondOptionBuilder
- func (r RespondOptionBuilder) StreamCompressor(compressor StreamCompressor) RespondOptionBuilder
- func (r RespondOptionBuilder) Transformer(transformer BodyTransformer) RespondOptionBuilder
- type RespondOptionFunc
- type RespondStreamBody
- type RespondStreamHookContext
- type Responder
- func (r *Responder) RegisterHook(hook RespondHook)
- func (r *Responder) RequestBodyCloner() Middleware
- func (r Responder) Respond(rw http.ResponseWriter, request *http.Request, body any, opts ...RespondOption)
- func (r Responder) RespondError(rw http.ResponseWriter, request *http.Request, errPayload error, ...)
- func (r *Responder) RespondStream(rw http.ResponseWriter, request *http.Request, contentType string, ...)
- func (r *Responder) SetBodyTransformer(transform BodyTransformer)
- func (r *Responder) SetCallerDepth(depth int)
- func (r *Responder) SetCompressor(compressor Compressor)
- func (r *Responder) SetEncoder(encoder Encoder)
- func (r *Responder) SetErrorTransformer(errorTransformer ErrorBodyTransformer)
- func (r *Responder) SetMaleo(t *maleo.Maleo)
- func (r *Responder) SetStreamCompressor(streamCompressor StreamCompressor)
- type ResponseErrorHookFunc
- type ResponseHookFunc
- type ResponseStreamHookFunc
- type RoundTrip
- type RoundTripContext
- type RoundTripExecuteHookFunc
- type RoundTripFilterRequest
- type RoundTripFilterResponse
- type RoundTripHook
- type RoundTripHookOption
- type RoundTripHookOptionBuilder
- func (r RoundTripHookOptionBuilder) FilterRequest(filter RoundTripFilterRequest) RoundTripHookOptionBuilder
- func (r RoundTripHookOptionBuilder) FilterResponse(filter RoundTripFilterResponse) RoundTripHookOptionBuilder
- func (r RoundTripHookOptionBuilder) Log(log RoundTripExecuteHookFunc) RoundTripHookOptionBuilder
- func (r RoundTripHookOptionBuilder) ReadRequestBodyLimit(limit int) RoundTripHookOptionBuilder
- func (r RoundTripHookOptionBuilder) ReadResponseBodyLimit(limit int) RoundTripHookOptionBuilder
- type RoundTripHookOptionFunc
- type RoundTripOption
- type RoundTripOptionBuilder
- type RoundTripOptionFunc
- type SimpleErrorTransformer
- type StreamCompressor
Constants ¶
This section is empty.
Variables ¶
var Exported exported
var Option option
Option holds all the available options for responding with maleohttp.
Functions ¶
func Respond ¶
func Respond(rw http.ResponseWriter, request *http.Request, body any, opts ...RespondOption)
Respond with the given body and options.
body is expected to be a serializable type. For streams, use RespondStream.
HTTP status code by default is http.StatusOK. If body implements maleo.HTTPCodeHint, the status code will be set to the value returned by the maleo.HTTPCodeHint method. If the maleohttp.Option.Responder().StatusCode() RespondOption is set, it will override the status regardless of the maleo.HTTPCodeHint.
There's a special case if you pass http.NoBody as body, there will be no respond body related operations executed. StatusCode default value is STILL http.StatusOK. If you wish to set the status code to http.StatusNoContent, you can still override the output by setting the related RespondOption.
Body of nil has different treatment with http.NoBody. if body is nil, the nil value is still passed to the BodyTransformer implementer, therefore the final result body may not actually be empty.
func RespondError ¶
func RespondError(rw http.ResponseWriter, request *http.Request, err error, opts ...RespondOption)
RespondError writes the given error to the http.ResponseWriter.
error is expected to be a serializable type.
HTTP Status code by default is http.StatusInternalServerError. If error implements maleo.HTTPCodeHint, the status code will be set to the value returned by the maleo.HTTPCodeHint method. If the Option.Responder().SetStatusCode() RespondOption is set, it will override the status regardless of the maleo.HTTPCodeHint.
if err is nil, it will be replaced with "Internal Server Error" message. It is done this way, because the library assumes that the user mishandled the method and to prevent sending empty values, a generic Internal Server Error message will be sent instead. If you wish to send an empty response, use Respond with http.NoBody as body.
func RespondStream ¶
func RespondStream(rw http.ResponseWriter, request *http.Request, contentType string, body io.Reader, opts ...RespondOption)
RespondStream writes the given stream to the http.ResponseWriter.
If the stream implements maleo.HTTPCodeHint, the status code will be set to the value returned by the maleo.HTTPCodeHint.
If the Compressor supports StreamCompressor, the stream will be compressed by said StreamCompressor and written to the http.ResponseWriter.
There's a special case if you pass http.NoBody as body, there will be no respond body related operations executed. StatusCode default value is STILL http.StatusOK. You can still override this output by setting the related RespondOption. With http.NoBody as body, maleohttp will immediately respond with status code after RespondOption are evaluated and end the process.
Body of nil will be treated as http.NoBody.
func WrapHTTPClient ¶
func WrapHTTPClient(client *http.Client, opts ...RoundTripOption) *http.Client
WrapHTTPClient wraps the given http.Client with maleohttp.RoundTrip implementation to support logging with maleo engine.
if the passed client is nil, maleohttp will use http.DefaultClient. if the client.Transport is nil, maleohttp will use http.DefaultTransport as base.
By default, RoundTrip uses maleo's Global Instance. You may override this with maleohttp.Option.RoundTrip().Maleo(*maleo.Maleo)
Caller by default points to where Client.Do(*http.Request) is called, but this assumes you don't use other http.RoundTripper or not using custom client. if the caller location is incorrect, You may override this with maleohttp.Option.RoundTrip().AddCallerDepth(int) or maleohttp.Option.RoundTrip().CallerDepth(int)
For reference, the default caller depth is 6.
Types ¶
type BeforeRespondErrorFunc ¶ added in v0.5.0
type BeforeRespondErrorFunc = func(ctx *RespondContext, rw http.ResponseWriter, request *http.Request, payload error) *RespondContext
type BeforeRespondFunc ¶
type BeforeRespondFunc = func(ctx *RespondContext, rw http.ResponseWriter, request *http.Request, body any) *RespondContext
type BeforeRespondStreamFunc ¶ added in v0.5.0
type BeforeRespondStreamFunc = func(ctx *RespondContext, rw http.ResponseWriter, request *http.Request, body io.Reader) *RespondContext
type BodyTransformFunc ¶
BodyTransformFunc is a convenient function that implements BodyTransformer.
func (BodyTransformFunc) BodyTransform ¶
func (b BodyTransformFunc) BodyTransform(ctx context.Context, input any) any
type BodyTransformer ¶
type BodyTransformer interface { // BodyTransform transform given input into another shape. This is called before the Encoder. // // input may be nil so make sure to take account for such situation. // // If the returned value is nil, the process will be short-circuited and the response body will be empty. BodyTransform(ctx context.Context, input any) any }
type BufferedReadWriter ¶
type BufferedReadWriter interface { BufferedReader io.Writer // Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset() }
type BufferedReader ¶
type BufferedReader interface { io.Reader // String returns the body as a string. This is very often a copy operation of the bytes. String() string // Bytes returns the bytes of the body. This usually is not a copy operation, so the underlying array may be modified // somewhere else or in the future. Use String to ensure an immutable copy. Bytes() []byte // Len returns the number of bytes in the buffer. Len() int }
BufferedReader is an extension around io.Reader that actually already have the values in memory and ready to be consumed.
type ClonedBody ¶
type ClonedBody interface { BufferedReader // CloneBytes returns a copy of the body bytes. Like String() but returns a copy of the bytes. CloneBytes() []byte // Truncated returns true if the body was truncated. Truncated() bool // Reader returns the reader that contains the body. // // Every call creates a new fresh cursor to the same underlying array. So multiple Readers created from this method // all have it's own read position. Reader() BufferedReader }
type Compressor ¶
type Compressor interface { ContentEncodingHint // Compress the given bytes. If the compressed bytes are smaller than the original bytes, the compressed bytes will // be returned. Otherwise, the original bytes will be returned. // // If ok is true, the compressed bytes will be used and the Content-Encoding header will be set with the // value returned from ContentEncoding method. Otherwise, the original bytes will be used. // // If err is not nil, the original bytes will be used and the error will be logged by Maleo at warn level. Compress(b []byte) (compressed []byte, ok bool, err error) }
type ContentEncodingHint ¶
type ContentEncodingHint interface { // ContentEncoding returns the value of the Content-Encoding header. If empty, the content-encoding header will be // set by the http framework. ContentEncoding() string }
type ContentTypeHint ¶
type ContentTypeHint interface { // ContentType Returns the content type of the encoded data. ContentType() string }
type Encoder ¶
type Encoder interface { ContentTypeHint // Encode the input into a byte array. Encode(input any) ([]byte, error) }
type ErrorBodyTransformer ¶
type ErrorBodyTransformer interface { // ErrorBodyTransform transform given input into another shape. This is called before the Encoder. // // input may be nil so make sure to take account for such situation. // // If the returned value is nil, the process will be short-circuited and the response body will be empty. ErrorBodyTransform(ctx context.Context, err error) any }
type FilterRequest ¶
type GzipCompression ¶
type GzipCompression struct {
// contains filtered or unexported fields
}
func NewGzipCompression ¶
func NewGzipCompression() *GzipCompression
NewGzipCompression creates a new GzipCompression.
func NewGzipCompressionWithLevel ¶
func NewGzipCompressionWithLevel(lvl int) *GzipCompression
NewGzipCompressionWithLevel creates a new GzipCompression with specified compression level.
func (GzipCompression) Compress ¶
func (g GzipCompression) Compress(b []byte) ([]byte, bool, error)
Compress implements maleohttp.Compressor.
func (GzipCompression) ContentEncoding ¶
func (g GzipCompression) ContentEncoding() string
ContentEncoding implements maleohttp.ContentEncodingHint.
func (GzipCompression) StreamCompress ¶
StreamCompress implements maleohttp.StreamCompressor.
type JSONEncoder ¶
type JSONEncoder struct {
// contains filtered or unexported fields
}
func (*JSONEncoder) ContentType ¶
func (j *JSONEncoder) ContentType() string
func (*JSONEncoder) SetHtmlEscape ¶
func (j *JSONEncoder) SetHtmlEscape(htmlEscape bool)
SetHtmlEscape Sets the htmlEscape flag. If set to true, HTML characters will be escaped. Useful if you plan to embed HTML in a JSON field.
func (*JSONEncoder) SetIndent ¶
func (j *JSONEncoder) SetIndent(indent string)
SetIndent Sets the indent for every level line. Used for pretty print JSON. Empty value disable indentation.
func (*JSONEncoder) SetPrefix ¶
func (j *JSONEncoder) SetPrefix(prefix string)
SetPrefix Sets the prefix of the output for every line. Empty value disable prefix.
type Middleware ¶
func RequestBodyCloner ¶
func RequestBodyCloner() Middleware
type NoCompression ¶
type NoCompression struct{}
NoCompression is a Compressor that does nothing. Basically it's an Uncompressed operation.
func (NoCompression) ContentEncoding ¶
func (n NoCompression) ContentEncoding() string
func (NoCompression) StreamCompress ¶
type NoopBodyTransform ¶
type NoopBodyTransform struct{}
NoopBodyTransform is a BodyTransformer that does nothing and only return the input as is.
func (NoopBodyTransform) BodyTransform ¶
func (n NoopBodyTransform) BodyTransform(_ context.Context, input any) any
type NoopCloneBody ¶
type NoopCloneBody struct{}
func (NoopCloneBody) Bytes ¶
func (n NoopCloneBody) Bytes() []byte
func (NoopCloneBody) CloneBytes ¶
func (n NoopCloneBody) CloneBytes() []byte
func (NoopCloneBody) Len ¶
func (n NoopCloneBody) Len() int
func (NoopCloneBody) Reader ¶
func (NoopCloneBody) Reader() BufferedReader
func (NoopCloneBody) String ¶
func (n NoopCloneBody) String() string
func (NoopCloneBody) Truncated ¶
func (n NoopCloneBody) Truncated() bool
type RespondBody ¶
type RespondContext ¶
type RespondContext struct { Encoder Encoder BodyTransformer BodyTransformer Compressor Compressor StreamCompressor StreamCompressor StatusCode int ErrorBodyTransformer ErrorBodyTransformer CallerDepth int Caller maleo.Caller }
type RespondErrorBody ¶
type RespondErrorHookContext ¶
type RespondErrorHookContext struct { ResponseBody RespondErrorBody // contains filtered or unexported fields }
type RespondHook ¶
type RespondHook interface { AcceptRequestBodySize(r *http.Request) int AcceptResponseBodyStreamSize(respondContentType string, request *http.Request) int // BeforeRespond is called after populating the handlers and values but before doing any operation to the response writer. // // Implementer must not write any status code or body to the response writer. // It will be set by the library. // // Implementer is allowed to set any extra header or cookie to the response writer. // // Implementer can modify everything else. Return the ctx to continue the operation. BeforeRespond(ctx *RespondContext, rw http.ResponseWriter, request *http.Request, body any) *RespondContext // BeforeRespondError is called after populating the handlers and values but before doing any operation to the response writer. // // Implementer must not write any status code or body to the response writer. // It will be set by the library. // // Implementer is allowed to set any extra header or cookie to the response writer. // // Implementer can modify everything else. Return the ctx to continue the operation. BeforeRespondError(ctx *RespondContext, rw http.ResponseWriter, request *http.Request, err error) *RespondContext // BeforeRespondStream is called after populating the handlers and values but before doing any operation to the response writer. // // Implementer must not write any status code or body to the response writer. // It will be set by the library. // // Implementer is allowed to set any extra header or cookie to the response writer. // // Implementer can modify everything else. Return the ctx to continue the operation. BeforeRespondStream(ctx *RespondContext, rw http.ResponseWriter, request *http.Request, body io.Reader) *RespondContext RespondHook(ctx *RespondHookContext) RespondErrorHookContext(ctx *RespondErrorHookContext) RespondStreamHookContext(ctx *RespondStreamHookContext) }
func NewLoggerHook ¶
func NewLoggerHook(opts ...RespondHookOption) RespondHook
func NewRespondHook ¶
func NewRespondHook(opts ...RespondHookOption) RespondHook
type RespondHookContext ¶
type RespondHookContext struct { ResponseBody RespondBody // contains filtered or unexported fields }
type RespondHookList ¶
type RespondHookList []RespondHook
func (RespondHookList) CountMaximumRequestBodyRead ¶
func (r RespondHookList) CountMaximumRequestBodyRead(request *http.Request) int
func (RespondHookList) CountMaximumRespondBodyRead ¶
func (r RespondHookList) CountMaximumRespondBodyRead(contentType string, request *http.Request) int
type RespondHookOption ¶
type RespondHookOption interface {
// contains filtered or unexported methods
}
type RespondHookOptionBuilder ¶
type RespondHookOptionBuilder []RespondHookOption
func (RespondHookOptionBuilder) BeforeRespond ¶
func (h RespondHookOptionBuilder) BeforeRespond(before BeforeRespondFunc) RespondHookOptionBuilder
BeforeRespond is called after populating the handlers and values but before doing any operation to the response writer.
You must not write any status code or body to the response writer. It will be set by the library.
You are allowed to set any extra header or cookie to the response writer.
You can modify everything else. Return the ctx to continue the operation.
func (RespondHookOptionBuilder) BeforeRespondError ¶ added in v0.5.0
func (h RespondHookOptionBuilder) BeforeRespondError(before BeforeRespondErrorFunc) RespondHookOptionBuilder
BeforeRespondError is called after populating the handlers and values but before doing any operation to the response writer.
You must not write any status code or body to the response writer. It will be set by the library.
You are allowed to set any extra header or cookie to the response writer.
You can modify everything else. Return the ctx to continue the operation.
func (RespondHookOptionBuilder) BeforeRespondStream ¶ added in v0.5.0
func (h RespondHookOptionBuilder) BeforeRespondStream(before BeforeRespondStreamFunc) RespondHookOptionBuilder
BeforeRespondStream is called after populating the handlers and values but before doing any operation to the response writer.
You must not write any status code or body to the response writer. It will be set by the library.
You are allowed to set any extra header or cookie to the response writer.
You can modify everything else. Return the ctx to continue the operation.
func (RespondHookOptionBuilder) FilterRequest ¶
func (h RespondHookOptionBuilder) FilterRequest(filter FilterRequest) RespondHookOptionBuilder
FilterRequest filter requests whose body are going to be cloned. Defaults to filter only human readable content type.
func (RespondHookOptionBuilder) FilterRespondStream ¶
func (h RespondHookOptionBuilder) FilterRespondStream(filter FilterRespond) RespondHookOptionBuilder
FilterRespondStream filter http server responds to clone. Defaults to filter only human readable content type.
func (RespondHookOptionBuilder) OnRespond ¶
func (h RespondHookOptionBuilder) OnRespond(on ResponseHookFunc) RespondHookOptionBuilder
OnRespond provides callback to be run after Responder writes the body.
OnRespond callback is executed when maleohttp.Responder.Respond() is called.
By default, the hook will use this api to call maleo to log the request and respond.
func (RespondHookOptionBuilder) OnRespondError ¶
func (h RespondHookOptionBuilder) OnRespondError(on ResponseErrorHookFunc) RespondHookOptionBuilder
OnRespondError provides callback to be run after Responder writes the body.
OnRespondError callback is executed when maleohttp.Responder.RespondError() is called.
By default, the hook will use this api to call maleo to log the request and respond.
func (RespondHookOptionBuilder) OnRespondStream ¶
func (h RespondHookOptionBuilder) OnRespondStream(on ResponseStreamHookFunc) RespondHookOptionBuilder
OnRespondStream provides callback to be run after Responder writes the body.
OnRespondStream callback is executed when maleohttp.Responder.RespondStream() is called.
By default, the hook will use this api to call maleo to log the request and respond.
func (RespondHookOptionBuilder) ReadRequestBodyLimit ¶
func (h RespondHookOptionBuilder) ReadRequestBodyLimit(limit int) RespondHookOptionBuilder
ReadRequestBodyLimit limits the number of bytes body being cloned. Defaults to 1MB.
Negative value will make the hook clones all the body.
Body will not be read if FilterRequest returns false.
func (RespondHookOptionBuilder) ReadRespondBodyStreamLimit ¶
func (h RespondHookOptionBuilder) ReadRespondBodyStreamLimit(limit int) RespondHookOptionBuilder
ReadRespondBodyStreamLimit limits the number of bytes of respond body being cloned. Defaults to 1MB.
Negative value will make the hook clones all the body.
Body will not be read if FilterResponds returns false.
type RespondOption ¶
type RespondOption interface {
Apply(*RespondContext)
}
type RespondOptionBuilder ¶
type RespondOptionBuilder []RespondOption
func (RespondOptionBuilder) AddCallerSkip ¶
func (r RespondOptionBuilder) AddCallerSkip(i int) RespondOptionBuilder
AddCallerSkip adds the caller skip value to be used for the response to get the caller information.
func (RespondOptionBuilder) Apply ¶
func (r RespondOptionBuilder) Apply(o *RespondContext)
func (RespondOptionBuilder) CallerSkip ¶
func (r RespondOptionBuilder) CallerSkip(i int) RespondOptionBuilder
CallerSkip overrides the caller skip to be used for the response to get the caller information.
func (RespondOptionBuilder) Compressor ¶
func (r RespondOptionBuilder) Compressor(compressor Compressor) RespondOptionBuilder
Compressor overrides the Compressor to be used for compressing the response body.
func (RespondOptionBuilder) Encoder ¶
func (r RespondOptionBuilder) Encoder(encoder Encoder) RespondOptionBuilder
Encoder overrides the Encoder to be used for encoding the response body.
func (RespondOptionBuilder) ErrorTransformer ¶
func (r RespondOptionBuilder) ErrorTransformer(transformer ErrorBodyTransformer) RespondOptionBuilder
func (RespondOptionBuilder) StatusCode ¶
func (r RespondOptionBuilder) StatusCode(code int) RespondOptionBuilder
StatusCode overrides the status code to be used for the response.
func (RespondOptionBuilder) StreamCompressor ¶
func (r RespondOptionBuilder) StreamCompressor(compressor StreamCompressor) RespondOptionBuilder
StreamCompressor overrides the StreamCompressor to be used for compressing the response body.
func (RespondOptionBuilder) Transformer ¶
func (r RespondOptionBuilder) Transformer(transformer BodyTransformer) RespondOptionBuilder
Transformer overrides the transformer to be used for transforming the response body.
type RespondOptionFunc ¶
type RespondOptionFunc func(*RespondContext)
func (RespondOptionFunc) Apply ¶
func (r RespondOptionFunc) Apply(o *RespondContext)
type RespondStreamBody ¶
type RespondStreamBody struct { Value ClonedBody ContentType string }
type RespondStreamHookContext ¶
type RespondStreamHookContext struct { ResponseBody RespondStreamBody // contains filtered or unexported fields }
type Responder ¶
type Responder struct {
// contains filtered or unexported fields
}
Responder handles the response and writing to http.ResponseWriter.
func NewResponder ¶
func NewResponder() *Responder
NewResponder creates a new Responder instance.
It has the following default values:
- Encoder: JSONEncoder (encodes to JSON)
- BodyTransformer: NoopBodyTransform (does nothing to whatever value you pass in)
- ErrorBodyTransformer: SimpleErrorTransformer (encodes error to {"error": "message/err.Error()"}) with JSONEncoder. Different Encoder may have different output.
- Maleo: points to the global maleo instance
- Compressor: NoCompression.
func (*Responder) RegisterHook ¶
func (r *Responder) RegisterHook(hook RespondHook)
func (*Responder) RequestBodyCloner ¶
func (r *Responder) RequestBodyCloner() Middleware
func (Responder) Respond ¶
func (r Responder) Respond(rw http.ResponseWriter, request *http.Request, body any, opts ...RespondOption)
Respond with the given body and options.
body is expected to be a serializable type. For streams, use RespondStream.
HTTP status by default is http.StatusOK. If body implements maleo.HTTPCodeHint, the status code will be set to the value returned by the maleo.HTTPCodeHint method. If the maleohttp.Option.StatusCode RespondOption is set, it will override the status regardless of the maleo.HTTPCodeHint.
There's a special case if you pass http.NoBody as body, there will be no respond body related operations executed. StatusCode default value is STILL http.StatusOK. If you wish to set the status code to http.StatusNoContent, you can still override this output by setting the related RespondOption.
Body of nil has different treatment with http.NoBody. if body is nil, the nil value is still passed to the BodyTransformer implementer, therefore the final result body may not actually be empty.
func (Responder) RespondError ¶
func (r Responder) RespondError(rw http.ResponseWriter, request *http.Request, errPayload error, opts ...RespondOption)
RespondError writes the given error to the http.ResponseWriter.
errPayload is expected to be a serializable type.
HTTP Status code by default is http.StatusInternalServerError. If error implements maleo.HTTPCodeHint, the status code will be set to the value returned by the maleo.HTTPCodeHint method. If the maleohttp.Option.StatusCode RespondOption is set, it will override the status regardless of the maleo.HTTPCodeHint.
if err is nil, it will be replaced with "Internal Server Error" message. It is done this way, because the library assumes that you mishandled the method and to prevent sending empty values, a generic Internal Server Error message will be sent instead. If you wish to send an empty response, use Respond with http.NoBody as body.
func (*Responder) RespondStream ¶
func (r *Responder) RespondStream(rw http.ResponseWriter, request *http.Request, contentType string, body io.Reader, opts ...RespondOption)
RespondStream writes the given stream to the http.ResponseWriter.
If the stream implements maleo.HTTPCodeHint, the status code will be set to the value returned by the maleo.HTTPCodeHint.
There's a special case if you pass http.NoBody as body, there will be no respond body related operations executed. StatusCode default value is set to http.StatusNoContent. You can still override this output by setting the related RespondOption. With http.NoBody as body, Maleohttp will immediately respond with status code after RespondOption are evaluated and end the process.
Body of nil will be treated as http.NoBody.
func (*Responder) SetBodyTransformer ¶
func (r *Responder) SetBodyTransformer(transform BodyTransformer)
SetBodyTransformer sets the BodyTransformer to be used by the Responder.
func (*Responder) SetCallerDepth ¶
SetCallerDepth sets the caller depth to be used to get caller function by the Responder.
func (*Responder) SetCompressor ¶
func (r *Responder) SetCompressor(compressor Compressor)
SetCompressor sets the compression to be used by the Responder.
func (*Responder) SetEncoder ¶
SetEncoder sets the Encoder to be used by the Responder.
func (*Responder) SetErrorTransformer ¶
func (r *Responder) SetErrorTransformer(errorTransformer ErrorBodyTransformer)
SetErrorTransformer sets the ErrorBodyTransformer to be used by the Responder.
func (*Responder) SetStreamCompressor ¶
func (r *Responder) SetStreamCompressor(streamCompressor StreamCompressor)
SetStreamCompressor sets the stream compression to be used by the Responder.
type ResponseErrorHookFunc ¶
type ResponseErrorHookFunc = func(ctx *RespondErrorHookContext)
type ResponseHookFunc ¶
type ResponseHookFunc = func(ctx *RespondHookContext)
type ResponseStreamHookFunc ¶
type ResponseStreamHookFunc = func(ctx *RespondStreamHookContext)
type RoundTrip ¶
type RoundTrip struct {
// contains filtered or unexported fields
}
func NewRoundTrip ¶
func NewRoundTrip(opts ...RoundTripOption) *RoundTrip
NewRoundTrip creates a new instance of http.RoundTripper implementation that wraps around the default http.RoundTripper. It provides logging support using maleo engine.
By default, RoundTrip uses maleo's Global Instance. You may override this with maleohttp.Option.RoundTrip().Maleo(*maleo.Maleo)
Caller by default points to where Client.Do(*http.Request) is called, but this assumes you don't use other http.RoundTripper or using custom client. if the caller location is incorrect, You may override this with maleohttp.Option.RoundTrip().AddCallerDepth(int) or maleohttp.Option.RoundTrip().CallerDepth(int)
For reference, the default caller depth is 6.
func WrapRoundTripper ¶
func WrapRoundTripper(rt http.RoundTripper, opts ...RoundTripOption) *RoundTrip
WrapRoundTripper wraps the given http.RoundTripper with maleohttp.RoundTrip implementation to support logging with maleo engine.
By default, RoundTrip uses maleo's Global Instance. You may override this with maleohttp.Option.RoundTrip().Maleo(*maleo.Maleo)
Caller by default points to where Client.Do(*http.Request) is called, but this assumes you don't use other http.RoundTripper or using custom client. if the caller location is incorrect, You may override this with maleohttp.Option.RoundTrip().AddCallerDepth(int) or maleohttp.Option.RoundTrip().CallerDepth(int)
For reference, the default caller depth is 6.
type RoundTripContext ¶
type RoundTripExecuteHookFunc ¶
type RoundTripExecuteHookFunc = func(*RoundTripContext)
type RoundTripFilterRequest ¶
type RoundTripFilterResponse ¶
type RoundTripHook ¶
type RoundTripHook interface { AcceptRequestBodySize(r *http.Request) int AcceptResponseBodySize(req *http.Request, res *http.Response) int ExecuteHook(ctx *RoundTripContext) }
func NewRoundTripHook ¶
func NewRoundTripHook(opts ...RoundTripHookOption) RoundTripHook
type RoundTripHookOption ¶
type RoundTripHookOption interface {
// contains filtered or unexported methods
}
type RoundTripHookOptionBuilder ¶
type RoundTripHookOptionBuilder []RoundTripHookOption
func (RoundTripHookOptionBuilder) FilterRequest ¶
func (r RoundTripHookOptionBuilder) FilterRequest(filter RoundTripFilterRequest) RoundTripHookOptionBuilder
func (RoundTripHookOptionBuilder) FilterResponse ¶
func (r RoundTripHookOptionBuilder) FilterResponse(filter RoundTripFilterResponse) RoundTripHookOptionBuilder
func (RoundTripHookOptionBuilder) Log ¶
func (r RoundTripHookOptionBuilder) Log(log RoundTripExecuteHookFunc) RoundTripHookOptionBuilder
func (RoundTripHookOptionBuilder) ReadRequestBodyLimit ¶
func (r RoundTripHookOptionBuilder) ReadRequestBodyLimit(limit int) RoundTripHookOptionBuilder
func (RoundTripHookOptionBuilder) ReadResponseBodyLimit ¶
func (r RoundTripHookOptionBuilder) ReadResponseBodyLimit(limit int) RoundTripHookOptionBuilder
type RoundTripHookOptionFunc ¶
type RoundTripHookOptionFunc func(*roundTripHook)
type RoundTripOption ¶
type RoundTripOption interface {
// contains filtered or unexported methods
}
type RoundTripOptionBuilder ¶
type RoundTripOptionBuilder []RoundTripOption
func (RoundTripOptionBuilder) AddCallerDepth ¶
func (rtob RoundTripOptionBuilder) AddCallerDepth(depth int) RoundTripOptionBuilder
AddCallerDepth adds the caller depth to the caller stack.
func (RoundTripOptionBuilder) CallerDepth ¶
func (rtob RoundTripOptionBuilder) CallerDepth(depth int) RoundTripOptionBuilder
CallerDepth sets the caller depth to the caller stack.
func (RoundTripOptionBuilder) Maleo ¶
func (rtob RoundTripOptionBuilder) Maleo(t *maleo.Maleo) RoundTripOptionBuilder
Maleo sets the maleo instance to use for logging.
type RoundTripOptionFunc ¶
type RoundTripOptionFunc func(*RoundTrip)
type SimpleErrorTransformer ¶
type SimpleErrorTransformer struct{}
func (SimpleErrorTransformer) ErrorBodyTransform ¶
func (n SimpleErrorTransformer) ErrorBodyTransform(_ context.Context, err error) any
type StreamCompressor ¶
type StreamCompressor interface { ContentEncodingHint // StreamCompress compresses the given reader and returns a new reader that will give the compressed data. // // If ok is true, the compressed bytes will be used and the Content-Encoding header will be set with the // value returned from ContentEncoding method. Otherwise, the original stream will be used. StreamCompress(contentType string, origin io.Reader) (io.Reader, bool) }
Source Files ¶
- body_clone.go
- compress.go
- compress_gzip.go
- encoder.go
- export.go
- http.go
- option.go
- respond.go
- respond_error.go
- respond_exported.go
- respond_hook.go
- respond_hook_option.go
- respond_logger.go
- respond_middleware.go
- respond_ok.go
- respond_option.go
- respond_stream.go
- round_trip.go
- round_trip_hook.go
- transform.go
- utils.go