rest

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package rest is a tiny typed-handler layer over net/http.

A HandlerFunc returns an Encoder — a value that knows how to serialize itself — instead of writing to the ResponseWriter directly. This keeps response encoding and error handling in one place (Respond), makes handlers trivial to unit-test (assert on the returned value, not a recorded response), and lets a handler signal failure by returning an *errs.Error, which Respond maps to the right HTTP status. Mount these handlers with the router package.

Encoders and status

An Encoder reports its body and content type; if it also implements an HTTPStatus() int method it sets the response status (otherwise 200, or 204 for a nil Encoder). JSON and JSONStatus wrap any value as a JSON Encoder, and *errs.Error is itself an Encoder, so a handler returns one uniform type for both success and failure.

Usage

func getWidget(ctx context.Context, r *http.Request) rest.Encoder {
	var in CreateWidget
	if err := rest.Decode(r, &in); err != nil {
		return err.(*errs.Error) // InvalidArgument -> 400
	}
	w, err := store.Find(ctx, in.ID)
	if err != nil {
		return errs.New(errs.NotFound, err) // -> 404
	}
	return rest.JSON(w) // -> 200 application/json
}

// Compose cross-cutting behavior, outermost first:
h := rest.ChainMiddleware(getWidget, authMid, logMid)
resp := h(ctx, r)
_ = rest.Respond(ctx, w, resp)

API

  • HandlerFunc: func(ctx, *http.Request) Encoder — the typed handler shape.
  • MidFunc / ChainMiddleware: wrap a HandlerFunc; mw[0] is the outermost layer.
  • Respond: write an Encoder to an http.ResponseWriter (nil -> 204; encode failure -> 500 with a generic body; cancelled ctx -> returns ctx.Err).
  • JSON / JSONStatus: wrap a value as a JSON Encoder (status 200, or explicit).
  • Decode: read and validate a JSON request body (DisallowUnknownFields + errs.Check), returning an *errs.Error (InvalidArgument) on bad input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, v any) error

Decode reads a JSON request body into v and validates it with errs.Check. It returns an *errs.Error (InvalidArgument) on malformed input.

func Respond

func Respond(ctx context.Context, w http.ResponseWriter, resp Encoder) error

Respond writes resp to w. A nil Encoder yields 204 No Content. If encoding fails, a 500 with a generic error body is written instead.

Types

type Encoder

type Encoder interface {
	Encode() (data []byte, contentType string, err error)
}

Encoder is a value that can serialize itself into a response body.

func JSON

func JSON(v any) Encoder

JSON wraps v as a JSON Encoder with status 200.

func JSONStatus

func JSONStatus(v any, status int) Encoder

JSONStatus wraps v as a JSON Encoder with an explicit status.

type HandlerFunc

type HandlerFunc func(ctx context.Context, r *http.Request) Encoder

HandlerFunc handles a request and returns an Encoder describing the response. Returning a non-nil error value (e.g. *errs.Error) is the idiomatic way to signal failure — Respond will set the right status.

func ChainMiddleware

func ChainMiddleware(h HandlerFunc, mw ...MidFunc) HandlerFunc

ChainMiddleware wraps h with mw in order, so mw[0] is the outermost layer.

type MidFunc

type MidFunc func(HandlerFunc) HandlerFunc

MidFunc wraps a HandlerFunc to add cross-cutting behavior.

Jump to

Keyboard shortcuts

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