Documentation ¶
Index ¶
- Constants
- Variables
- type Body
- type HTTPStatus
- type Response
- func NewError(ctx context.Context, e error) *Response
- func NewErrorWithStatus(ctx context.Context, e error, code HTTPStatus) *Response
- func NewHTML(ctx context.Context, template *template.Template, data interface{}) *Response
- func NewHTMLWithStatus(ctx context.Context, template *template.Template, data interface{}, ...) *Response
- func NewHandler(ctx context.Context, h http.Handler, req *http.Request) *Response
- func NewJSON(ctx context.Context, v interface{}) *Response
- func NewJSONWithStatus(ctx context.Context, v interface{}, code HTTPStatus) *Response
- func NewRedirect(ctx context.Context, url string) *Response
- func NewRedirectWithStatus(ctx context.Context, url string, status HTTPStatus) *Response
- func NewResponse(ctx context.Context, body Body) *Response
- func NewResponseWithStatus(ctx context.Context, body Body, status HTTPStatus) *Response
- func NewStaticFile(ctx context.Context, path string, contentType string) *Response
- func NewStaticFileWithStatus(ctx context.Context, path string, contentType string, code HTTPStatus) *Response
- func NewText(ctx context.Context, s interface{}) *Response
- func NewTextWithStatus(ctx context.Context, s interface{}, code HTTPStatus) *Response
Constants ¶
const (
ContentType string = "content-type"
)
Widely used headers
Variables ¶
var NoContent = &noContent{}
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)
var UseFormattedJSON = false
UseFormattedJSON is a configuration variable about if json object is formatted or not.
Functions ¶
This section is empty.
Types ¶
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 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 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 NewErrorWithStatus ¶
func NewErrorWithStatus(ctx context.Context, e error, code HTTPStatus) *Response
NewErrorWithStatus returns an error response with the given status code
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 ¶
NewHandler returns a new *Response that calls h.ServeHTTP
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 ¶
NewRedirect returns *Responose for redirect
func NewRedirectWithStatus ¶
func NewRedirectWithStatus(ctx context.Context, url string, status HTTPStatus) *Response
NewRedirectWithStatus returns *Responose for redirect
func NewResponse ¶
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 ¶
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 NewTextWithStatus ¶
func NewTextWithStatus(ctx context.Context, s interface{}, code HTTPStatus) *Response
NewTextWithStatus returns a text formatted response with the given status code
func (*Response) Render ¶
func (r *Response) Render(w http.ResponseWriter)
Render renders whole http contnet