api

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 13 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 engine's local API: methods called by name with JSON in and out over a WebSocket on a Unix socket, each with a JSON schema for its input and output, under the physa.v1 subprotocol and one API revision; the engine may send messages of its own on the same connection.

Index

Constants

View Source
const (
	Subprotocol = client.Subprotocol // the WebSocket subprotocol both sides require
	Revision    = 1                  // rises when a method changes in a way a client could notice
	Path        = client.Path        // the only HTTP path the server answers, with GET

	CodeBadRequest    = client.CodeBadRequest
	CodeUnknownMethod = client.CodeUnknownMethod
	CodeInternal      = client.CodeInternal
)

The wire's names are the public client's, so the two never part.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn is the connection a handler's request came on. It stays valid after the handler returns, until the connection closes.

func (*Conn) AfterReply

func (c *Conn) AfterReply(fn func())

AfterReply runs fn once the handler's reply is written (or failed to be, the connection having closed); called later, it runs fn at once. So engine.stop replies before shutdown. fn must only signal: never call Server.Close from it, which waits for the goroutine running fn.

func (*Conn) Send

func (c *Conn) Send(ctx context.Context, event string, data any) error

Send sends a message of the engine's own, {"event":name,"data":data}. It returns an error once the connection has closed.

type DescribeInput

type DescribeInput struct {
	Name string `json:"name" desc:"The method to describe."`
}

DescribeInput is api.describe's input.

type Description

type Description struct {
	Name            string            `json:"name" desc:"The method's name."`
	Summary         string            `json:"summary" desc:"What the method does, in one line."`
	Notes           []string          `json:"notes" desc:"Anything a caller should know; empty when there is nothing."`
	Input           *apischema.Schema `json:"input" desc:"The JSON schema of the method's params."`
	Output          *apischema.Schema `json:"output" desc:"The JSON schema of the method's result."`
	RequestExample  string            `json:"request_example" desc:"Example params, as JSON text."`
	ResponseExample string            `json:"response_example" desc:"Example result, as JSON text."`
}

Description is api.describe's output.

type Error

type Error = client.Error

Error is the error a call replies with: a code from the list above and a message for a person.

type Hooks

type Hooks struct {
	Connected, Disconnected func()
	Called                  func(method string)
}

Hooks tell the engine what happens on its connections; any may be nil. Disconnected runs once a connection's calls have returned too; Called runs before each request, with the method it names.

type Method

type Method struct {
	Name, Summary                   string
	Notes                           []string
	Input, Output                   *apischema.Schema
	RequestExample, ResponseExample string
	// contains filtered or unexported fields
}

Method is one method of the API: its name, what it does, the schemas of its input and output, and an example of each. Build one with Define.

func Define

func Define[In, Out any](name, summary string, notes []string, reqExample, respExample string,
	fn func(ctx context.Context, c *Conn, in In) (Out, error)) Method

Define builds a method typed once, by In and Out. A call checks its params (missing or null mean {}) against In's schema, replying bad_request with the check's message, then decodes them into In. A returned *Error is replied as it is; any other error replies internal.

type MethodSummary

type MethodSummary struct {
	Name    string `json:"name" desc:"The method's name, as a call names it."`
	Summary string `json:"summary" desc:"What the method does, in one line."`
}

MethodSummary is one line of api.list's output.

type Server

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

Server serves a table on Path. It tracks every open connection and its request goroutines, because http.Server.Shutdown neither closes nor waits for a connection upgraded to a WebSocket: the engine calls Close.

func NewServer

func NewServer(t *Table, h Hooks) *Server

NewServer serves t, telling h what happens.

func (*Server) Close

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

Close refuses new connections, closes every open one with going-away in parallel, and returns nil once their goroutines have returned. When ctx ends first, it closes the rest without a handshake and cancels their calls' contexts, so it is bounded even with a client that never reads, and returns ctx's error once the goroutines are done.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP upgrades GET Path to a WebSocket under physa.v1 and serves it.

type Table

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

Table is the set of methods a server answers, sorted by name.

func NewTable

func NewTable(methods ...Method) *Table

NewTable holds methods sorted by name, adding api.list and api.describe. A name given twice panics: it is a programming error that tests catch.

Jump to

Keyboard shortcuts

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