Documentation
¶
Index ¶
- Variables
- func HandleEmptyRequest[T HTTPResponse](endpoint func(r *http.Request, log *slog.Logger) (T, Problem)) http.Handler
- func HandleJSONRequest[S any, T HTTPResponse](endpoint func(r *http.Request, body S, log *slog.Logger) (T, Problem)) http.Handler
- func ListenAndServeUntilSignal(server *http.Server) error
- func LogProblem(ctx context.Context, log *slog.Logger, p Problem)
- type BasicProblem
- func (p *BasicProblem) Error() string
- func (p *BasicProblem) ErrorData() map[string]any
- func (p *BasicProblem) ProblemData() map[string]any
- func (p *BasicProblem) ProblemDetail() string
- func (p *BasicProblem) ProblemInstance() string
- func (p *BasicProblem) ProblemType() ProblemType
- func (p *BasicProblem) SetProblemHeaders(h http.Header)
- func (p *BasicProblem) Unwrap() error
- type EmptyBodyHandler
- type HTTPResponse
- type HeadersOnly
- type JSONBody
- type JSONBodyHandler
- type NoContent
- type Problem
- func GetJSONBody[T any](r *http.Request) (T, Problem)
- func InstancedProblemf(ptype ProblemType, instance string, format string, args ...any) Problem
- func ProblemOrFallback(err error, fallbackType ProblemType, fallbackInstance string, ...) Problem
- func Problemf(ptype ProblemType, format string, args ...any) Problem
- func StatusProblemf(status int, format string, args ...any) Problem
- type ProblemJSON
- type ProblemType
- type UnsupportedMediaTypeError
- func (e *UnsupportedMediaTypeError) Error() string
- func (e *UnsupportedMediaTypeError) ErrorData() map[string]any
- func (e *UnsupportedMediaTypeError) ProblemData() map[string]any
- func (e *UnsupportedMediaTypeError) ProblemDetail() string
- func (e *UnsupportedMediaTypeError) ProblemInstance() string
- func (e *UnsupportedMediaTypeError) ProblemType() ProblemType
- func (e *UnsupportedMediaTypeError) SetProblemHeaders(headers_out http.Header)
Constants ¶
This section is empty.
Variables ¶
var AllowBlankContentType = false
Incorrect Content-Type will always be rejected (required to prevent XSRF which can send JSON as text/plain bypassing CORS), but the header can be made optional.
var JSONOptions json.Options = json.JoinOptions( jsontext.WithIndent("\t"), jsontext.Multiline(true), jsontext.CanonicalizeRawInts(false), json.FormatNilSliceAsNull(false), json.FormatNilMapAsNull(false), )
Options to use when processing and emitting JSON bodies
Functions ¶
func HandleEmptyRequest ¶
func HandleJSONRequest ¶
Types ¶
type BasicProblem ¶
type BasicProblem struct {
PType ProblemType
Instance string
Detail string
InternalMessage string
WrappedError error
}
func (*BasicProblem) Error ¶
func (p *BasicProblem) Error() string
func (*BasicProblem) ErrorData ¶
func (p *BasicProblem) ErrorData() map[string]any
func (*BasicProblem) ProblemData ¶
func (p *BasicProblem) ProblemData() map[string]any
func (*BasicProblem) ProblemDetail ¶
func (p *BasicProblem) ProblemDetail() string
func (*BasicProblem) ProblemInstance ¶
func (p *BasicProblem) ProblemInstance() string
func (*BasicProblem) ProblemType ¶
func (p *BasicProblem) ProblemType() ProblemType
func (*BasicProblem) SetProblemHeaders ¶
func (p *BasicProblem) SetProblemHeaders(h http.Header)
func (*BasicProblem) Unwrap ¶
func (p *BasicProblem) Unwrap() error
type EmptyBodyHandler ¶
type EmptyBodyHandler[T HTTPResponse] struct { Name string EndpointFunc func(r *http.Request, log *slog.Logger) (T, Problem) }
func (*EmptyBodyHandler[T]) ServeHTTP ¶
func (h *EmptyBodyHandler[T]) ServeHTTP(w http.ResponseWriter, r *http.Request)
type HTTPResponse ¶
type HTTPResponse interface {
WriteHTTPResponse(w http.ResponseWriter) error // the error should be ignorable if you do not care about dropped connections
}
type HeadersOnly ¶
func (*HeadersOnly) WriteHTTPResponse ¶
func (r *HeadersOnly) WriteHTTPResponse(w http.ResponseWriter) error
nil is a valid value here, as NoContent has no content
type JSONBody ¶
func AsJSONBody ¶
func (*JSONBody[T]) WriteHTTPResponse ¶
func (r *JSONBody[T]) WriteHTTPResponse(w http.ResponseWriter) error
type JSONBodyHandler ¶
type JSONBodyHandler[S any, T HTTPResponse] struct { Name string EndpointFunc func(r *http.Request, body S, log *slog.Logger) (T, Problem) }
func (*JSONBodyHandler[S, T]) ServeHTTP ¶
func (h *JSONBodyHandler[S, T]) ServeHTTP(w http.ResponseWriter, r *http.Request)
type NoContent ¶
type NoContent struct{}
func (*NoContent) WriteHTTPResponse ¶
func (r *NoContent) WriteHTTPResponse(w http.ResponseWriter) error
nil is a valid value here, as NoContent has no content
type Problem ¶
type Problem interface {
error // allows this to be returned as an error as well as providing a log message through the Error() method
ProblemType() ProblemType // type of error including the status code
ProblemInstance() string // Optional URI pointing to a persistent record of this occurrence of the problem.
ProblemDetail() string // Human-readable error message to be returned to the client
SetProblemHeaders(headers_out http.Header) // set any headers required for the response
ProblemData() map[string]any // additional fields to be sent to the response, MUST be json/v2 serializable
ErrorData() map[string]any // additional data to be included in log
}
Am error message that can be returned from an HTTP API. Follows RFC 9457 structure.
func InstancedProblemf ¶
func InstancedProblemf(ptype ProblemType, instance string, format string, args ...any) Problem
func ProblemOrFallback ¶
func ProblemOrFallback(err error, fallbackType ProblemType, fallbackInstance string, fallbackDetail string) Problem
type ProblemJSON ¶
type ProblemJSON struct {
Problem
}
func (*ProblemJSON) RawBody ¶
func (p *ProblemJSON) RawBody() []byte
func (*ProblemJSON) WriteHTTPResponse ¶
func (p *ProblemJSON) WriteHTTPResponse(w http.ResponseWriter) error
Writes a Problem as an HTTP response. Returns the rrror from the Write call itself, which can be ignored if you don't care about dropped sockets. Panics if ProblemData returns fields with non-serializable types.
type ProblemType ¶
type ProblemType struct {
Title string `json:"title"`
Status int `json:"status"`
URI string `json:"type,omitempty"`
LogLevel slog.Level `json:"-"`
}
Error type from RFC 9457. This extends an HTTP status code with an optional URI for further specificity. Title is descriptive text but should be consistent for a given problem type.
func ProblemStatus ¶
func ProblemStatus(status int) ProblemType
ProbemType for an HTTP status code with no further specificity.
type UnsupportedMediaTypeError ¶
func (*UnsupportedMediaTypeError) Error ¶
func (e *UnsupportedMediaTypeError) Error() string
func (*UnsupportedMediaTypeError) ErrorData ¶
func (e *UnsupportedMediaTypeError) ErrorData() map[string]any
func (*UnsupportedMediaTypeError) ProblemData ¶
func (e *UnsupportedMediaTypeError) ProblemData() map[string]any
func (*UnsupportedMediaTypeError) ProblemDetail ¶
func (e *UnsupportedMediaTypeError) ProblemDetail() string
func (*UnsupportedMediaTypeError) ProblemInstance ¶
func (e *UnsupportedMediaTypeError) ProblemInstance() string
func (*UnsupportedMediaTypeError) ProblemType ¶
func (e *UnsupportedMediaTypeError) ProblemType() ProblemType
func (*UnsupportedMediaTypeError) SetProblemHeaders ¶
func (e *UnsupportedMediaTypeError) SetProblemHeaders(headers_out http.Header)