messages

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package messages provides a declarative and composable structure for defining HTTP responses in Go.

It introduces the Response type, which encapsulates HTTP status, headers, and a response body writer. This allows handler functions to focus on returning data rather than writing directly to http.ResponseWriter.

The package also includes the Responder type, which adapts declarative handler functions (ResponseHandlerFunc) into standard http.HandlerFunc values, with built-in error handling, centralized logging, and optional buffered output to ensure atomic HTTP responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Responder

type Responder struct {
	// Logger is used to log internal errors encountered
	// while writing responses. If nil, errors will logged
	// using slog.Default().
	Logger *slog.Logger

	// Buffer indicates whether a response body should be
	// buffered internally before sending it to the client.
	// If true, responses are only sent if writing succeeds,
	// ensuring atomicity. If false, the response body
	// streams directly.
	Buffer bool
}

Responder provides a centralized mechanism for sending Response values. It encapsulates configuration for response buffering and structured error logging.

Use Responder.HandlerFunc to convert a ResponseHandlerFunc into an http.HandlerFunc.

func (Responder) HandlerFunc

func (re Responder) HandlerFunc(fn ResponseHandlerFunc) http.HandlerFunc

HandlerFunc wraps a ResponseHandlerFunc using re's configuration, returning a standard http.HandlerFunc.

It handles writing status, headers, and body. If buffering is enabled, the body is written to an internal buffer and flushed only if writing succeeds, preventing partial writes.

If the handler returns a Response with an invalid Status, a 500 Internal Server Error is sent and logged. Errors during body writing or buffering are also logged but HTTP errors are only sent where possible - i.e. where Status has not been committed.

type Response

type Response struct {
	Status    int
	Headers   http.Header
	WriteBody func(io.Writer) error
}

Response defines a structured HTTP response message.

It includes a status code, headers, and an optional WriteBody function, which writes the response body to an io.Writer. If WriteBody is nil, only the headers and status are sent.

This type is returned from a ResponseHandlerFunc and processed by a Responder.

type ResponseHandlerFunc

type ResponseHandlerFunc func(*http.Request) Response

ResponseHandlerFunc defines a declarative HTTP handler function.

It accepts an *http.Request and returns a structured Response. Use Responder.HandlerFunc to adapt this function into a standard http.HandlerFunc.

Jump to

Keyboard shortcuts

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