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

preview

package
v0.32.5 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package preview is the preview backend: it starts or detects the project's dev server from the starter manifest's dev command and port (loaded through pkg/starterstore), and tracks the server's lifecycle — starting, running, stopped, failed — for the preview pane's presentational component.

The Manager is the single owner of a dev-server start for one project root, and it follows the starter manifest strictly:

  • it never guesses a command. A missing manifest, or a manifest without a usable dev declaration (a dev command AND a dev port), settles the state to stopped with a reason;
  • it detects an already-running server on the manifest's dev port and adopts it (running, detected) instead of starting a second one;
  • a dev server it starts runs in its own process group (verify's DevProcess, the shared platform process type), so Stop releases the port for real, and a liveness monitor settles the state to failed when the dev command dies or the port stops answering.

The package is standard-library plus repo-internal only, so it compiles for the CLI, the webui server, and WASM builds alike. Under WASM the process support is a no-op stub (verify's js build), so Start settles to failed with the stub's reason; hosted previews are out of scope here and are not a local dev server.

Index

Constants

View Source
const DefaultReadyTimeout = 30 * time.Second

DefaultReadyTimeout bounds how long Start waits for the dev port to answer after spawning the dev command: it mirrors verify's dev-server timeout (a cold dev-server start without stalling a turn).

Variables

This section is empty.

Functions

func PortServing

func PortServing(port int) bool

PortServing reports whether anything answers a plain HTTP GET on port (any status counts — the dev-port probe). Callers outside the package use it for the detection fast path (the webui start handler) so a dev server already up on the manifest's port is reported synchronously instead of after a start that would find it mid-flight.

Types

type Manager

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

Manager drives one project's dev server. One manager per project root; every method is safe for concurrent use.

func New

func New(root string, opts ...Option) *Manager

New returns the preview manager for a project root: the manager that starts or detects that project's dev server.

func (*Manager) Restart

func (m *Manager) Restart(ctx context.Context) State

Restart stops the dev server this manager started and starts it again. A detected (external) server is not owned, so a restart re-detects it: the port already serves the app, and the outcome is running (detected).

func (*Manager) Start

func (m *Manager) Start(ctx context.Context) State

Start ensures the project's dev server is running and returns the state it settled into:

  • running (detected) when something already answers on the manifest's dev port — the manager adopts it instead of starting a second one;
  • running when it spawned the manifest's dev command and the port answered within the ready timeout;
  • failed with a reason when the dev command could not be started, exited before readiness, or the ready timeout elapsed;
  • stopped with a reason when the project has no usable dev declaration (missing or invalid manifest, no dev command, no dev port) — the manager never guesses a command.

Start is synchronous: it blocks until the outcome is known. Callers that want a non-blocking start (the webui handler, so the pane can poll State) run it in a goroutine. A Start that arrives while a first start is still settling waits for that cycle and reports its settled state — it never spawns a second server.

func (*Manager) State

func (m *Manager) State() State

State returns the dev server's current lifecycle state.

A detected (external) running server is re-probed on every read: an external server that dies later settles to stopped (the manager did not start it, so "failed" would be a lie). A started (owned) server's liveness is maintained by the liveness monitor instead, so State never blocks on a probe in that case.

func (*Manager) Stop

func (m *Manager) Stop() State

Stop stops the dev server this manager started, releasing the port. It is idempotent: stopping an absent or already-reaped server is a no-op. A detected (external) server is not owned, so Stop leaves it alone and reports its (still running) state.

type Option

type Option func(*Manager)

Option customizes a Manager.

func WithReadyTimeout

func WithReadyTimeout(d time.Duration) Option

WithReadyTimeout overrides how long Start waits for the dev port to answer after spawning the dev command (DefaultReadyTimeout otherwise).

type State

type State struct {
	Status   Status `json:"status"`
	URL      string `json:"url,omitempty"`
	Error    string `json:"error,omitempty"`
	Detected bool   `json:"detected,omitempty"`
	// Hosted marks that the URL is a platform-registered preview (the agent
	// called register_preview_port in a hosted workspace) rather than a
	// local dev server managed by this package. The pane embeds the hosted
	// URL directly; local start/restart/stop actions never apply to it.
	Hosted bool `json:"hosted,omitempty"`
}

State is the lifecycle snapshot the preview pane renders: the state, the embed URL while running, the reason while stopped/failed, and whether a running server was detected on the port rather than started by the manager.

type Status

type Status string

Status is one of the preview pane's four lifecycle states: the words the pane's status prop uses, so the API and the pane speak the same language.

const (
	StatusStarting Status = "starting"
	StatusRunning  Status = "running"
	StatusStopped  Status = "stopped"
	StatusFailed   Status = "failed"
)

The four lifecycle states a dev server can be in.

Jump to

Keyboard shortcuts

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