httpapi

package
v0.70.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package httpapi is ContentKit's HTTP surface as data. Every route of every module is declared once, as a Route in the module's table: method, path, auth tier, query, bodies and error codes. The module mounts its handlers from that table (Mount), and internal/contract renders the same tables as api/openapi.json, the browser SDK's generated types and docs/api/routes.md.

Index

Constants

View Source
const (
	CodeInvalidRequest = "invalid_request"
	CodeUnauthorized   = "unauthorized"
	CodeForbidden      = "forbidden"
	CodeNotFound       = "not_found"
	CodeConflict       = "conflict"
	CodeRateLimited    = "rate_limited"
	CodeUnavailable    = "unavailable"
	CodeUpgrade        = "upgrade_required"
	CodeInternal       = "internal_error"
	CodeTenantMismatch = "tenant_mismatch"
)

The codes more than one module answers.

View Source
const (
	GET, POST, PUT, PATCH, DELETE = http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete
)

Variables

Modules lists every module in catalog order.

View Source
var NoContent = Reply{Status: http.StatusNoContent}

NoContent is a 204.

View Source
var Page = []Param{Int("limit", "page size: 20 by default, at most 100"), Int("offset", "items to skip")}

Page is the limit/offset query of a list.

Functions

func ErrorSet

func ErrorSet(name string) []string

ErrorSet lists the codes of a set ErrorSets names.

func H

func H[S any](fn func(S, http.ResponseWriter, *http.Request)) func(S) http.HandlerFunc

H adapts a handler method expression, such as (*posts).handleList, to Serve.

func Mount

func Mount[S any](mux *http.ServeMux, s S, routes []Route[S])

Mount registers routes on mux, served from s.

func Register

func Register[S any](m Module, routes []Route[S])

Register adds a module's routes to the catalog. Modules call it from init, once per table, in the order their routes are documented.

func Reserved

func Reserved() []string

Reserved is the first path segments the non-content modules take: a content kind with one of these names would be unreachable under the one mount.

Types

type ErrorCode

type ErrorCode struct {
	Code    string
	Status  int
	Meaning string
}

ErrorCode is one stable error code: every module answers the flat body {"error", "code", …}, where code is one of these with its status. Clients branch on the code; the message is for people and may change.

func ErrorCodes

func ErrorCodes() []ErrorCode

ErrorCodes is every registered code, sorted.

func LookupErrorCode

func LookupErrorCode(code string) (ErrorCode, bool)

LookupErrorCode returns a registered code.

type Module

type Module string

Module is one mountable handler. contentkit.Runtime.Handler serves every configured module under one prefix, each at its fixed sub-path (Prefix).

const (
	// Content is posts, comments, reactions, favorites, polls, comment bans
	// and moderation (content.Runtime.Handler).
	Content Module = "content"
	// Upload is the media upload API (media.UploadHandler).
	Upload Module = "upload"
	// Media is the media read API and playlists (media.Reader.Handler).
	Media Module = "media"
	// Codes resolves content codes (contenturl.Router.Handler).
	Codes Module = "codes"
	// Taxonomy is the taxonomy admin API (taxonomy.Handler).
	Taxonomy Module = "taxonomy"
)

func (Module) Prefix

func (m Module) Prefix() string

Prefix is the module's sub-path under the one mount: content at its root, the others beneath their own first segment, which no content kind may take.

type Param

type Param struct {
	Name string
	// Kind is string, integer, number, boolean, flag (present means on),
	// strings (repeated) or list (comma-separated).
	Kind string
	Doc  string
}

Param is one query parameter.

func Bool

func Bool(name, doc string) Param

func Flag

func Flag(name, doc string) Param

func Int

func Int(name, doc string) Param

func List

func List(name, doc string) Param

func Number

func Number(name, doc string) Param

func Repeated

func Repeated(name, doc string) Param

func Text

func Text(name, doc string) Param

type Reply

type Reply struct {
	Status int
	Body   any
}

Reply is one success outcome: its status and a zero value of its body's type (nil for none).

func Accepted

func Accepted(body any) Reply

func Created

func Created(body any) Reply

func OK

func OK(body any) Reply

type Route

type Route[S any] struct {
	Spec
	Serve func(S) http.HandlerFunc
}

Route is a Spec with the handler that serves it, built from the state S the module mounts with.

type Spec

type Spec struct {
	Method string
	// Path is the route from its module's mount, in ServeMux syntax.
	Path string
	// Resource is the route's section in the docs and its OpenAPI tag.
	Resource string
	// Doc says what the route does, in one line.
	Doc  string
	Auth Tier
	// Perm names the content.Perms field a Staff route checks.
	Perm string
	// Query lists the query parameters; Request is a zero value of the JSON
	// body's type (nil for none); Responses is every success outcome.
	Query     []Param
	Request   any
	Responses []Reply
	// Errors lists the route's own codes; ErrorSets names the shared ones.
	Errors []string
	// Module is set by Register.
	Module Module
}

Spec is a route without its handler: what the contract is generated from.

func Catalog

func Catalog() []Spec

Catalog is every registered route, by module (Modules order), each module in declaration order.

func Match

func Match(method, path string) (Spec, bool)

Match finds the catalog route serving a method and a path under the one mount, preferring literal segments as ServeMux does.

func (Spec) AllErrors

func (s Spec) AllErrors() []string

AllErrors is every code the route can answer, sorted.

func (Spec) ErrorSets

func (s Spec) ErrorSets() []string

ErrorSets names the shared code sets a route answers besides its own Errors.

func (Spec) FullPath

func (s Spec) FullPath() string

FullPath is the route's path under the one mount.

func (Spec) Key

func (s Spec) Key() string

Key is the route's identity within its module: "GET /posts/{id}".

type Stream

type Stream struct{ ContentType string }

Stream is a body that is not JSON: an image, a playlist.

type Tier

type Tier string

Tier is what a route requires of its caller before its handler acts.

const (
	// Public: the actor is optional; the host's resolver decides what it sees.
	Public Tier = "public"
	// User: a signed-in actor; 401 unauthorized without one.
	User Tier = "user"
	// Staff: the host permission named by Perm; 403 forbidden without it.
	Staff Tier = "staff"
)

Jump to

Keyboard shortcuts

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