Captain / Documentation / Part 6. The standard library

Worker, app and Gump modules

Contents ↓

Three groups of source modules define the contract between a Captain program and a host that runs it: Worker::Http for workers that answer HTTP on Gump, App::Ui for apps with a user interface, and the Gump:: modules that install and observe Gump itself. This page lists their surface. Part 8 describes the hosts.

Worker::Http

lib/worker/http.capt, profile captain.worker/1. Needs VM 7. The worker runtime refuses a program whose copies of the three structs and three effects below differ from these; the helpers after them are ordinary code and may grow. The full contract is WORKER_V1.

A worker exports one handler:

Fragment
export fetch request: Http::Request -> Http::Response [ ... ].
Type Fields
Header name: Text, value: Text
Request id: Text, method: Text, path: Text, query: Text, headers: List[Header], body: Octets, received: i64
Response status: i32, headers: List[Header], body: Octets

received is the time the runtime read the request head, in milliseconds since the Unix epoch: the request's own observation of time, so a handler stays deterministic and replayable without a clock.

Effect Gives What it does
fetch method: Text url: Text headers: List[Header] body: Octets Response one outbound HTTPS request to an origin the worker is allowed
fetchWithSecret method: Text url: Text headers: List[Header] body: Octets header: Text secret: Secret[Opaque] Response the same, with request header header set to the secret's value, which the program never sees
log message: Text bool one line in the worker's log

These effects are declared without a body: the worker runtime implements them, and captain run does not.

Helper Gives What it does
response status: i32 headers: List[Header] body: Octets Response any response
text status: i32 body: Text Response text/plain; charset=utf-8
json status: i32 body: Text Response application/json; the body is JSON text you have already written
header name: Text value: Text Header one header
header named: Text in: Request Option[Text] the first header with that name, which must be given in lower case
bodyText of: Request Text the body decoded as UTF-8, with replacement characters for invalid bytes

The builders exist because a module cannot write a record literal of another module's struct.

greeter.capt
module Example::Greeter.

use Worker::Http.

;; The worker's handler: every request comes here.
export fetch request: Http::Request -> Http::Response [
  ret greet name: (Http::bodyText of: request).
].

export greet name: Text -> Http::Response [
  ((name trimmed) isEmpty) ifTrue: [ ret Http::text status: 400 body: "say who\n" ].
  ret Http::json status: 200
    body: (("{\"hello\":\"" append: (name trimmed)) append: "\"}").
].

fetch request: takes a struct, so it is not a command-line entry point, but the pure greet name: can be tried directly:

Terminal
$ captain build greeter.capt -I lib/worker -o greeter.capb
built greeter.capb ([..] bytes, sha256 [..])
$ captain run greeter.capb greet name=ada
run log: greet.runlog
Worker::Http::Response { status: 200, headers: #( Worker::Http::Header { name: "content-type", value: "application/json" } ), body: octets 7b2268656c6c6f223a22616461227d }

The body is printed as octets: the hex of {"hello":"ada"}.

App::Ui

lib/app/ui.capt, profile captain.app/1. Unlike the rest of the library, this file is under the MIT licence (lib/app/LICENSE), so an app may carry it freely. The six structs are frozen for captain.app/1: the app host refuses an app whose copies differ. The full contract, including the five procedures an app must export (wants, start launch:, update state:message:, receive state:reply:, view state:), is APP_V1.

Type Fields
Attr name: Symbol, value: Text
View kind: Symbol, text: Text, value: Text, on: Symbol, attrs: List[Attr], children: List[View]
Message name: Symbol, text: Text, at: i64 (when the host took the event, in Unix milliseconds)
Command id: Text, kind: Symbol, target: Text, body: Text
Reply id: Text, outcome: Symbol (#ok, #refused, #failed or #unknown), status: i32, body: Text, at: i64
Launch now: i64, locale: Text, seed: Text, home: Text

View builders, all giving View:

Builder Element
column children: List[View], row children: List[View] layout
heading text: Text, heading text: Text level: i32 a heading
text text: Text, text text: Text tone: Symbol text; tone is #muted, #strong, #danger or #good
button text: Text on: Symbol a button that sends the message on
input value: Text on: Symbol, input label: Text value: Text on: Symbol a one-line field
area label: Text value: Text on: Symbol a multi-line field
check label: Text checked: bool on: Symbol a checkbox
choice label: Text value: Text on: Symbol options: List[View] a choice among option text: Text value: Text elements
image asset: Text description: Text an image from the app's package
leaf kind: Symbol an element with nothing else, such as #divider or #space
element kind: text: value: on: attrs: children: any element, spelled out
with view: View attr: Symbol value: Text the same element with one more attribute
keyed view: View key: Text the same element with a key, so it keeps its identity across views

Command builders, all giving Command; the id comes back on the reply:

Builder Command
get path: Text id: Text HTTPS GET to the app's home; path is relative to it
post path: Text body: Text id: Text HTTPS POST of body as text/plain
after ms: i64 id: Text a timer: the reply comes after ms milliseconds
store key: Text value: Text id: Text, load key: Text id: Text the app's own storage
copy text: Text id: Text copy to the clipboard
openFile id: Text, saveFile name: Text body: Text id: Text ask the person to open or save a file
command kind: Symbol target: Text body: Text id: Text any command, spelled out
Helper Gives What it does
fine reply: Reply bool the reply is #ok with HTTP status 200

An app with nothing to ask returns no commands as #().

counter.capt
module Example::Counter.

use App::Ui.

export struct State { count: i32 }.
export struct Update { state: State, commands: List[Ui::Command] }.

export wants -> List[Symbol] [ ret List empty: Symbol. ].

export start launch: Ui::Launch -> Update [
  ret Update { state: State { count: 0 }, commands: #() }.
].

export update state: State message: Ui::Message -> Update [
  ((message name) = #more) ifTrue: [
    ret Update { state: State { count: (state count) + 1 }, commands: #() }.
  ].
  ret Update { state: state, commands: #() }.
].

export receive state: State reply: Ui::Reply -> Update [
  ret Update { state: state, commands: #() }.
].

export view state: State -> Ui::View [
  ret Ui::column children: #(
    (Ui::heading text: "Counter"),
    (Ui::text text: (Text from: (state count)) tone: #strong),
    (Ui::button text: "One more" on: #more)
  ).
].

Gump

The lib/gump/ modules install a Gump node, form a cluster, and observe it. They are the platform's own operations and are listed here only so the library is complete; most programs will not call them. Several rely on the platform-internal effects named in Processes, HTTP and hosts, which keep activation material inside the runtime.

Module What it holds
Gump::Bootstrap the zero-to-one operation: prepare, initialize, claimRunning, reform and complete effects that prepare a host over SSH and hand activation to Gump
Gump::Install install effect: installs and verifies one exact Gump package without starting it
Gump::AptRepository configure effect on SshHost: installs the signed repository key and source with pinned digests, then refreshes indexes
Gump::Activation collect effect on SshHost: collects the one-time activation bundle
Gump::Handoff bind effect: binds the activation to the machine identity
Gump::Initialization prepare effect: mints a new cluster's initialization material
Gump::Initialize claim effect: crosses the zero-to-one boundary through Gump's own command line
Gump::Management status, snapshot and workloadSnapshot effects: authenticated observation of a running cluster
Gump::Capacity pure capacity arithmetic for a fixed three-member cluster: fixedThree observed:, usableCount members:, assessFixedThree members:
Gump::Package install package:, a thin composition over AptGet::install package:
Gump::Verify verify at:: the installed executable's version, over SSH