server

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 24 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package server is Workly's public Go API: the one package other modules (such as the hosted control plane) import to run and extend the server. Everything else lives in internal/.

Index

Constants

View Source
const (
	MCPReadOnly = mcp.ReadOnly
	MCPFull     = mcp.Full
	MCPOff      = mcp.Off
)

MCP modes, for Options.MCP.

View Source
const DefaultWorkspace = workspace.Default

DefaultWorkspace is the single workspace used by dev and self-hosted servers.

Variables

View Source
var ErrNotFound = store.ErrNotFound

ErrNotFound is returned by Store methods for a record that doesn't exist, such as Store.DeleteWorkspace for a workspace that's gone already.

View Source
var ErrUnauthorized = workspace.ErrUnauthorized

ErrUnauthorized should be returned by a WorkspaceResolver for missing or invalid credentials; the server responds 401.

Functions

func LimitError

func LimitError(status int, code, message string) error

LimitError refuses a request from an Admission: the client gets status, code and message in the API's error shape, e.g. 403 "quota_exceeded". Use 429 only when retrying soon will work: the SDKs retry every 429.

func NewLogger

func NewLogger(cfg Config) *slog.Logger

NewLogger returns the logger described by cfg: JSON or text on stderr.

func SQLitePath

func SQLitePath(databaseURL string) (string, error)

SQLitePath returns the file path in a sqlite:// URL.

Types

type Admission

type Admission = api.Admission

Aliases expose internal types without making their packages public.

type Config

type Config = config.Config

Aliases expose internal types without making their packages public.

func LoadConfig

func LoadConfig(getenv func(string) string) (Config, error)

LoadConfig reads the WORKLY_* environment variables (via getenv), so that builds wrapping the server share its configuration. Call Validate on the result after applying any overrides.

type Email

type Email = alert.Email

Aliases expose internal types without making their packages public.

type MCPMode

type MCPMode = mcp.Mode

Aliases expose internal types without making their packages public.

type Mailer

type Mailer = alert.Mailer

Aliases expose internal types without making their packages public.

func SMTPMailer

func SMTPMailer(url, from string) (Mailer, error)

SMTPMailer sends alert emails through an SMTP server, given as smtp://user:pass@host:587 (STARTTLS) or smtps://user:pass@host:465, from the sender address from, e.g. "Workly <alerts@example.com>".

type Options

