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
- Variables
- func DeliveryRefused(err error) error
- func LimitError(status int, code, message string) error
- func NewLogger(cfg Config) *slog.Logger
- func SQLitePath(databaseURL string) (string, error)
- type Admission
- type Config
- type Email
- type ExecutionKind
- type MCPMode
- type Mailer
- type Options
- type ScheduleAdmission
- type Server
- type Store
- type WorkspaceID
- type WorkspaceResolver
Constants ¶
const ( ExecutedTask = dispatch.KindTask ExecutedWorkflowStep = dispatch.KindWorkflowStep ExecutedCallback = dispatch.KindCallback ExecutedScheduleRun = dispatch.KindScheduleRun )
Kinds of execution, for Options.Executed.
MCP modes, for Options.MCP.
const DefaultWorkspace = workspace.Default
DefaultWorkspace is the single workspace used by dev and self-hosted servers.
Variables ¶
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.
ErrUnauthorized should be returned by a WorkspaceResolver for missing or invalid credentials; the server responds 401.
Functions ¶
func DeliveryRefused ¶ added in v0.4.5
DeliveryRefused wraps an error from DeliveryDialControl to say that the connection would be refused again, e.g. to a private address: the attempt fails its task or run at once instead of being retried, with err's message as its error.
func LimitError ¶
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 SQLitePath ¶
SQLitePath returns the file path in a sqlite:// URL.
Types ¶
type ExecutionKind ¶ added in v0.4.6
Aliases expose internal types without making their packages public.
type Mailer ¶
Aliases expose internal types without making their packages public.
func SMTPMailer ¶
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, and one
// wrapped in DeliveryRefused fails the task or run without retries. 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.
Admission Admission
// Executed, if set, is told of each execution, for metering usage: the
// first delivery attempt of a task, workflow step, callback or schedule
// run since it was created or retried by hand, once the attempt is
// done. Retries with backoff aren't executions, and neither is a
// schedule run that starts a workflow (its steps are). It is called
// from deliveries' goroutines, so it must be safe for concurrent use
// and not block for long.
Executed func(ctx context.Context, ws WorkspaceID, kind ExecutionKind)
// 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 (*Server) MetricsHandler ¶
MetricsHandler serves the metrics in the Prometheus text format, without authentication. See Options.DisableMetrics.
type WorkspaceID ¶
Aliases expose internal types without making their packages public.
type WorkspaceResolver ¶
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.