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

server

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package server implements the HTTP API and serves the embedded viewer.

The viewer assets are bundled into the binary via embed.FS so a single `ckg serve` binary needs no sidecar files. The build assumes `make viewer` has been run so internal/server/web_assets/{index.html, assets/viewer.js[.map]} exist before `go build`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyViewerAssetsTo

func CopyViewerAssetsTo(dst string) error

CopyViewerAssetsTo materialises the embedded viewer onto disk under dst. Used by `ckg export-static` (T31) so the same assets that ship with the server can be pinned into a static export bundle.

Types

type Options

type Options struct {
	DevViewerDir string
	NoViewer     bool
}

Options tunes how Server mounts the static viewer surface. The zero value preserves the original behavior (embedded viewer at `/`).

  • DevViewerDir overrides the embedded FS with a disk path. Set by `CKG_DEV_VIEWER_DIR` so a viewer dev loop (`make viewer` after each edit) doesn't require rebuilding the ckg binary. Ignored when empty.
  • NoViewer skips the static mount entirely, leaving only `/api/*` reachable. Used by `ckg serve --no-viewer` for operators who front the API with their own reverse proxy + separately hosted viewer (the `ckg export-static` bundle).

type Server

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

Server bundles a read-only Store, a routed mux, and a logger. Construct one per `ckg serve` invocation. Server implements http.Handler so callers (and tests via httptest) can drive it directly.

The store field is the read-only persist.StoreReader interface — server has no business writing to the graph. This narrowing also lets the future PostgreSQL backend (spec §3 / WORK-PLAN B2) plug in without rewiring server.

func New

func New(store persist.StoreReader, log *slog.Logger) *Server

New wires routes against store and returns a ready-to-serve Server with default options (embedded viewer mounted at `/`). A nil log is replaced with a stderr text logger so handlers can always log without a nil check.

func NewWithOptions

func NewWithOptions(store persist.StoreReader, log *slog.Logger, opts Options) *Server

NewWithOptions is the configurable constructor. See Options.

Manifest caching: the underlying SQLite kv-read for /api/manifest measured at p50=235ms on the go-stablenet baseline. Because the manifest only changes on a fresh `ckg build`, we read it once at construction time and serve every subsequent caller from memory via the cachedManifestStore wrapper. Trade-off: external graph rebuild while serve is up will produce stale manifest reads (and stale evidence-cache invalidation) — `ckg serve` has always been "stop and restart on rebuild" in practice, so this matches the existing operational contract.

TicketIndex pre-warm: the evidence cache's first BuildPack / TicketIndex call materialises the BM25 corpus + per-hunk virtual docs (~5s on the same graph). Kicking it off in a background goroutine at boot pushes that cost off the user's first request. Subsequent calls hit the warm cache (sync.RWMutex double-check locked) and land at ~190ms p50.

func (*Server) ListenAndServe

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

ListenAndServe runs the HTTP server until ctx is cancelled. On cancel, http.Server.Shutdown is invoked with a fresh background context so the graceful path runs even after the parent ctx is already done.

http.ErrServerClosed is suppressed because that is the expected outcome of a clean Shutdown — surfacing it would force every caller to special-case it.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes Server satisfy http.Handler, primarily so tests can drive it via httptest.NewServer.

Jump to

Keyboard shortcuts

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