type Options struct {
	// Addr is the listen address, e.g. ":7337".
	Addr  string
	Store Store
	// WorkspaceResolver defaults to StaticResolver("").
	WorkspaceResolver WorkspaceResolver
	// Logger defaults to slog.Default().
	Logger *slog.Logger
	// ShutdownTimeout bounds graceful shutdown: in-flight HTTP requests and
	// deliveries get this long to finish. Defaults to 25s, which fits in
	// Kubernetes' default 30s termination grace period.
	ShutdownTimeout time.Duration
	// OnReady, if set, is called once the server is listening.
	OnReady func(addr net.Addr)

	// SigningSecret signs deliveries (the Workly-Signature header). Required
	// unless DisableDispatcher is set.
	SigningSecret string
	// PreviousSigningSecret, if set, adds a second signature to each
	// delivery, so the signing secret can be rotated without failing
	// deliveries: endpoints verifying with either secret accept them.
	PreviousSigningSecret string
	// SigningSecrets, if set, gives each workspace its own secrets for
	// signing deliveries (the current one, and the previous one during a
	// rotation), instead of SigningSecret and PreviousSigningSecret. It's
	// called for every delivery, so it should cache. For a server shared by
	// untrusted users, who mustn't be able to sign for each other.
	SigningSecrets func(ctx context.Context, ws WorkspaceID) (current, previous []byte, err error)
	// MaxInFlight caps concurrent task deliveries across all queues and
	// workspaces. Defaults to 100.
	MaxInFlight int
	// WorkspaceMaxInFlight, if set, returns a workspace's own cap on
	// concurrent task deliveries, within MaxInFlight (0 for none), so on a
	// server shared by many workspaces one with slow endpoints can't hold
	// every delivery slot. Each workspace's cap is looked up again a minute
	// after the last lookup, and kept if a lookup fails. Free slots are shared
	// equally by the workspaces with tasks due either way, however many
	// queues each has.
	WorkspaceMaxInFlight func(ctx context.Context, ws WorkspaceID) (int, error)
	// DisableDispatcher runs the API only, without delivering tasks.
	DisableDispatcher bool
	// DisableQueueAutoCreate makes enqueueing to a missing queue an error
	// instead of creating the queue with defaults.
	DisableQueueAutoCreate bool
	// MaxPayloadBytes limits task bodies. Defaults to 1 MiB.
	MaxPayloadBytes int64
	// Retention is how long finished tasks (succeeded, failed or cancelled)
	// are kept before they are deleted. 0 keeps them forever.
	Retention time.Duration
	// RunRetention is the same for schedule runs, except that each
	// schedule's latest run is always kept. 0 keeps them forever.
	RunRetention time.Duration
	// WorkspaceRetention, if set, returns the workspaces that keep finished
	// tasks, groups and runs for their own duration instead of Retention and
	// RunRetention (0 keeps them forever), e.g. by plan. It's called once
	// per sweep and when the API reports retention, so keep it cheap; while
	// it fails, nothing is swept.
	WorkspaceRetention func(ctx context.Context) (map[WorkspaceID]time.Duration, error)
	// DevEndpoints serves the /_dev/echo delivery target. For `workly dev`.
	DevEndpoints bool
	// DeliveryDialControl, if set, is called with the resolved address of
	// each connection a delivery makes, before connecting (see
	// net.Dialer.Control); an error fails the attempt with it. A server
	// open to untrusted users can refuse private networks with it. Proxy
	// settings from the environment are ignored while it's set, since a
	// proxy would connect on Workly's behalf.
	DeliveryDialControl func(network, address string, c syscall.RawConn) error
	// Admission, if set, is asked before tasks are enqueued and schedules
	// created or replaced, through the API and so through MCP, and can
	// refuse with LimitError. For quotas on a hosted service. It is also
	// told of the tasks a schedule creates by starting a workflow run.
	Admission Admission
	// Mailer, if set, sends alert emails, so workspaces can have email
	// alert channels; SMTPMailer builds one. Slack channels need nothing.
	Mailer Mailer
	// PublicURL is where people open Workly, e.g.
	// https://workly.example.com. Alerts link to its dashboard; without it
	// they have no links.
	PublicURL string
	// Version is sent in the User-Agent of deliveries.
	Version string
	// Dashboard serves the web UI at /. Defaults to the dashboard embedded
	// in this module; a wrapping build passes its own, e.g.
	// dashboard.HandlerFS over its embedded dist.
	Dashboard http.Handler
	// WrapHandler, if set, wraps the server's HTTP handler, e.g. to mount a
	// wrapping build's own routes in front of Workly's.
	WrapHandler func(http.Handler) http.Handler
	// DisableMetrics doesn't serve /metrics on the API listener, e.g. to
	// serve MetricsHandler on an internal port instead. With WORKLY_API_KEY
	// set, /metrics requires it like /v1.
	DisableMetrics bool
	// MetricsAddr, if set, serves /metrics on this address (e.g. ":9090")
	// without authentication, and not on Addr. Keep the port internal to
	// the cluster; it lets Prometheus scrape without an API key.
	MetricsAddr string
	// MCP sets which tools the MCP endpoint (/mcp) serves to coding agents:
	// MCPReadOnly (the default) serves the tools that only read, MCPFull
	// adds ones that enqueue, retry, cancel and reconfigure, and MCPOff
	// doesn't serve it. /mcp takes the same credentials as /v1.
	MCP MCPMode
	// MCPModeFor, if set, is asked for each /mcp request once its workspace
	// is resolved, and can make it read-only (anything but MCPFull) when MCP
	// is MCPFull; it can't widen MCP. For credentials that carry their own
	// access, such as OAuth tokens granted only read access.
	MCPModeFor func(*http.Request) MCPMode
	// MCPResourceMetadata, if set, is the URL of /mcp's OAuth protected
	// resource metadata (RFC 9728): a 401 on /mcp names it in its
	// WWW-Authenticate header, so MCP clients can sign in with OAuth
	// instead of an API key. Workly doesn't serve the metadata or issue
	// tokens; the WorkspaceResolver accepts whatever tokens are issued.
	MCPResourceMetadata string
}

type ScheduleAdmission

type ScheduleAdmission = api.ScheduleAdmission

Aliases expose internal types without making their packages public.

type Server

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

func New

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

func (*Server) Handler

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

Handler returns the server's HTTP handler, for embedding or tests.

func (*Server) MetricsHandler

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

MetricsHandler serves the metrics in the Prometheus text format, without authentication. See Options.DisableMetrics.

func (*Server) Run

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

Run serves and delivers tasks until ctx is cancelled, then shuts down gracefully: in-flight requests and deliveries get ShutdownTimeout to finish.

type Store

type Store = store.Store

Aliases expose internal types without making their packages public.

func OpenStore

func OpenStore(ctx context.Context, databaseURL string) (Store, error)

OpenStore opens a store from a database URL:

postgres://user:pass@host:5432/db
sqlite://relative/path.db
sqlite:///absolute/path.db

type WorkspaceID

type WorkspaceID = workspace.ID

Aliases expose internal types without making their packages public.

type WorkspaceResolver

type WorkspaceResolver = workspace.Resolver

Aliases expose internal types without making their packages public.

func StaticResolver

func StaticResolver(apiKey string) WorkspaceResolver

StaticResolver resolves every request to DefaultWorkspace. If apiKey is non-empty, /v1 requests must present it as a bearer token.

Jump to

Keyboard shortcuts

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