Documentation
¶
Overview ¶
Package apiwidgets keeps the dashboard widgets that show values read from a JSON address. A recipe says what to read and how to show it, and can be shared: it never chooses the host, and its secrets are inputs that stay on the box.
Index ¶
- Constants
- Variables
- type Basic
- type Card
- type Change
- type Hub
- func (h *Hub) Card(ctx context.Context, id string) (Card, error)
- func (h *Hub) Create(c Change) (View, error)
- func (h *Hub) Delete(id string) error
- func (h *Hub) List() []View
- func (h *Hub) Preview(recipe []byte, response []byte) (Card, error)
- func (h *Hub) Test(ctx context.Context, id string, c Change) (TestResult, error)
- func (h *Hub) Update(id string, c Change) (View, error)
- type Input
- type Recipe
- type Row
- type RowSpec
- type Rule
- type Shown
- type Status
- type StatusSpec
- type TestResult
- type Value
- type View
Constants ¶
const ConfigFile = "/var/lib/casaos/api_widgets.json"
ConfigFile holds the widgets and their secrets (root, 0600).
Variables ¶
var ( // ErrNotFound is an id no widget has. ErrNotFound = errors.New("no such widget") // ErrRead is a read of a widget's address that failed, in Test: the text of // such an error is its reason, and nothing else. ErrRead = errors.New("the address could not be read") // ErrSecretAgain is a change of what is sent, or of where, that came without // every secret: the ones kept were given for another request. ErrSecretAgain = fmt.Errorf("%w: the address or the request changed, every input must be filled again", ErrInvalid) )
var Default = New("/")
Default is the box's own.
var ErrInvalid = errors.New("invalid widget")
ErrInvalid is a recipe that is refused; the message names the field.
Functions ¶
This section is empty.
Types ¶
type Card ¶
type Card struct {
Title string `json:"title"`
Icon string `json:"icon,omitempty"`
Layout string `json:"layout"`
Status *Status `json:"status,omitempty"`
Rows []Row `json:"rows"`
More int `json:"more"`
Link string `json:"link,omitempty"`
Refresh int `json:"refresh"`
Error string `json:"error,omitempty"`
}
Card is what the dashboard draws. More counts the rows left out.
type Change ¶
type Change struct {
Base string `json:"base"`
InsecureTLS bool `json:"insecure_tls"`
Recipe json.RawMessage `json:"recipe"`
Secrets map[string]string `json:"secrets"`
}
Change is what creates or replaces a widget. A secret left out, or empty, is one that is not given.
type Hub ¶
Hub is the API widgets of one box.
func (*Hub) Card ¶
Card is the card of a widget: read when it is asked for, kept refresh seconds, or 60 s when the read failed (Error then says why, and there is no row). There is one read at a time for a widget; who asks meanwhile waits and gets its card.
func (*Hub) Preview ¶
Preview is the card a recipe, which may still be a draft, makes of an answer the caller already has. It sends nothing and needs no secret.
func (*Hub) Test ¶
Test reads the address of a change and answers what it gave, with the card the recipe, which may still be a draft, makes of it. With the id of a widget, the inputs left out take its secrets, by the rule of Update. Nothing is written and nothing is kept. An error of the read shows its reason only.
type Recipe ¶
type Recipe struct {
Version int `json:"recasaos_widget"`
Name string `json:"name"`
Icon string `json:"icon,omitempty"`
Path string `json:"path"`
Query map[string]Value `json:"query,omitzero"`
Headers map[string]Value `json:"headers,omitzero"`
Basic *Basic `json:"basic,omitempty"`
Inputs []Input `json:"inputs,omitzero"`
BaseHint string `json:"base_hint,omitempty"`
Layout string `json:"layout"`
Rows []RowSpec `json:"rows"`
Status *StatusSpec `json:"status,omitempty"`
Refresh int `json:"refresh"`
}
Recipe is the shareable half of a widget.
type Row ¶
type Row struct {
Label string `json:"label"`
Value Shown `json:"value"`
Level string `json:"level,omitempty"`
Bar *float64 `json:"bar,omitempty"`
}
Row is one line of the card. Bar is 0 to 100.
type RowSpec ¶
type RowSpec struct {
Label string `json:"label"`
Value string `json:"value"`
Each *string `json:"each,omitempty"` // "" is the root, so presence is what counts
Format string `json:"format,omitempty"`
Unit string `json:"unit,omitempty"`
Rules []Rule `json:"rules,omitzero"`
Limit int `json:"limit,omitempty"`
Bar bool `json:"bar,omitempty"`
}
RowSpec is one line of the card. With Each, it is one line per element of that array, and Label and Value are pointers inside the element.
type Rule ¶
type Rule struct {
Op string `json:"op"`
To any `json:"to"`
Level string `json:"level"`
Text string `json:"text,omitempty"`
}
Rule gives a level, and maybe another text, to a value; the first true one wins.
type Shown ¶
type Shown struct {
Kind string `json:"kind"` // text, number, bytes, percent, duration, date (Unix seconds) or missing
Text string `json:"text,omitempty"`
Number *float64 `json:"number,omitempty"`
Unit string `json:"unit,omitempty"`
}
Shown is a value ready for the card. The browser writes numbers in the reader's language, so only a text comes written.
type StatusSpec ¶
type StatusSpec struct {
Value string `json:"value"`
Format string `json:"format,omitempty"`
Unit string `json:"unit,omitempty"`
Rules []Rule `json:"rules,omitzero"`
}
StatusSpec fills the card's chip.
type TestResult ¶
type TestResult struct {
Response json.RawMessage `json:"response"`
Card Card `json:"card"`
}
TestResult is what the address answered and the card the recipe makes of it.
type Value ¶
type Value struct{ Text, Input string }
Value is a header, query or basic value: a fixed text, or the input that fills it.
func (Value) MarshalJSON ¶
MarshalJSON writes "text" or {"input":"id"}.
func (*Value) UnmarshalJSON ¶
UnmarshalJSON is as strict as the rest of the recipe: the decoder's own strictness does not reach here.
type View ¶
type View struct {
ID string `json:"id"`
Base string `json:"base"`
InsecureTLS bool `json:"insecure_tls"`
Recipe Recipe `json:"recipe"`
InputsSet map[string]bool `json:"inputs_set"`
// why the last read failed, as long as that card is the one kept
Error string `json:"error"`
}
View is a widget as it is shown: of its secrets, only whether each is filled.