Help us shape the future of Go! Take the 2026 Go Developer Survey

httpapi

package
v1.4.14 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenAPI

func OpenAPI(reg *cmdregistry.Registry, dataSchema func(string) ([]byte, bool)) ([]byte, error)

OpenAPI describes document commands; streaming remains a transport contract.

func ValidateAllowedHosts

func ValidateAllowedHosts(hosts []string) error

ValidateAllowedHosts accepts DNS names, IPv4 literals and bracketed IPv6 literals. An empty list adds no hosts; an invalid entry returns a user error naming that entry. It does not resolve DNS or accept ports or URL components.

Types

type AuthHook

type AuthHook interface {
	// Authenticate returns the caller identity or an error without consuming the body.
	Authenticate(*http.Request) (Principal, error)
}

AuthHook authenticates every route before its body is read. An error refuses the request with E426; a successful principal does not bypass invocation gates.

func BearerAuth

func BearerAuth(tok string) AuthHook

BearerAuth checks a case-sensitive Bearer token using constant-time comparison of SHA-256 digests. An empty configured token rejects every request.

type Invoker

Invoker executes a registry command with the request context and server guard. Implementations must honor cancellation and the shared invocation contracts.

type Options

type Options struct {
	// Registry is the startup command snapshot; callers must not mutate it while serving.
	Registry *cmdregistry.Registry
	// Invoker executes commands; nil selects invoke.Invoke with the server's guard.
	Invoker Invoker
	// ProjectDir is the invocation working directory, not an HTTP-controlled value.
	ProjectDir string
	// Port is the required Host/Origin port; it must match the listener's actual port.
	Port int
	// AllowedHosts adds DNS names, IPv4 or bracketed IPv6 literals to the loopback
	// defaults. Values must omit ports, schemes, paths and whitespace.
	AllowedHosts []string
	// Timeout limits document invocations; nonpositive values use invoke.DefaultTimeout.
	// NDJSON invocations instead run until their request context ends.
	Timeout time.Duration
	// MaxInFlight caps command invocations; nonpositive values default to four.
	MaxInFlight int
	// Auth authenticates all routes before body reads and must not be nil.
	Auth AuthHook
	// DataSchema supplies committed data schemas by command path for OpenAPI.
	// Nil or a missing schema produces an open data object.
	DataSchema func(string) ([]byte, bool)
	// Log receives serialized request metadata only; nil selects os.Stderr.
	Log io.Writer
	// ReadHeaderTimeout bounds header reads; nonpositive values default to ten seconds.
	ReadHeaderTimeout time.Duration
	// ReadTimeout bounds request reads; nonpositive values default to thirty seconds.
	// The deadline is cleared after command request decoding to permit streams.
	ReadTimeout time.Duration
	// IdleTimeout bounds keep-alive idle time; nonpositive values default to sixty seconds.
	IdleTimeout time.Duration
}

Options configures one server. Registry and Auth are required; New supplies documented defaults for optional limits without opening a listener.

type Principal

type Principal struct {
	// Subject is the caller's opaque identity, not a token or a permission list.
	Subject string
}

Principal identifies an authenticated caller; it carries no authorization grants.

type Server

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

Server owns one authenticated mux, startup registry snapshot and invocation guard. Configure mounts before serving; concurrent mount changes are unsupported.

func New

func New(o Options) (*Server, error)

New validates options and builds routes, OpenAPI and one shared invocation guard. It does not listen or execute commands; Registry and Auth are required.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the private mux behind Host, Origin and AuthHook checks and request logging. Command routes additionally enforce invocation validation.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe listens on addr without TLS or a write timeout. Cancellation stops accepting requests and permits five seconds to drain before closing remaining connections and canceling their request contexts. It then waits up to eleven seconds for invocation child cleanup and audit writes. Context values are preserved; its deadline and cancellation do not interrupt the graceful drain. A completed shutdown returns nil; listener failures return their error.

func (*Server) Mount

func (s *Server) Mount(prefix string, h http.Handler) (err error)

Mount adds a handler behind Host, Origin and AuthHook checks before serving. It refuses nil handlers, conflicting patterns, the root, /v1 and /openapi.json prefixes, non-absolute paths and pattern, escape, whitespace or query syntax.

func (*Server) Serve

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

Serve serves an already bound listener with the same cancellation and graceful shutdown behavior as ListenAndServe. It closes the listener when serving ends.

Jump to

Keyboard shortcuts

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