handlers

package
v4.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Megabyte is a pre-defined maximum payload size that can be used in
	// NewBaseHandler
	Megabyte = 1024 * 1024
)

Variables

This section is empty.

Functions

func DefaultErrorParser added in v4.13.0

func DefaultErrorParser(ctx context.Context, err error, debug bool) (int, interface{})

DefaultErrorParser provides a reasonable default error parser that can handle the various sentile errors in go-base as well as ozzo-validation errors.

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

type ErrorParser = func(ctx context.Context, err error, debug bool) (int, interface{})

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

func NewBaseHandler(componentName string, maxBodyBytes int64, debug bool) *Handler

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

func NewBaseHandlerWithTracer(tracer tracing.Tracer, maxBodyBytes int64, debug bool) *Handler

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)

func (*Handler) Error added in v4.13.0

func (h *Handler) Error(ctx context.Context, w http.ResponseWriter, err error)

func (*Handler) Parse added in v4.13.0

func (h *Handler) Parse(r *http.Request, out interface{}) error

func (*Handler) Write added in v4.13.0

func (h *Handler) Write(ctx context.Context, w http.ResponseWriter, status int, obj interface{})

Jump to

Keyboard shortcuts

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