api

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 14 Imported by: 0 modules, 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 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

View Source
const MaxDownload = 32 << 20

MaxDownload is the largest file Download will take.

View Source
const ScreenVersion = 1

ScreenVersion is the screen protocol version this client speaks.

Variables

View Source
var (
	ErrUnauthorized = errors.New("not logged on or session expired")
	ErrBadLogon     = errors.New("not authorized by Cloudflare Access")
	ErrUnavailable  = errors.New("host not responding")
)
View Source
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

type Access interface {
	Headers(ctx context.Context) (http.Header, error)
}

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

func NewClient(baseURL string, access Access) *Client

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

func (c *Client) Download(ctx context.Context, path string) ([]byte, error)

Download fetches a file from the API, such as a PDF a screen sent along.

func (*Client) Host

func (c *Client) Host() string

Host returns the server's host:port, for display.

func (*Client) Info

func (c *Client) Info(ctx context.Context) (AppInfo, error)

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

func (c *Client) Listen(ctx context.Context, opened func(), on func(event string)) error

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) Logoff

func (c *Client) Logoff(context.Context) error

Logoff forgets the user locally. Access sessions end on their own.

func (*Client) Logon

func (c *Client) Logon(ctx context.Context) (string, error)

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.

func (*Client) Snapshot added in v0.2.0

func (c *Client) Snapshot(ctx context.Context, req SnapshotRequest) ([]byte, string, error)

Snapshot has the API draw a screen as a PNG or PDF and returns the file and the name the API gave it.

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.

func (Cloudflared) Headers

func (c Cloudflared) Headers(ctx context.Context) (http.Header, error)

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

type Download struct {
	URL  string `json:"url"`
	Name string `json:"name"`
}

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.

func (ServiceToken) Headers

func (t ServiceToken) Headers(context.Context) (http.Header, error)

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.

type SnapshotScreen added in v0.2.0

type SnapshotScreen struct {
	Rows   int         `json:"rows"`
	Cols   int         `json:"cols"`
	Fields []SnapField `json:"fields"`
	Cursor *Cursor     `json:"cursor,omitempty"`
}

Jump to

Keyboard shortcuts

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