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 NewJSONStreamHandler(fn JSONStreamHandlerFunc, headers ...string) http.Handler
- func Write(w http.ResponseWriter, code int, contentType string, ...) error
- type Err
- type ErrResponse
- type JSONStream
- type JSONStreamConn
- type JSONStreamHandlerFunc
- 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 NewJSONStreamHandler ¶ added in v1.6.24
func NewJSONStreamHandler(fn JSONStreamHandlerFunc, headers ...string) http.Handler
NewJSONStreamHandler wraps a channel-based handler function in an HTTP handler. The receive channel yields compacted JSON frames from the request body and nil for heartbeat lines. Sending nil on the write channel emits a blank heartbeat line.
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 {
Type string `json:"object" jsonschema:"The type of the response, always 'error'"`
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 interface {
Recv() <-chan json.RawMessage
Send(json.RawMessage) error
}
JSONStream mirrors the client-side streaming interface: Recv yields newline- delimited JSON frames from the request body, and Send writes one JSON frame to the response body followed by a newline and an immediate flush.
type JSONStreamConn ¶ added in v1.6.24
type JSONStreamConn interface {
JSONStream
Context() context.Context
Close() error
}
JSONStreamConn is the richer stream connection returned by NewJSONStream. It adds access to the request context and explicit close semantics.
func NewJSONStream ¶ added in v1.6.23
func NewJSONStream(w http.ResponseWriter, r *http.Request, headers ...string) (JSONStreamConn, 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.
type JSONStreamHandlerFunc ¶ added in v1.6.24
type JSONStreamHandlerFunc func(req *http.Request, stream JSONStream) error
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