Documentation
¶
Overview ¶
Package api is the terminal's client for a Greenscreen application, as served by @greenscreen-dev/worker.
GET /v1/health
GET /v1/screen/info what the application calls itself; no logon
POST /v1/session logon: -> {"userid"}
POST /v1/screen the application's screens; see docs/protocol.md
GET /v1/live WebSocket: when to redraw the screen (live.go)
POST /v1/snapshot the screen drawn as a PNG or PDF, if the app has it
The terminal reaches the application's data only through the screens. Everything except health and info is behind Cloudflare Access (see access.go). Errors are returned as {"error":{"code","message","field"}}.
Index ¶
- Constants
- Variables
- type Access
- type AppInfo
- type Client
- func (c *Client) Download(ctx context.Context, path string) ([]byte, error)
- func (c *Client) Host() string
- func (c *Client) Info(ctx context.Context) (AppInfo, error)
- func (c *Client) Listen(ctx context.Context, opened func(), on func(event string)) error
- func (c *Client) Logoff(context.Context) error
- func (c *Client) Logon(ctx context.Context) (string, error)
- func (c *Client) Screen(ctx context.Context, req ScreenRequest) (ScreenResponse, error)
- func (c *Client) Snapshot(ctx context.Context, req SnapshotRequest) ([]byte, string, error)
- type Cloudflared
- type Cursor
- type Download
- type Field
- type LogonResponse
- type Screen
- type ScreenRequest
- type ScreenResponse
- type ServiceToken
- type SnapField
- type SnapshotRequest
- type SnapshotScreen
Constants ¶
const MaxDownload = 32 << 20
MaxDownload is the largest file Download will take.
const ScreenVersion = 1
ScreenVersion is the screen protocol version this client speaks.
Variables ¶
var ( ErrBadLogon = errors.New("not authorized by Cloudflare Access") )
var ErrNoCloudflared = errors.New("cloudflared not found: install it from https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/")
Functions ¶
This section is empty.
Types ¶
type Access ¶
Access gets requests through Cloudflare Access, which guards the API. Headers returns what to add to each request, signing the user in first if needed, which may open a browser.
type AppInfo ¶
type AppInfo struct {
V int `json:"v"`
Title string `json:"title"`
System string `json:"system"`
Prefix string `json:"prefix"`
Banner []string `json:"banner"`
Tagline string `json:"tagline"` // under the banner instead of the title
Live bool `json:"live"` // serves GET /v1/live
Snapshot bool `json:"snapshot"` // serves POST /v1/snapshot
}
AppInfo is GET /v1/screen/info: what the application calls itself, for the logon screen and the client's own messages.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls the application's API on behalf of one logged-on user.
func NewClient ¶
NewClient returns a client for the API at baseURL. access may be nil, for a local `wrangler dev`.
func (*Client) Download ¶ added in v0.2.0
Download fetches a file from the API, such as a PDF a screen sent along.
func (*Client) Info ¶
Info asks the application what it calls itself. It needs no logon, though Access must let the path through (a Bypass policy) for it to work before sign-in.
func (*Client) Listen ¶ added in v0.2.0
Listen connects to GET /v1/live, a WebSocket on which the API says when the screen should be redrawn: {"event":"syslog"} when the system log has moved, {"event":"message"} when a SEND has come for this user. It calls opened once connected, then on with each event's name, until the connection drops or ctx ends, and returns why.
func (*Client) Logon ¶
Logon signs in through Cloudflare Access, which may open a browser, and returns the userid the API knows the user by.
func (*Client) Screen ¶
func (c *Client) Screen(ctx context.Context, req ScreenRequest) (ScreenResponse, error)
Screen sends an AID key and the screen's input to the API and returns the next screen.
type Cloudflared ¶
type Cloudflared struct {
App string // the API's base URL
Path string // cloudflared binary; default "cloudflared" on $PATH
}
Cloudflared signs in with the cloudflared CLI, which opens a browser for the team's identity provider and caches the resulting app token.
type Cursor ¶
type Cursor struct {
Row int `json:"row,omitempty"`
Col int `json:"col,omitempty"`
Field string `json:"field,omitempty"`
}
Cursor is a position, 1-based, and the input field it is in, if any.
type Download ¶ added in v0.2.0
Download names a file to fetch from the API (URL is a path such as /v1/invoices/INV00012/pdf) and save as Name.
type Field ¶
type Field struct {
Row int `json:"row"`
Col int `json:"col"`
Text string `json:"text"`
Name string `json:"name"`
Length int `json:"length"`
Value string `json:"value"`
Color string `json:"color"`
Bright bool `json:"bright"`
Highlight string `json:"highlight"`
Hidden bool `json:"hidden"`
Numeric bool `json:"numeric"`
Mixed bool `json:"mixed"`
// Modified starts the field with its modified data tag set, so it is
// sent back as typed: input kept across help, a picked value, or a
// retrieved command.
Modified bool `json:"modified"`
}
Field is an output field (Text) or, when Name is set, an input field of Length characters prefilled with Value.
type LogonResponse ¶
type LogonResponse struct {
UserID string `json:"userid"`
}
type Screen ¶
type Screen struct {
Map string `json:"map"`
Rows int `json:"rows"`
Cols int `json:"cols"`
Fields []Field `json:"fields"`
Cursor *Cursor `json:"cursor"`
// Refresh, in seconds, asks the client to redraw the screen this often
// while the operator leaves it alone. Zero never does.
Refresh int `json:"refresh"`
// Live names what the screen shows that GET /v1/live announces changes
// to, such as "syslog". While listening, the client redraws on those
// events and Refresh is only a fallback.
Live string `json:"live"`
}
type ScreenRequest ¶
type ScreenRequest struct {
V int `json:"v"`
AID string `json:"aid"`
State string `json:"state,omitempty"`
Fields map[string]string `json:"fields"`
Modified []string `json:"modified"`
Cursor Cursor `json:"cursor"`
Rows int `json:"rows"`
Cols int `json:"cols"`
Terminal string `json:"terminal,omitempty"`
System string `json:"system,omitempty"`
TZOffset int `json:"tz_offset"`
Live bool `json:"live,omitempty"` // listening on GET /v1/live
}
ScreenRequest is sent when an AID key is pressed, or with AID "START" for the first screen, "REDRAW" for a new window size and "TIMEOUT" when the terminal has been left idle too long.
type ScreenResponse ¶
type ScreenResponse struct {
V int `json:"v"`
State string `json:"state"`
Screen *Screen `json:"screen"`
Logoff *struct {
Message string `json:"message"`
} `json:"logoff"`
Download *Download `json:"download"`
}
ScreenResponse holds the next screen and the commarea to send back with the next request, or Logoff when the operator has logged off. Download, when set, is a file sent along with the screen, such as an invoice PDF.
type ServiceToken ¶
type ServiceToken struct{ ID, Secret string }
ServiceToken is an Access service token, for scripts and CI.
type SnapField ¶ added in v0.2.0
type SnapField struct {
Row int `json:"row"`
Col int `json:"col"`
Text string `json:"text"`
Color string `json:"color,omitempty"`
Bright bool `json:"bright,omitempty"`
Highlight string `json:"highlight,omitempty"`
Hidden bool `json:"hidden,omitempty"`
Input bool `json:"input,omitempty"`
}
SnapField is one field as it shows, input fields with what is in them.
type SnapshotRequest ¶ added in v0.2.0
type SnapshotRequest struct {
Format string `json:"format"` // "png" or "pdf"
Map string `json:"map,omitempty"`
Terminal string `json:"terminal,omitempty"`
Screen SnapshotScreen `json:"screen"`
}
SnapshotRequest is the body of POST /v1/snapshot: the screen as the terminal shows it, to be drawn as a PNG or PDF.