api

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package api provides shared plumbing so new endpoints get consistent behavior (problem+json errors, panic recovery, JSON helpers) for free.

Index

Constants

View Source
const (
	// Prefix is where every application endpoint lives.
	Prefix = "/api/"
	// RouxPrefix is reserved for endpoints the kit itself provides (health, ...).
	// Applications cannot register routes here.
	RouxPrefix = "/api/roux/"
)
View Source
const StatusClientClosedRequest = 499

StatusClientClosedRequest is the status of a request whose client went away before the answer (nginx's 499): below 500, so it marks no trace as failed.

Variables

This section is empty.

Functions

func Decode

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

Decode reads a single JSON object from the request body into v, rejecting unknown fields and trailing data with a 400 problem.

func DecodeLenient

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

DecodeLenient is Decode for roux's own endpoints, which ignore members they do not know, so an older or newer client may send more than is read.

func JSON

func JSON(w http.ResponseWriter, status int, v any) error

JSON writes v as a JSON response with the given status.

func MapType

func MapType[T error](m *Mux, fn func(T) *problem.Problem)

MapType presents errors of type T (found via errors.As anywhere in the chain) as the problem returned by fn. Use it for typed errors you don't own, e.g. library errors. Return nil from fn to decline and fall through to earlier mappings. (A function, not a method: Go methods can't be generic.)

func PageRequest

func PageRequest(r *http.Request, cfg page.Config) (page.Request, error)

PageRequest parses the standard pagination query parameters (pageIndex, pageSize, sortBy, sortDirection) into a resolved page.Request. Out-of-range numbers are clamped (see page.Config.Resolve); malformed values and unknown sortBy fields return roux's 400 problem with per-field errors.

func PageRequestUnsorted

func PageRequestUnsorted(r *http.Request) (page.Request, error)

PageRequestUnsorted is PageRequest for a list whose order is fixed (a history, newest first): pageIndex and pageSize are read and clamped, and any sortBy or sortDirection is ignored, not refused.

func Recoverer

func Recoverer(next http.Handler) http.Handler

Recoverer converts panics into 500 problem responses.

Types

type ErrorRenderer

type ErrorRenderer func(w http.ResponseWriter, r *http.Request, p *problem.Problem)

ErrorRenderer writes a problem as the response. problem.Write is the default; a kit package that must answer in another format under its own base path (sign-in's urn:roux:error problems, say) supplies its own via Mux.Kit.

type HandlerFunc

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

HandlerFunc is like http.HandlerFunc but returns an error. Returned errors are rendered as RFC 9457 problem details by the Mux it is registered on.

type Kit

type Kit struct {
	// contains filtered or unexported fields
}

Kit registers one kit package's endpoints under its reserved base path.

func (*Kit) Handle

func (k *Kit) Handle(pattern string, h HandlerFunc)

Handle registers "METHOD /path", with the path relative to the kit's base: on a kit at /api/roux/auth, Handle("GET /me", h) serves GET /api/roux/auth/me.

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux wraps http.ServeMux so unmatched routes (404) and wrong methods (405) also produce problem details instead of plain text.

func NewMux

func NewMux() *Mux

NewMux returns an empty Mux. A handler answers a refusal by returning a *problem.Problem (problem.NotFound, problem.Conflict, ...), wrapped or not.

func (*Mux) Fallback

func (m *Mux) Fallback(h http.Handler)

Fallback sets the handler for every request outside /api/ and outside the kit packages' base paths (see Kit) that no route matched: the front end (see kit/spa). Those reserved paths never reach it; an unknown one is always a JSON 404 problem, never a web page. May be set once, before serving.

func (*Mux) Handle

func (m *Mux) Handle(pattern string, h HandlerFunc)

Handle registers an application endpoint as it is: no sign-in, no permission, no CSRF check. An application route almost always wants those, so mount it with app.Kit.Handle (or HandleSignedIn, HandlePublic) instead, which guards it; a route mounted here is named in a warning at startup (see Unguarded). The pattern must be "METHOD /api/..." (Go 1.22 syntax, e.g. "GET /api/items/{id}") and must not be under /api/roux/, which is reserved for the kit. Violations panic at startup.

func (*Mux) HandleGuarded

func (m *Mux) HandleGuarded(pattern string, h HandlerFunc)

HandleGuarded is Handle for a handler that guards itself (app.Kit.Handle wraps one in authn.Host.Guard and mounts it here): it is left out of Unguarded.

func (*Mux) HandleRoux

func (m *Mux) HandleRoux(pattern string, h HandlerFunc)

HandleRoux registers an endpoint roux itself provides. The pattern names the whole path, which must be under /api/roux/: HandleRoux("GET /api/roux/healthz", h). Only roux's packages call this; applications use app.Kit.Handle.

func (*Mux) Kit

func (m *Mux) Kit(prefix string, render ErrorRenderer) *Kit

Kit reserves a base path for one of roux's packages and returns its registrar. Routes registered through it live under prefix, and every error there (including the router's own 404 and 405) is written by render. The front-end fallback never serves paths under a kit base. Reserving is for roux's packages (sign-in reserves /api/roux/auth, for instance); applications use Handle. Panics at startup if prefix is not an absolute path without a trailing slash, is under /api/ but not under /api/roux/, or overlaps a base already reserved (other than /api/roux itself, which the more specific base takes over beneath it).

func (*Mux) MapError

func (m *Mux) MapError(target error, toProb func(err error) *problem.Problem)

MapError presents errors matching target (via errors.Is) as the problem returned by toProb. Use it for sentinel errors. Register before serving.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Mux) Unguarded

func (m *Mux) Unguarded() []string

Unguarded is every route mounted with Handle, in order: those no guard is known to wrap.

type ProblemSource

type ProblemSource interface {
	Problem() *problem.Problem
}

ProblemSource can be implemented by any error type that knows how it should be presented over HTTP. It keeps domain packages free of HTTP concerns: they only need to satisfy this one-method interface (no import required).

Jump to

Keyboard shortcuts

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