response

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderContentType   = "Content-Type"
	TypeApplicationJson = "application/json"
	DefaultHttpStatus   = http.StatusOK
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BodyOption

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

func (BodyOption) Apply

func (p BodyOption) Apply(response *Response)

type HeaderOption

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

func (HeaderOption) Apply

func (h HeaderOption) Apply(response *Response)

type JsonBodyOption

type JsonBodyOption struct {
	Option
	// contains filtered or unexported fields
}

func (JsonBodyOption) Apply

func (jp JsonBodyOption) Apply(response *Response)

type Option

type Option interface {
	Apply(response *Response)
}

func WithBody

func WithBody(body []byte) Option

WithBody sets the response's body.

func WithContentTypeJson

func WithContentTypeJson() Option

WithContentTypeJson tells the Response its content-Type will be "application/json"

func WithHeader

func WithHeader(name, value string) Option

WithHeader adds a header to the Response. Invoke WithHeader as many times as you have headers to set.

func WithJsonBody

func WithJsonBody(v any) Option

WithJsonBody takes an object meant to be translated to JSON inside the response's body and sets the content-type to "application/json"

func WithStatus

func WithStatus(code int) Option

type Response

type Response struct {
	Status  int
	Headers map[string]string
	Body    []byte
}

func NewJsonResponse

func NewJsonResponse(v any, opts ...Option) *Response

func NewResponse

func NewResponse(opts ...Option) *Response

NewResponse creates a Response with default attributes: Status=200 Body=empty byte slice Headers=empty hashmap

func (*Response) Write

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

Write sends the Response's content to the http.ResponseWriter.

type StatusOption

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

func (StatusOption) Apply

func (s StatusOption) Apply(response *Response)

Jump to

Keyboard shortcuts

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