respond

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

Package respond assists handlers to produce reponses

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(ct string, h Renderer) error

Register adds a Renderer to the global Registry

Types

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

A Registry is a collection of Renderers

func CloneRegistry

func CloneRegistry() *Registry

CloneRegistry creates a new Registry using the global as starting point

func NewRegistry

func NewRegistry(renderer ...Renderer) *Registry

NewRegistry creates a new Renderers Registry optionally starting with a given set of Renderers

func (*Registry) Clone

func (reg *Registry) Clone() *Registry

Clone creates a new Registry using the current as starting point.

func (*Registry) Get

func (reg *Registry) Get(ct string) (r Renderer, found bool)

Get retrieves a Renderer, or the Identity

func (*Registry) GetParsed

func (reg *Registry) GetParsed(ct string) (qlist.QualityValue, bool)

GetParsed retrieves the parsed qlist.QualityValue of a given Media Type if the Registry knows it

func (*Registry) Register

func (reg *Registry) Register(ct string, h Renderer) error

Register adds a Renderer to a particular Registry

func (*Registry) Replace

func (reg *Registry) Replace(ct string, h Renderer) (Renderer, error)

Replace registers or replaces a Renderer on the Registry. Returns the previous Renderer assigned to the specified Content-Type

func (*Registry) SetIdentity

func (reg *Registry) SetIdentity(ct string) error

SetIdentity defines the default Content-Type for this Registry. Otherwise it will be the first registered Renderer

func (*Registry) Supports

func (reg *Registry) Supports(types ...string) *Responder

Supports creates a new Responder supporting the given media types

type RenderFunc

type RenderFunc func(w io.Writer, v any) error

A RenderFunc writes the v object encoded

type Renderer

type Renderer interface {
	Render(w io.Writer, v any) error
	ContentType() string
}

Renderer is an object that renders objects with a predetermined encoding

func GetRenderer

func GetRenderer(ct string) (Renderer, bool)

GetRenderer retrieves a Renderer from the global Registry, or its Identity.

func NewRenderer

func NewRenderer(ct string, h RenderFunc) Renderer

NewRenderer creates a Renderer implementation using the given content type string and render functions

type Responder

type Responder struct {
	// contains filtered or unexported fields
}

A Responder is a narrowed set of supported Media Types

func Supports

func Supports(types ...string) *Responder

Supports creates a new Responder supporting the given media types, using the global Registry

func (*Responder) Supports

func (res *Responder) Supports(types ...string) *Responder

Supports sets additional supported media types on the Responder, but they need to exist in the registry

func (*Responder) WithRequest

func (res *Responder) WithRequest(req *http.Request) (*Response, error)

WithRequest builds a new Response for the best accepted type

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response is the stage to build and send the response for a particular request

func (*Response) Accepted

func (r *Response) Accepted(rw http.ResponseWriter) *Response

Accepted prepares a 202 Accepted Response

func (*Response) AddHeader

func (r *Response) AddHeader(key, value string)

AddHeader appends a value to a header key for the response

func (*Response) BadGateway

func (r *Response) BadGateway(rw http.ResponseWriter) *Response

BadGateway prepares a 502 Bad Gateway Response

func (*Response) BadRequest

func (r *Response) BadRequest(rw http.ResponseWriter) *Response

BadRequest prepares a 400 Bad Request Response

func (*Response) Code

func (r *Response) Code() int

Code returns the status set for this response

func (*Response) Conflict

func (r *Response) Conflict(rw http.ResponseWriter) *Response

Conflict prepares a 409 Conflict Response

func (*Response) ContentType

func (r *Response) ContentType() string

ContentType is the preferred Media Type for this Response

func (*Response) Created

func (r *Response) Created(rw http.ResponseWriter) *Response

Created prepares a 201 Created Response

func (*Response) DeleteHeader

func (r *Response) DeleteHeader(key string)

DeleteHeader removes a header key from the response

func (*Response) Forbidden

func (r *Response) Forbidden(rw http.ResponseWriter) *Response

Forbidden prepares a 403 Forbidden Response

func (*Response) Found

