Documentation
¶
Overview ¶
Package responder provides a flexible and configurable way to send HTTP responses with different content types and status codes. It supports JSON, text, HTML, CSV, and XML responses, and allows customization of error message formatting and content formatting. It may be useful when writing web servers without a full-fledged web framework and avoid boilerplate code.
Index ¶
- Constants
- type DataFormatter
- type ErrorFormatter
- type ErrorResponse
- type OptionsModifier
- type Responder
- func CSVResponder(options ...OptionsModifier) Responder
- func HTMLResponder(options ...OptionsModifier) Responder
- func JSONResponder(options ...OptionsModifier) Responder
- func New(contentType string, optionsModifiers ...OptionsModifier) Responder
- func TextResponder(options ...OptionsModifier) Responder
- func XMLResponder(options ...OptionsModifier) Responder
- type Response
- type SuccessResponse
Constants ¶
const ( // TextContentType is the content type for plain text responses TextContentType = "text/plain; charset=utf-8" // CSVContentType is the content type for CSV responses CSVContentType = "text/csv; charset=utf-8" // HTMLContentType is the content type for HTML responses HTMLContentType = "text/html; charset=utf-8" // JSONContentType is the content type for JSON responses JSONContentType = "application/json; charset=utf-8" // XMLContentType is the content type for XML responses XMLContentType = "application/xml; charset=utf-8" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DataFormatter ¶ added in v0.2.0
DataFormatter defines a function type for formatting the data before sending it in the response. It receives the original data as an any type and returns the formatted data as a []byte.
type ErrorFormatter ¶
ErrorFormatter defines a function type for formatting error messages before sending them in the response. It receives the original error message as any type and returns the formatted message as an any type. The output of this function is passed to the DataFormatter. The default error formatter converts the message to a string.
type ErrorResponse ¶ added in v0.2.0
type ErrorResponse struct {
// contains filtered or unexported fields
}
ErrorResponse represents an HTTP response with status, message, and error.
func (ErrorResponse) Error ¶ added in v0.2.0
func (r ErrorResponse) Error() string
Error returns the internal error associated with the error response.
func (ErrorResponse) Status ¶ added in v0.2.0
func (r ErrorResponse) Status() int
Status returns the HTTP status code of the error response.
type OptionsModifier ¶
type OptionsModifier func(*options)
OptionsModifier defines a function type for modifying Responder options.
func WithDataFormatter ¶ added in v0.2.0
func WithDataFormatter(f DataFormatter) OptionsModifier
WithDataFormatter sets a custom data formatter
func WithErrorFormatter ¶
func WithErrorFormatter(f ErrorFormatter) OptionsModifier
WithErrorFormatter sets a custom error message formatter
func WithLogger ¶
func WithLogger(l *slog.Logger) OptionsModifier
WithLogger sets a logger for the responder
type Responder ¶
type Responder interface {
// Send200 sends a 200 OK response.
// It takes as second argument the data to be sent to the client.
Send200(responseWriter, any)
// Send201 sends a 201 Created response.
// It takes as second argument the data to be sent to the client.
Send201(responseWriter, any)
// Send202 sends a 202 Accepted response.
// It takes as second argument the data to be sent to the client.
Send202(responseWriter, any)
// Send204 sends a 204 No Content response.
Send204(responseWriter)
// Redirect301 sends a 301 Moved Permanently response to the given URL.
Redirect301(responseWriter, *http.Request, string)
// Redirect302 sends a 302 Found response to the given URL.
Redirect302(responseWriter, *http.Request, string)
// Redirect303 sends a 303 See Other response to the given URL.
Redirect303(responseWriter, *http.Request, string)
// Redirect307 sends a 307 Temporary Redirect response to the given URL.
Redirect307(responseWriter, *http.Request, string)
// Send400 sends a 400 Bad Request response. It takes as second argument
// the error that caused the bad request, and as third argument a message
// to be sent to the client.
// The error will be logged if a logger was provided.
Send400(responseWriter, error, any)
// Send401 sends a 401 Unauthorized response. It takes as second argument
// the error that caused the unauthorized response, and as third argument
// a message to be sent to the client.
// The error will be logged if a logger was provided.
Send401(responseWriter, error, any)
// Send403 sends a 403 Forbidden response. It takes as second argument
// the error that caused the forbidden response, and as third argument
// a message to be sent to the client.
// The error will be logged if a logger was provided.
Send403(responseWriter, error, any)
// Send404 sends a 404 Not Found response. It takes as second argument
// the error that caused the not found response, and as third argument
// a message to be sent to the client.
// The error will be logged if a logger was provided.
Send404(responseWriter, error, any)
// Send500 sends a 500 Internal Server Error response.
// It takes as second argument the error that caused the
// internal server error, and as third argument
// a message to be sent to the client.
// The error will be logged if a logger was provided.
Send500(responseWriter, error, any)
// Send sends a response with the given status code and body.
Send(responseWriter, Response)
}
Responder defines the interface for sending HTTP responses.
func CSVResponder ¶
func CSVResponder(options ...OptionsModifier) Responder
CSVResponder creates a new CSV responder.
func HTMLResponder ¶
func HTMLResponder(options ...OptionsModifier) Responder
HTMLResponder creates a new HTML responder.
func JSONResponder ¶
func JSONResponder(options ...OptionsModifier) Responder
JSONResponder creates a new JSON response handler. The Content-Type will be set to application/json with UTF-8 charset and the message will be formatted as a JSON error object { "error": string }.
func New ¶
func New(contentType string, optionsModifiers ...OptionsModifier) Responder
New creates a new Responder with the given content type and options.
func TextResponder ¶
func TextResponder(options ...OptionsModifier) Responder
TextResponder creates a new text responder.
func XMLResponder ¶ added in v0.1.3
func XMLResponder(options ...OptionsModifier) Responder
XMLResponder creates a new XML responder.
type Response ¶ added in v0.2.0
type Response interface {
// Status returns the HTTP status code of the response.
Status() int
}
Response represents an HTTP response with status, body, message, and error. It can be used to encapsulate both successful and error responses.
type SuccessResponse ¶ added in v0.2.0
type SuccessResponse struct {
// contains filtered or unexported fields
}
SuccessResponse represents a successful HTTP response with status, body.
func (SuccessResponse) Status ¶ added in v0.2.0
func (r SuccessResponse) Status() int
Status returns the HTTP status code of the successful response.