Documentation ¶
Index ¶
Constants ¶
const ( // Megabyte is a pre-defined maximum payload size that can be used in // NewBaseHandler Megabyte = 1024 * 1024 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseHandler ¶
type BaseHandler interface { tracing.Tracer // Read reads the body and tries to decode JSON from it to the given output type Parse(r *http.Request, out interface{}) error // Write writes the response serving the given object as JSON with the given status Write(ctx context.Context, w http.ResponseWriter, status int, obj interface{}) // Error serves the proper error object based on the given error and its type Error(context.Context, http.ResponseWriter, error) }
BaseHandler contains all the base functions every handler should have
type ErrorParser ¶ added in v4.13.0
ErrorParser is a function that parses an error into an HTTP status code and response body.
type Handler ¶ added in v4.13.0
type Handler struct { tracing.Tracer // ErrorParser is used to parse error objects into HTTP status codes and response bodies. ErrorParser ErrorParser // MaxBodyBytes is the maximal request body size, < 0 means the default Megabyte. // Using 0 will disable the limit and allow parsing streams. MaxBodyBytes int64 // Debug was used to enable/disable Debug mode, when enabled error messages will be included in responses. Debug bool }
Handler is the default implementation of BaseHandler and is suitable for use in most REST API implementations.
func NewBaseHandler ¶
NewBaseHandler creates a new base HTTP handler that contains shared logic among all the handlers. The handler supports parsing and writing JSON objects `maxBodyBytes` is the maximal request body size, < 0 means the default Megabyte. Using 0 will disable the limit. `componentName` is used for tracing to identify to which component this handler belongs to.
func NewBaseHandlerWithTracer
deprecated
NewBasehandlerWithTracer create a new base HTTP handler, like NewBaseHandler, but allows the caller to configure the Tracer implementation independently.
Deprecated: you can now configure/override the default Tracer using
h := NewBaseHandler(componentName, maxBodyBytes, debug) h.Tracer = tracing.NewTracer("handlers", componentName)