Documentation
¶
Index ¶
- Constants
- func Attachment(w http.ResponseWriter, r io.Reader, code int, params map[string]string) error
- func Empty(w http.ResponseWriter, code int) error
- func Error(w http.ResponseWriter, err error, detail ...any) error
- func JSON(w http.ResponseWriter, code, indent int, v any) error
- func Write(w http.ResponseWriter, code int, contentType string, ...) error
- type Err
- type ErrResponse
- type JSONStream
- type TextStream
Constants ¶
const ( ErrBadRequest = Err(http.StatusBadRequest) ErrNotFound = Err(http.StatusNotFound) ErrConflict = Err(http.StatusConflict) ErrNotImplemented = Err(http.StatusNotImplemented) ErrInternalError = Err(http.StatusInternalServerError) ErrNotAuthorized = Err(http.StatusUnauthorized) ErrForbidden = Err(http.StatusForbidden) ErrGatewayError = Err(http.StatusBadGateway) )
Variables ¶
This section is empty.
Functions ¶
func Attachment ¶
Attachment will write out an attachment from a io.Reader
func Empty ¶
func Empty(w http.ResponseWriter, code int) error
Empty is a utility function to serve an empty response
func Error ¶
func Error(w http.ResponseWriter, err error, detail ...any) error
Error writes an error from a HTTP status code, with additional detail
func JSON ¶
func JSON(w http.ResponseWriter, code, indent int, v any) error
JSON will write a JSON response to the writer with a HTTP status code, optionally indenting the JSON by a number of spaces
func Write ¶
func Write(w http.ResponseWriter, code int, contentType string, fn func(w io.Writer) (int, error)) error
Write a custom response to the writer with a HTTP status code. If the custom function is set and returns the number of bytes written (greater than zero), it will be used to write the response body.
Types ¶
type ErrResponse ¶ added in v1.6.14
type ErrResponse struct {
Code int `json:"code" jsonschema:"HTTP status code"`
Reason string `json:"reason,omitempty" jsonschema:"Human-readable reason phrase"`
Detail any `json:"detail,omitempty" jsonschema:"Optional additional detail"`
}
ErrResponse is the JSON error body returned by all error responses. It is also used to generate the OpenAPI response schema.
func (ErrResponse) Error ¶ added in v1.6.22
func (err ErrResponse) Error() string
type JSONStream ¶ added in v1.6.23
type JSONStream struct {
// contains filtered or unexported fields
}
JSONStream implements a full-duplex newline-delimited JSON stream.
Recv reads exactly one JSON value per line from the request body and Send writes one JSON value to the response body followed by a newline and an immediate flush.
func NewJSONStream ¶ added in v1.6.23
func NewJSONStream(w http.ResponseWriter, r *http.Request, headers ...string) (*JSONStream, error)
Create a new full-duplex JSON stream with mimetype application/ndjson. Additional header tuples can be provided as a series of key-value pairs.
func (*JSONStream) Close ¶ added in v1.6.23
func (s *JSONStream) Close() error
Close closes the request body and marks the stream as closed.
func (*JSONStream) Context ¶ added in v1.6.23
func (s *JSONStream) Context() context.Context
Context returns the request context associated with the stream.
func (*JSONStream) Recv ¶ added in v1.6.23
func (s *JSONStream) Recv() (json.RawMessage, error)
Recv returns the next newline-delimited JSON frame from the request body. A blank line is treated as a keep-alive heartbeat and returns nil, nil.
func (*JSONStream) Send ¶ added in v1.6.23
func (s *JSONStream) Send(frame json.RawMessage) error
Send writes one JSON frame to the response body and flushes it immediately.
type TextStream ¶
type TextStream struct {
// contains filtered or unexported fields
}
TextStream implements a stream of text events
func NewTextStream ¶
func NewTextStream(w http.ResponseWriter, tuples ...string) *TextStream
Create a new text stream with mimetype text/event-stream Additional header tuples can be provided as a series of key-value pairs
func (*TextStream) Close ¶
func (s *TextStream) Close() error
Close the text stream to stop sending ping messages
func (*TextStream) Write ¶
func (s *TextStream) Write(name string, data ...any)
Write a text event to the stream, and one or more optional data objects which are encoded as JSON