response

package
v0.0.0-...-519d24f Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentType string = "content-type"
)

Widely used headers

Variables

View Source
var NoContent = &noContent{}
View Source
var UseEmptyIfSliceIsZero = true

UseEmptyIfSliceIsZero is a configuration variable about if the data is an empty slice ([]T{}), then render [] instead of null (Go's default JSON behavior)

View Source
var UseFormattedJSON = false

UseFormattedJSON is a configuration variable about if json object is formatted or not.

Functions

This section is empty.

Types

type Body

type Body interface {
	Render(ctx context.Context, w io.Writer)
}

Body is an interface to write response

type HTTPStatus

type HTTPStatus int

HTTPStatus is an enum for http status code

const (
	HTTPStatusOK HTTPStatus = 200 + iota
	HTTPStatusCreated
	HTTPStatusAccepted
	HTTPStatusNonAuthoritativeInformation
	HTTPStatusNoContent
	HTTPStatusResetContent
	HTTPStatusPartialContent
)

Available status code for 2xx

const (
	HTTPStatusMovedParmanently HTTPStatus = 301 + iota
	HTTPStatusFound
	HTTPStatusSeeOther
	HTTPStatusNotModified
	HTTPStatusUseProxy
)

Available status code for 3xx

const (
	HTTPStatusBadRequest HTTPStatus = 400 + iota
	HTTPStatusUnauthorized
	HTTPStatusPaymentRequired
	HTTPStatusForbidden
	HTTPStatusNotFound
	HTTPStatusMethodNotAllowed
	HTTPStatusNotAcceptable
	HTTPStatusProxyAuthenticationRequired
	HTTPStatusRequestTimeout
	HTTPStatusCoflict
	HTTPStatusGone
	HTTPStatusLengthRequired
	HTTPStatusPreconditionFailed
	HTTPStatusPayloadTooLarge
	HTTPStatusURITooLong
	HTTPStatusRangeNotSatisfiable
	HTTPStatusExpectationFailed
)

Available status code for 4xx

const (
	HTTPStatusInternalServerError HTTPStatus = 500 + iota
	HTTPStatusNotImplemented
	HTTPStatusBadGateway
	HTTPStatusServiceUnavailable
	HTTPStatusGatewayTimeout
)

Available status code for 5xx

func MustParseHTTPStatus

func MustParseHTTPStatus(s string) HTTPStatus

func ParseHTTPStatus

func ParseHTTPStatus(s string) (HTTPStatus, error)

func ParseHTTPStatusOr

func ParseHTTPStatusOr(s string, or HTTPStatus) HTTPStatus

func (HTTPStatus) MarshalJSON

func (i HTTPStatus) MarshalJSON() ([]byte, error)

func (HTTPStatus) String

func (i HTTPStatus) String() string

func (*HTTPStatus) UnmarshalJSON

func (i *HTTPStatus) UnmarshalJSON(b []byte) error

type Response

type Response struct {
	Status  HTTPStatus
	Header  http.Header
	Cookies []*http.Cookie
	Body    Body
	// contains filtered or unexported fields
}

Response represents http response.

func NewError

func NewError(ctx context.Context, e error) *Response

NewError returns a text response

func NewErrorWithStatus

func NewErrorWithStatus(ctx context.Context, e error, code HTTPStatus) *Response

NewErrorWithStatus returns an error response with the given status code

func NewHTML

func NewHTML(ctx context.Context, template *template.Template, data interface{}) *Response

NewHTML returns a new *HTML

func NewHTMLWithStatus

func NewHTMLWithStatus(ctx context.Context, template *template.Template, data interface{}, code HTTPStatus) *Response

NewHTMLWithStatus returns a html response generated by template and data with status code

func NewHandler

func NewHandler(ctx context.Context, h http.Handler, req *http.Request) *Response

NewHandler returns a new *Response that calls h.ServeHTTP

func NewJSON

func NewJSON(ctx context.Context, v interface{}) *Response

NewJSON returns a JSON response

func NewJSONWithStatus

func NewJSONWithStatus(ctx context.Context, v interface{}, code HTTPStatus) *Response

NewJSONWithStatus returns a JSON formatted response with the given status code

func NewRedirect

func NewRedirect(ctx context.Context, url string) *Response

NewRedirect returns *Responose for redirect

func NewRedirectWithStatus

func NewRedirectWithStatus(ctx context.Context, url string, status HTTPStatus) *Response

NewRedirectWithStatus returns *Responose for redirect

func NewResponse

func NewResponse(ctx context.Context, body Body) *Response

NewResponse retuurns a *Response to write body content

func NewResponseWithStatus

func NewResponseWithStatus(ctx context.Context, body Body, status HTTPStatus) *Response

NewResponseWithStatus retuurns a *Response to write body content with a custom status code.

func NewStaticFile

func NewStaticFile(ctx context.Context, path string, contentType string) *Response

NewStaticFile returns a static file response

func NewStaticFileWithStatus

func NewStaticFileWithStatus(ctx context.Context, path string, contentType string, code HTTPStatus) *Response

NewStaticFileWithStatus returns a text formatted response with the given status code

func NewText

func NewText(ctx context.Context, s interface{}) *Response

NewText returns a text response

func NewTextWithStatus

func NewTextWithStatus(ctx context.Context, s interface{}, code HTTPStatus) *Response

NewTextWithStatus returns a text formatted response with the given status code

func (*Response) Content

func (r *Response) Content() string

Content returns the rendered result of the response body

func (*Response) Context

func (r *Response) Context() context.Context

Context returns the context when the response is created

func (*Response) Render

func (r *Response) Render(w http.ResponseWriter)

Render renders whole http contnet

func (*Response) SetCookie

func (r *Response) SetCookie(c *http.Cookie, hmac *xhmac.Base64)

SetCookie add a cookie on the response

Directories

Path Synopsis
ia

Jump to

Keyboard shortcuts

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