apiserver

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 36 Imported by: 0

Documentation

Overview

Package apiserver implements a standalone REST API server that exposes all cloop functionality over HTTP with an OpenAPI 3.0 spec at /openapi.json. It is designed for CI/CD integration, external dashboards, and scripting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIRoutes added in v0.0.2

func APIRoutes() []apidoc.Route

APIRoutes describes this surface for the documentation generator in tests/docs, which renders docs/reference/http-api.md from it and from pkg/ui.APIRoutes.

Builds a zero Server: the handlers are method values, well-formed without server state, and nothing else about a route depends on a running daemon.

Types

type Server

type Server struct {
	WorkDir string
	Port    int
	Token   string // optional bearer token; empty = no auth

	// RPS and Burst control the per-IP token-bucket rate limiter.
	// Zero values use defaultRPS / defaultBurst.
	RPS   float64
	Burst int

	// MaxRequestBodyBytes caps any incoming POST/PUT/PATCH request body
	// the server will accept. Zero substitutes maxJSONBodyBytes (10 MiB).
	// Oversize requests are rejected with HTTP 413. Set via
	// config.UIConfig.MaxRequestBodyBytes when wired through cmd/serve.go.
	// Task 20102.
	MaxRequestBodyBytes int64

	// TLSCertFile / TLSKeyFile enable native HTTPS. Both must be set or
	// neither; Run refuses to start on a half-configuration rather than
	// silently serving the bearer token over plaintext. TLSMinVersion is
	// "1.2" (default) or "1.3". Set from --tls-cert / --tls-key, or from
	// ui.tls.* in .cloop/config.yaml.
	TLSCertFile   string
	TLSKeyFile    string
	TLSMinVersion string

	// ListenHost is the address Run binds, without the port (--listen).
	// Empty takes the default for this server's authentication: every
	// interface with a Token, 127.0.0.1 without one — because without one
	// POST /run/start starts a run on this host for anyone who reaches it.
	// An address beyond loopback without a Token is refused unless
	// AllowUnauthenticatedNetwork is set (Task 20393; pkg/exposure).
	ListenHost string
	// AllowUnauthenticatedNetwork is ui.allow_unauthenticated_network, read
	// from the same shared ui block as TLS: a server without a Token may bind
	// the address ListenHost names even beyond loopback, and warns at every
	// start that it does.
	AllowUnauthenticatedNetwork bool

	// TrustedProxies is ui.trusted_proxies, from the same shared ui block:
	// the peers whose X-Forwarded-* are believed, loopback always among them
	// (Task 20394).
	TrustedProxies sameorigin.Proxies
	// AllowedHosts is ui.allowed_hosts: names a server without a token
	// answers to besides localhost and IP addresses (originGuard).
	AllowedHosts []string

	// ReadyCheck overrides the readiness check used by /readyz. nil means
	// use defaultReadyCheck (stat state.db, open it, run SELECT 1 bounded
	// by ctx). Tests override this field to simulate degraded states like
	// "closed db handle" or "state store not initialized".
	ReadyCheck func(ctx context.Context) error

	// Log is the structured logger used for lifecycle / error messages
	// emitted by the API server itself (startup, shutdown, panics in
	// background goroutines). Nil means the package picks a sensible
	// default at first-use (text output to stdout, project bound).
	Log logger.Logger
	// contains filtered or unexported fields
}

Server is the cloop REST API HTTP server.

func New

func New(workdir string, port int, token string) *Server

New creates a new API server.

func (*Server) BoundAddr added in v0.0.5

func (s *Server) BoundAddr() net.Addr

BoundAddr is the address Run's listener holds, or nil before it has bound and after a refused start.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run is the lifecycle entrypoint without signal handling: it blocks until ctx is cancelled or the underlying http.Server fails. On ctx cancellation it triggers a bounded graceful shutdown (10s) and returns nil.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown initiates a graceful shutdown of a server started via Run. Safe to call from any goroutine; if the server has not started or has already been shut down it is a no-op. ctx bounds how long Shutdown will wait for in-flight requests to drain.

func (*Server) Start

func (s *Server) Start() error

Start begins listening on the configured port. It installs SIGINT/SIGTERM handlers so the daemon shuts down gracefully when supervised by systemd or interrupted from a TTY: in-flight requests drain (bounded to 10s) before Start returns nil. Production callers use this; tests should call Run with their own context for fine-grained lifecycle control.

Jump to

Keyboard shortcuts

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