httpapi

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package httpapi serves the public HTTP API, following its contract (api/openapi.yaml): the server and its timeouts, the probes, and the middleware every request goes through (request identifier, access log, panic recovery, security headers). It never serves HTML: every error is an RFC 9457 problem.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(opts Options) http.Handler

NewHandler returns the handler of the API: its routes behind the request middleware.

func NewServer

func NewServer(h http.Handler, logger *slog.Logger) *http.Server

NewServer returns a server for h with every phase of a request bounded; Serve gives it its listener. Its own errors, such as TLS handshake failures, go to logger.

func Serve

func Serve(ctx context.Context, srv *http.Server, ln net.Listener) error

Serve serves srv on ln until ctx is done, then shuts it down: it stops accepting connections and lets in-flight requests finish, for at most shutdownTimeout. It returns nil after a clean shutdown.

Types

type Authenticator added in v0.39.0

type Authenticator interface {
	// Authenticate returns the record of the key raw. A malformed,
	// unknown or revoked key is refused with domain.ErrInvalidAPIKey,
	// app.ErrKeyNotFound or app.ErrKeyRevoked; any other error means the
	// key could not be checked.
	Authenticate(ctx context.Context, raw string) (domain.KeyRecord, error)
}

Authenticator tells whether an API key may call the API.

type Checker added in v0.35.0

type Checker interface {
	// Check returns the report of in; a source it could not consult is
	// reported unavailable, never an error.
	Check(ctx context.Context, in domain.Input) domain.Report
	// CheckAll returns the report of each input, in the order of ins.
	CheckAll(ctx context.Context, ins []domain.Input) []domain.Report
}

Checker checks an input against the sources.

type Options

type Options struct {
	// Ready backs /readyz.
	Ready Readiness
	// Check backs /v1/check and /v1/check/batch.
	Check Checker
	// Auth checks the API key of /v1/check and /v1/check/batch; nil
	// answers them all with a 503.
	Auth Authenticator
	// Sources backs /v1/sources.
	Sources SourceLister
	// Logger receives the access log and the recovered panics.
	Logger *slog.Logger
	// NewID makes a request identifier when the client sent no valid one;
	// nil means crypto/rand.Text.
	NewID func() string
	// ProblemBase is the URL of the documentation of the problems, ending
	// with a slash: the type of a problem is its code under it.
	ProblemBase string
	// BatchMaxItems is the most items POST /v1/check/batch accepts, at
	// least one: config.Config.BatchMaxItems.
	BatchMaxItems int
}

Options configures the handler.

type Readiness

type Readiness interface {
	// Ready reports whether every snapshot is loaded.
	Ready() bool
}

Readiness tells whether the API can answer checks.

type SourceLister added in v0.37.0

type SourceLister interface {
	// Sources returns every source and the state of its data in service.
	Sources() []app.ListedSource
}

SourceLister lists the sources a check consults.

Directories

Path Synopsis
Package contracttest checks HTTP exchanges against the public contract, api/openapi.yaml.
Package contracttest checks HTTP exchanges against the public contract, api/openapi.yaml.

Jump to

Keyboard shortcuts

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