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 ¶
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.
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 ¶
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 (*Server) Close ¶
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.