func (r *Response) Found(rw http.ResponseWriter, loc string) error

Found sends a 302 Found Response

func (*Response) GatewayTimeout

func (r *Response) GatewayTimeout(rw http.ResponseWriter) *Response

GatewayTimeout prepares a 504 Gateway Timeout Response

func (*Response) Gone

func (r *Response) Gone(rw http.ResponseWriter) *Response

Gone prepares a 410 Gone Response

func (*Response) InternalServerError

func (r *Response) InternalServerError(rw http.ResponseWriter) *Response

InternalServerError prepares a 500 Internal Server Error Response

func (*Response) LengthRequired

func (r *Response) LengthRequired(rw http.ResponseWriter) *Response

LengthRequired prepares a 411 Length Required Response

func (*Response) MethodNotAllowed

func (r *Response) MethodNotAllowed(rw http.ResponseWriter) *Response

MethodNotAllowed prepares a 405 Method Not Allowed Response

func (*Response) MovedPermanently

func (r *Response) MovedPermanently(rw http.ResponseWriter, loc string) error

MovedPermanently sends a 301 Moved Permanently Response

func (*Response) NoContent

func (r *Response) NoContent(rw http.ResponseWriter)

NoContent sends a 204 No Content Response. Render isn't used.

func (*Response) NotAcceptable

func (r *Response) NotAcceptable(rw http.ResponseWriter) *Response

NotAcceptable prepares a 406 Not Acceptable Response

func (*Response) NotFound

func (r *Response) NotFound(rw http.ResponseWriter) *Response

NotFound prepares a 404 Not Found Response

func (*Response) NotImplemented

func (r *Response) NotImplemented(rw http.ResponseWriter) *Response

NotImplemented prepares a 501 Not Implemented Response

func (*Response) OK

func (r *Response) OK(rw http.ResponseWriter) *Response

OK prepares a 200 OK Response

func (*Response) PermanentRedirect

func (r *Response) PermanentRedirect(rw http.ResponseWriter, loc string) error

PermanentRedirect sends a 308 Permanent Redirect Response

func (*Response) PreconditionFailed

func (r *Response) PreconditionFailed(rw http.ResponseWriter) *Response

PreconditionFailed prepares a 412 Precondition Failed Response

func (*Response) Render

func (r *Response) Render(v any) error

Render sends the rendered response to the user

func (*Response) RequestEntityTooLarge

func (r *Response) RequestEntityTooLarge(rw http.ResponseWriter) *Response

RequestEntityTooLarge prepares a 413 Request Entity Too Large Response

func (*Response) SeeOther

func (r *Response) SeeOther(rw http.ResponseWriter, loc string) error

SeeOther sends a 303 See Other Response

func (*Response) ServiceUnavailable

func (r *Response) ServiceUnavailable(rw http.ResponseWriter) *Response

ServiceUnavailable prepares a 503 Service Unavailable Response

func (*Response) SetHeader

func (r *Response) SetHeader(key, value string)

SetHeader sets the value of a header key for the response

func (*Response) TemporaryRedirect

func (r *Response) TemporaryRedirect(rw http.ResponseWriter, loc string) error

TemporaryRedirect sends a 307 Temporary Redirect Response

func (*Response) Unauthorized

func (r *Response) Unauthorized(rw http.ResponseWriter) *Response

Unauthorized prepares a 401 Unauthorized Response

func (*Response) UnprocessableEntity

func (r *Response) UnprocessableEntity(rw http.ResponseWriter) *Response

UnprocessableEntity prepares a 422 Unprocessable Entity Response

func (*Response) UnsupportedMediaType

func (r *Response) UnsupportedMediaType(rw http.ResponseWriter) *Response

UnsupportedMediaType prepares a 415 Unsupported Media Type Response

func (*Response) WithStatus

func (r *Response) WithStatus(code int) *Response

WithStatus sets the status code directly, to be used in conjunction with [WithWriter].

func (*Response) WithWriter

func (r *Response) WithWriter(rw http.ResponseWriter) *Response

WithWriter sets the writer directly, to be used in conjunction with [WithStatus].

Jump to

Keyboard shortcuts

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