api

package
v0.0.1-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Overview

Package api wires together the sqi-server HTTP surface: the chi router, standard middleware stack, and all route groups.

Router layout

GET /healthz — liveness probe
GET /readyz — readiness probe
GET /metrics — Prometheus scrape endpoint
GET /debug/pprof/* — Go runtime profiling (gated by config)
/api/v1/* — REST API
GET /api/v1/ws — WebSocket upgrade
GET /api/v1/openapi.yaml — OpenAPI 3.1 spec
/* — embedded SPA + static assets

Middleware stack (outermost → innermost)

  1. chi Recoverer — catches panics, returns 500, logs stack trace
  2. go-chi/cors — CORS preflight + headers (origins from Config)
  3. chi RequestID — sets X-Request-ID; chi's version generates a UUID when the header is absent
  4. chi Compress — gzip response bodies for clients that accept it
  5. middleware.RequestLogger — structured slog request logging
  6. middleware.RequestMetrics — Prometheus HTTP metrics

/api/v1 sub-router middleware

  1. middleware.APIVersion("1") — sets X-API-Version: 1 on every API response. To mark individual endpoints as deprecated, wrap their handler with middleware.Deprecated.

Note: the structured-logging and metrics middleware are mounted here instead of the now-removed obsServer so every HTTP and WebSocket request continues to be logged and counted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRouter

func NewRouter(cfg Config, deps Deps, logger *slog.Logger, m *metrics.Metrics, hr *health.Registry) chi.Router

NewRouter builds and returns the chi router that serves the full sqi-server HTTP surface. The returned router is ready to be handed to http.Server.

Route groups for the REST API (/api/v1/*) and WebSocket (/api/v1/ws) are populated incrementally as each task is completed. Pass a Deps value with all application-layer dependencies for the currently implemented handlers.

Types

type Config

type Config struct {
	// CORSOrigins is the list of origins the CORS middleware allows.
	// Use ["*"] to permit all origins (suitable for local/dev deployments).
	// Defaults to ["*"] when empty.
	CORSOrigins []string

	// EnablePprof registers Go runtime profiling endpoints at /debug/pprof/
	// when true. Must never be enabled on servers exposed to untrusted networks.
	EnablePprof bool

	// DisableRateLimit turns off per-IP rate limiting when true.
	// Use in tests and benchmarks where the loopback address would cause all
	// concurrent requests to share one bucket and trigger 429s.
	DisableRateLimit bool

	// WorkerOfflineThreshold is the heartbeat-timeout window used to decide
	// whether a disabled worker is dead (and thus removable). It mirrors the
	// scheduler's WorkerTimeout. Zero is treated as "no grace" (a disabled
	// worker with any past heartbeat is considered dead).
	WorkerOfflineThreshold time.Duration
}

Config holds HTTP-layer configuration for NewRouter.

type Deps

type Deps struct {
	// Store provides access to all persistent state (jobs, tasks, workers, …).
	Store store.Store

	// Submitter handles the full OpenJD parse → validate → persist pipeline
	// used by POST /api/v1/jobs.
	Submitter *openjd.Submitter

	// Scheduler is the running scheduler instance. It is called by
	// DELETE /api/v1/jobs/{id} to propagate cancellation to workers
	// and by future task/retry endpoints.
	Scheduler *scheduler.Scheduler

	// Hub is the WebSocket fan-out hub used by the /api/v1/ws handler.
	// May be nil in tests that do not exercise WebSocket push;
	// the handler degrades gracefully by accepting subscriptions without
	// delivering any pushes.
	Hub *ws.Hub

	// DiagReader exposes the in-memory diagnostic log buffer to
	// GET /api/v1/diagnostics/logs. It is nil-tolerant: when nil (diagnostics
	// disabled) the endpoint responds with 503.
	DiagReader DiagReader

	// Version is the server build metadata reported by GET /api/v1/version.
	// The zero value is acceptable (fields default to empty strings).
	Version version.Info
}

Deps holds the application-layer dependencies injected into the REST handlers. All fields are required; a nil field will cause a panic when the corresponding endpoint is first called.

type DiagReader

type DiagReader interface {
	Query(diag.Filter) []diag.Record
}

DiagReader is the read-only view of the diagnostic log buffer the handler depends on. *diag.Buffer satisfies it. When diagnostics are disabled the server injects a nil reader, in which case the endpoint returns 503.

Jump to

Keyboard shortcuts

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