response

package
v0.85.0-pre.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ClientRedirectHeader        = "X-Client-Redirect"
	ClientAcceptsRedirectHeader = "X-Accepts-Client-Redirect"
)

Variables

This section is empty.

Functions

func GetClientRedirectURL

func GetClientRedirectURL(w http.ResponseWriter) string

Types

type Proxy

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

For usage in JSON API handlers that may run in parallel or do not have direct access to the http.ResponseWriter. Proxy instances are not meant to be shared. Rather, they should exist inside a single function/handler scope, and afterwards should be used by a parent scope to actually de-duplicate, determine priority, and write to the real http.ResponseWriter.

Concurrency model: Proxy instances are NOT thread-safe and must not be shared across goroutines. The intended usage pattern is:

  • Create one Proxy per goroutine/task
  • Each goroutine writes only to its own Proxy
  • After all goroutines complete, merge proxies on a single goroutine using MergeProxyResponses
  • Apply the merged result to the ResponseWriter

Do not instantiate directly. Use NewProxy().

func MergeProxyResponses

func MergeProxyResponses(proxies ...*Proxy) *Proxy

Consumers should deduplicate head els after calling MergeProxyResponses by using headels.ToHeadEls(proxy.GetHeadElements())

func NewProxy

func NewProxy() *Proxy

func (*Proxy) AddHeadEls added in v0.83.0

func (p *Proxy) AddHeadEls(els *headels.HeadEls)

func (*Proxy) AddHeader

func (p *Proxy) AddHeader(key, value string)

func (*Proxy) ApplyToResponseWriter

func (p *Proxy) ApplyToResponseWriter(w http.ResponseWriter, r *http.Request)

func (*Proxy) GetCookies

func (p *Proxy) GetCookies() []*http.Cookie

func (*Proxy) GetHeadEls added in v0.83.0

func (p *Proxy) GetHeadEls() *headels.HeadEls

func (*Proxy) GetHeader

func (p *Proxy) GetHeader(key string) string

func (*Proxy) GetHeaders

func (p *Proxy) GetHeaders(key string) []string

func (*Proxy) GetLocation

func (p *Proxy) GetLocation() string

func (*Proxy) GetStatus

func (p *Proxy) GetStatus() (int, string)

func (*Proxy) IsError

func (p *Proxy) IsError() bool

func (*Proxy) IsRedirect

func (p *Proxy) IsRedirect() bool

func (*Proxy) IsSuccess

func (p *Proxy) IsSuccess() bool

func (*Proxy) Redirect

func (p *Proxy) Redirect(r *http.Request, url string, code ...int) (bool, error)

Redirect sets a redirect on the proxy. If the request accepts client redirects, a client redirect is set; otherwise, a server redirect is set. Returns whether a client redirect was used and any error from URL validation.

func (*Proxy) SetCookie

func (p *Proxy) SetCookie(cookie *http.Cookie)

func (*Proxy) SetHeader

func (p *Proxy) SetHeader(key, value string)

func (*Proxy) SetStatus

func (p *Proxy) SetStatus(status int, errorStatusText ...string)

type Response

type Response struct {
	Writer http.ResponseWriter
	// contains filtered or unexported fields
}

func New

func (*Response) AddHeader

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

func (*Response) BadRequest

func (res *Response) BadRequest(reasons ...string)

func (*Response) ClientRedirect

func (res *Response) ClientRedirect(url string) error

ClientRedirect sets a client-side redirect header and status 200. Returns an error if the response is already committed.

func (*Response) Error

func (res *Response) Error(status int, reasons ...string)

func (*Response) Forbidden

func (res *Response) Forbidden(reasons ...string)

func (*Response) HTML

func (res *Response) HTML(html string)

func (*Response) HTMLBytes

func (res *Response) HTMLBytes(bytes []byte)

func (*Response) InternalServerError

func (res *Response) InternalServerError(reasons ...string)

func (*Response) IsCommitted

func (res *Response) IsCommitted() bool

func (*Response) JSON

func (res *Response) JSON(v any)

Pass in non-encoded JSON-marshallable data

func (*Response) JSONBytes

func (res *Response) JSONBytes(bytes []byte)

Pass in pre-encoded JSON bytes

func (*Response) MethodNotAllowed

func (res *Response) MethodNotAllowed(reasons ...string)

func (*Response) NotFound

func (res *Response) NotFound()

func (*Response) NotModified

func (res *Response) NotModified()

func (*Response) OK

func (res *Response) OK()

Returns a 200 JSON response of {"ok":true}

func (*Response) OKText

func (res *Response) OKText()

Returns a 200 text response of "OK"

func (*Response) Redirect

func (res *Response) Redirect(r *http.Request, url string, code ...int) (usedClientRedirect bool, err error)

func (*Response) ServerRedirect

func (res *Response) ServerRedirect(r *http.Request, url string, code ...int)

func (*Response) SetHeader

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

func (*Response) SetStatus

func (res *Response) SetStatus(status int)

func (*Response) Text

func (res *Response) Text(text string)

func (*Response) TooManyRequests

func (res *Response) TooManyRequests(reasons ...string)

func (*Response) Unauthorized

func (res *Response) Unauthorized(reasons ...string)

Jump to

Keyboard shortcuts

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